-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextend.sh
executable file
·189 lines (156 loc) · 5.01 KB
/
extend.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
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
#!/bin/bash
VM=$1
EXTENT=$2
VIRSH=$(which virsh)
QEMU=$(which qemu-img)
POOL=default
POOL_DIR=/var/lib/libvirt/images
RESIZE=$(which virt-resize)
RELEASE=$(which lsb_release)
DISTRO=$($RELEASE -s -i)
FILESYSTEM=$(which virt-filesystems)
usage () {
echo ""
echo "Usage: $0 <vm-name> <desired-size>"
echo ""
echo "This command extends the disk size of an existing virtual machine to a TARGET SIZE."
echo "WARN: this EXPANDS a machine disk size, does NOT work for shrinking."
echo "If the machine disk is using a backing fie, then the disk will be consolidated first,"
echo "then the script will proceed with the extension."
echo ""
echo "You should specify the desired size in G or M, for example"
echo "$0 ubuntu16.test 30G"
}
### Am i Root check
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root, or preceded by sudo."
echo "If sudo does not work, contact your system administrator."
exit 1
fi
### check if virsh command is available
which virsh
if [ $? -ne "0" ]; then
echo "virt-clients not installed, check requirements into README"
exit 1
fi
### check if qemu-img command is available
which qemu-img
if [ $? -ne "0" ]; then
echo "qemu-utils not installed, check requirements into README"
exit 1
fi
if [ -z $1 ]; then
echo "Provide an instance name, pretty please?"
usage
exit 1
fi
if [ -z $2 ]; then
echo "Provide the total amount in Gb you whish to have on the specified VM."
echo "Make sure is bigger than the original."
usage
exit 1
fi
if [ -z $RELEASE ]; then
echo "You need to install lsb_release for your distribution"
exit 1
fi
if [ $DISTRO = "Ubuntu" ]; then
PERMS="libvirt-qemu:kvm"
elif [ $DISTRO = "CentOS" ]; then
PERMS="root:root"
else
echo "For now i can run on Ubuntu and Centos"
echo "This is not the case, so i bail out, sorry."
fi
if [ ! -f /etc/libvirt/qemu/${VM}.xml ]; then
echo "$VM doesn't exists, as far as i can see."
exit 1
fi
check_running () {
$VIRSH list --name | grep -w $VM > /dev/null 2>&1
}
pool_list () {
$VIRSH vol-list --pool $POOL
}
check_running
if [ $? -eq "0" ]; then
echo "INFO: the requested instance $VM is running on this hypervisor."
echo "We will have to turn it down."
$VIRSH shutdown ${VM}
sleep 10
check_running
if [ $? -eq "0" ]; then
echo "$VM is still running, i will terminate it now."
$VIRSH destroy ${VM}
check_running
if [ $? -eq "0" ]; then
echo "$VM is still alive after destroy, something is fishy..."
echo "Bailing out"
exit 1
fi
else
echo "The domain ${VM} has been shutdown, continuing..."
fi
else
echo "the requested instance $VM it's not running, good."
echo "Continuing..."
fi
check_running
if [ $? -eq "0" ]; then
echo "$VM is still running, and we previously tried to destroy it. Bailing out."
exit 1
fi
## Check the backing chain
check_backing () {
$QEMU info --backing-chain $POOL_DIR/${VM}.qcow2 | grep backing
}
## If there is a backing-chain, then we consolidate
## And why not, we use one script that works :-)
check_backing
if [ $? -eq "0" ]; then
./consolidate.sh $VM
echo "But we are to enlarge the disk size, so we continue"
fi
## determine the size of the existing disk
DISK_SIZE=$($QEMU info --backing-chain $POOL_DIR/${VM}.qcow2 | grep disk | cut -f 3 -d " ")
## determine if the existing disk contains a LVM structure
check_lvm () {
$FILESYSTEM --long --csv -a $POOL_DIR/${VM}.qcow2 --volume-groups | grep -v Name
}
## resize the original disk on top of the desired end size
resize_partition () {
## create a disk with the desired output size
$QEMU create -f qcow2 -o preallocation=metadata $POOL_DIR/${VM}-target.qcow2 "$EXTENT"
## resize the original disk on top of the desired end size
echo "extending filesystem assigned to lmv ($TARGET) on $VM to $EXTENT of disk"
$RESIZE --expand $TARGET $POOL_DIR/${VM}.qcow2 $POOL_DIR/${VM}-target.qcow2
## clean up this shit
## rm -rf $POOL_DIR/${VM}.qcow2
mv $POOL_DIR/${VM}.qcow2 $POOL_DIR/${VM}.qcow2.backup
echo "I am keeping a copy of the original volume in $POOL_DIR/${VM}.qcow2.backup."
echo "This is for safety. Remember to clean up with"
echo "$VIRSH vol-delete ${VM}.qcow2.backup --pool default"
echo "if applicable."
mv $POOL_DIR/${VM}-target.qcow2 $POOL_DIR/${VM}.qcow2
chown $PERMS $POOL_DIR/${VM}.qcow2
chmod 644 $POOL_DIR/${VM}.qcow2
## refresh the pool
$VIRSH pool-refresh $POOL
}
## expand filesystem accordingly
check_lvm
if [ $? -eq "0" ]; then
### LVM EXTENDING ###
echo "Volume is using LVM"
## extract the target for expand (onliner tailored for LVM)
TARGET=$($FILESYSTEM --long --csv -a $POOL_DIR/${VM}.qcow2 --volume-groups | grep -v Name | cut -f 4 -d ",")
resize_partition
elif [ $? -eq "1" ]; then
echo "Volume is not using LVM"
## extract the target for expand (onliner tailored for non LVM)
TARGET=$($FILESYSTEM --long --csv -a $POOL_DIR/${VM}.qcow2 | grep -v Name | cut -f 1 -d ",")
resize_partition
else
echo "Not sure is the domain is using LVM or not"
fi
exit 0