forked from virt-s1/image2clouds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expand_image_disk.sh
executable file
·47 lines (41 loc) · 1.3 KB
/
expand_image_disk.sh
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
#!/bin/bash
#
# Description:
# Expand /dev/sda1 in $IMAGE_FILE.
#
# History:
# v1.0 2020-02-05 charles.shih Move this logic out from another script.
# v1.1 2020-02-10 charles.shih Check VM state before executing
# Parse parameters
if [ -z "$1" ]; then
echo "Usage: $0 <Size in GiB>"
exit 1
fi
# Load profile and verify the veribles
source ./profile
[ -z "$WORKSPACE" ] && echo "\$WORKSPACE is essintial but not existing, exit." && exit 1
[ -z "$IMAGE_FILE" ] && echo "\$IMAGE_FILE is essintial but not existing, exit." && exit 1
# Check utilities
qemu-img -V >/dev/null || exit 1
virt-resize -V >/dev/null || exit 1
# Check VM state
$(dirname $0)/check_vm_state.sh undefined
if [ "$?" != "0" ]; then
$(dirname $0)/check_vm_state.sh shutoff
if [ "$?" != "0" ]; then
echo "ERROR: The VM must be stopped first."
exit 1
fi
fi
# Enlarge the image
size=$1
echo -e "\nEnlarge the image to $size GiB..."
fsize=$(ls -l $IMAGE_FILE | awk '{print $5}')
if [ "$fsize" -lt "$(($size * 1024 * 1024 * 1024))" ]; then
qemu-img create -f qcow2 -o preallocation=metadata $WORKSPACE/newdisk.qcow2 ${size}G || exit 1
virt-resize --expand /dev/sda1 $IMAGE_FILE $WORKSPACE/newdisk.qcow2 || exit 1
mv -f $WORKSPACE/newdisk.qcow2 $IMAGE_FILE || exit 1
else
echo -e "Already enlarged to $size GiB, skip this operation."
fi
exit 0