Repository navigation
Expand file tree
/
Copy pathresizevolume.sh
More file actions
executable file
·269 lines (230 loc) · 6.64 KB
/
Copy pathresizevolume.sh
File metadata and controls
executable file
·269 lines (230 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# resizevolume.sh -- resize a volume
usage() {
printf "Usage: %s: -c <current-volume-size> -s <new-volume-size> -p <volume path> -v <vm instance name> -t <storage-type> -r <shrink-bool>\n" $(basename $0) >&2
}
getdevmappername() {
local path=$1
local devmappername=`readlink -f $path |cut -d/ -f3`
if [[ $devmappername =~ "dm-" ]]
then
dmname=$devmappername
return 0
else
return 1;
fi
}
getdevmappersize() {
local dm=$1
if [ ! -e "/sys/block/${dm}/size" ]
then
log "unable to find ${dm} in /sys/block" 1
exit 1
fi
actualsize=$((`cat /sys/block/${dm}/size`*512));
if [[ -z "$actualsize" ]]
then
log "unable to find actual size of ${dm}" 1
exit 1
fi
return 0
}
# log "message" 1 <-- prints to stdout as well as log, to pass error up to cloudstack
# log "message" prints only to log file
# variable shouldwelog controls whether we print to log file
log() {
local d=`date`
local msg=${1}
local stdout=${2}
if [ ! -z "$stdout" ]
then
echo $1
fi
if [ $shouldwelog -eq 1 ]
then
echo "$d - $1" >> /var/log/cloudstack/agent/resizevolume.log
fi
}
failshrink() {
# if this is a shrink operation, fail if commands will shrink the volume and we haven't signed of on shrinking
if [ $actualsize -gt $newsize ]
then
if [ "$shrink" == "false" ]
then
log "result would shrink the volume from $actualsize to $newsize, but confirmation to shrink wasn't passed. Shrink='$shrink'" 1
exit 1
fi
fi
}
notifyqemu() {
#move this back into cloudstack libvirt calls once the libvirt java bindings support block resize
#we try to inform hypervisor of new size, but don't fail if we can't
if `virsh help 2>/dev/null | grep -q blockresize`
then
if `virsh domstate $vmname >/dev/null 2>&1`
then
sizeinkb=$(($newsize/1024))
devicepath=$(virsh domblklist $vmname | grep $path | awk '{print $1}')
virsh blockresize --path $devicepath --size $sizeinkb $vmname >/dev/null 2>&1
retval=$?
if [ -z $retval ] || [ $retval -ne 0 ]
then
log "failed to live resize $path to size of $sizeinkb kb" 1
else
liveresize='true'
fi
fi
fi
}
resizelvm() {
local dmname=''
local actualsize=''
local liveresize='false'
##### sanity checks #####
if ! `lvresize --version > /dev/null 2>&1`
then
log "unable to resolve executable 'lvresize'" 1
exit 1
fi
if ! `virsh --version > /dev/null 2>&1`
then
log "unable to resolve executable 'virsh'" 1
exit 1
fi
##### end sanity #####
if ! getdevmappername $path
then
log "unable to resolve a device mapper dev from $path" 1
exit 1
fi
getdevmappersize $dmname
if [ $actualsize -ne $currentsize ]
then
log "disk isn't the size we think it is: cloudstack said $currentsize, disk said $actualsize."
fi
# if this is a shrink operation, fail if commands will shrink the volume and we haven't signed of on shrinking
failshrink
output=`lvresize -f -L ${newsize}B $path 2>&1`
retval=$?
if [ -z $retval ] || [ $retval -ne 0 ]
then
log "lvresize failed: $output " 1
exit 1
fi
#move this back into cloudstack libvirt calls once the libvirt java bindings support block resize
#we try to inform hypervisor of new size, but don't fail if we can't
notifyqemu
log "performed successful resize - dm:$dmname currentsize:$currentsize newsize:$newsize path:$path type:$ptype vmname:$vmname live:$liveresize shrink:$shrink"
}
resizeqcow2() {
##### sanity checks #####
if [ ! -e "$path" ]
then
log "unable to find file $path" 1
exit 1
fi
if ! `qemu-img info /dev/null > /dev/null 2>&1`
then
log "unable to resolve executable 'qemu-img'" 1
exit 1
fi
if ! `virsh --version > /dev/null 2>&1`
then
log "unable to resolve executable 'virsh'" 1
exit 1
fi
##### end sanity #####
actualsize=`qemu-img info $path | grep "virtual size" | sed -re 's/^.*\(([0-9]+).*$/\1/g'`
if [ $actualsize -ne $currentsize ]
then
log "disk isn't the size we think it is: cloudstack said $currentsize, disk said $actualsize."
fi
# if this is a shrink operation, fail if commands will shrink the volume and we haven't signed of on shrinking
failshrink
#move this back into cloudstack libvirt calls once the libvirt java bindings support block resize
#we try to inform hypervisor of new size, but don't fail if we can't
if `virsh help 2>/dev/null | grep -q blockresize`
then
if `virsh domstate $vmname >/dev/null 2>&1`
then
log "vm $vmname is running, use 'virsh blockresize' to resize the volume"
notifyqemu
if [ $? -eq 0 ]
then
log "performed successful resize - currentsize:$currentsize newsize:$newsize path:$path type:$ptype vmname:$vmname live:$liveresize shrink:$shrink"
exit 0
fi
fi
fi
output=`qemu-img resize $path $newsize 2>&1`
retval=$?
if [ -z $retval ] || [ $retval -ne 0 ]
then
log "qemu-img resize failed: $output" 1
exit 1
fi
log "performed successful resize - currentsize:$currentsize newsize:$newsize path:$path type:$ptype vmname:$vmname live:$liveresize shrink:$shrink"
}
sflag=
cflag=
pflag=
vflag=
tflag=
rflag=
while getopts 'c:s:v:p:t:r:' OPTION
do
case $OPTION in
s) sflag=1
newsize="$OPTARG"
;;
c) cflag=1
currentsize="$OPTARG"
;;
v) vflag=1
vmname="$OPTARG"
;;
p) dflag=1
path="$OPTARG"
;;
t) tflag=1
ptype="$OPTARG"
;;
r) rflag=1
shrink="$OPTARG"
;;
?) usage
exit 2
;;
esac
done
shouldwelog=1 #set this to 1 while debugging to get output in /var/log/cloudstack/agent/resizevolume.log
if [ "$ptype" == "CLVM" ]
then
resizelvm
elif [ "$ptype" == "QCOW2" ]
then
resizeqcow2
elif [ "$ptype" == "NOTIFYONLY" ]
then
notifyqemu
else
echo "unsupported type $ptype"
exit 1;
fi
exit 0