-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvirtual-manage
executable file
·450 lines (405 loc) · 13.6 KB
/
virtual-manage
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#!/bin/bash
LC_ALL=C
CLUSTER_GROUP="^${GROUP}.+$"
SNAPSHOT_NAME=""
DISK_DIRECTORY="${HOME}/.cluster/${GROUP}"
DISK_SIZE=10
TIMEOUT=300
GRACEFULLY_SHUTDOWN=false
function configure_etc_hosts {
sudo sed -i '/# BEGIN ANSIBLE MANAGED BLOCK/,/# END ANSIBLE MANAGED BLOCK/d' /etc/hosts
if [ -f "${HOME}/.cluster/${GROUP}/etc_hosts" ]; then
cat "${HOME}/.cluster/${GROUP}/etc_hosts" | sudo tee -a /etc/hosts > /dev/null
echo -e "\e[93m\e[1m==> Modify /etc/hosts with values from ~/.cluster/${GROUP}/etc_hosts\e[0m"
fi
}
function configure_ssh_config {
sed -i '/# BEGIN ANSIBLE MANAGED BLOCK/,/# END ANSIBLE MANAGED BLOCK/d' ~/.ssh/config
if [ -f "${HOME}/.cluster/${GROUP}/ssh_config" ]; then
cat "${HOME}/.cluster/${GROUP}/ssh_config" >> ~/.ssh/config
echo -e "\e[93m\e[1m==> Modify ~/.ssh/config with values from ~/.cluster/${GROUP}/ssh_config\e[0m"
fi
}
function start_networks {
echo -e "\e[93m\e[1m==> Start all inactive networks\e[0m"
list_inactive_networks | while read -r NETWORK; do
if [[ -n "$NETWORK" && "$NETWORK" =~ $CLUSTER_GROUP ]]; then
virsh net-start "$NETWORK" | grep -v "^$"
# restart failed network
if [ $? -ne 0 ]; then
virsh net-start "$NETWORK" | grep -v "^$"
fi
fi
done
echo -e "\e[32m\e[1m==> All networks started :) \e[0m"
}
function shutdown_networks {
echo -e "\e[93m\e[1m==> Shutdown all active networks\e[0m"
list_running_networks | while read -r NETWORK; do
if [[ -n "$NETWORK" && "$NETWORK" =~ $CLUSTER_GROUP ]]; then
virsh net-destroy "$NETWORK" | grep -v "^$"
fi
done
echo -e "\e[32m\e[1m==> All networks shutdown :) \e[0m"
}
function start {
echo -e "\e[93m\e[1m==> Start all inactive domains in ${GROUP} cluster\e[0m"
list_inactive_domains | while read -r DOMAIN; do
if [[ -n "$DOMAIN" && "$DOMAIN" =~ $CLUSTER_GROUP ]]; then
virsh start "$DOMAIN" | grep -v "^$"
fi
done
echo -e "\e[32m\e[1m==> All domains started in ${GROUP} cluster :) \e[0m"
configure_etc_hosts
configure_ssh_config
}
function status {
list_running_domains | while read -r DOMAIN; do
echo -e "$DOMAIN\t\t\e[1m\e[92mrunning\e[39m\e[0m"
echo -e "$(virsh dominfo "$DOMAIN" | grep CPU\(s\))"
echo -e "$(virsh dominfo "$DOMAIN" | grep 'Max memory')"
echo -e ""
echo -e "$(virsh -q domblklist "$DOMAIN" --details)"
echo -e ""
echo -e "$(virsh -q domfsinfo "$DOMAIN")"
echo -e ""
echo -e "$(virsh -q domifaddr "$DOMAIN" --source agent | grep ipv4)"
echo -e "\n"
done
list_inactive_domains | while read -r DOMAIN; do
echo -e "$DOMAIN\t\t\e[1m\e[91mshut off\e[39m\e[0m"
done
}
function cleanup {
list_all_domains | while read -r DOMAIN; do
if [[ -n "$DOMAIN" && "$DOMAIN" =~ $CLUSTER_GROUP ]]; then
virsh destroy "$DOMAIN"
virsh undefine --remove-all-storage "$DOMAIN"
fi
done
}
function clone {
VM_NAME=$1
if [[ -z "$VM_NAME" ]]; then
echo -en "error: expected syntax: virtual-manage --clone <string>\n\n"
exit 0
fi
virt-clone -o "${VM_NAME}" -n "${VM_NAME}"_clone -f "/var/lib/libvirt/images/${VM_NAME}_clone.qcow2"
}
function acpi_shutdown {
list_running_domains | while read -r DOMAIN; do
if [[ -n "$DOMAIN" && "$DOMAIN" =~ $CLUSTER_GROUP ]]; then
virsh shutdown "$DOMAIN" --mode acpi
fi
done
}
function power_shutdown {
echo -e "==> power shutdown all running KVM domains..."
list_running_domains | while read -r DOMAIN; do
if [[ -n "$DOMAIN" && "$DOMAIN" =~ $CLUSTER_GROUP ]]; then
virsh destroy "$DOMAIN" | grep -v "^$"
# Give libvirt some time for killing off the domain.
sleep 3
fi
done
}
function shutdown {
echo -e "\e[93m\e[1m==> Shutdown all domains in ${GROUP} cluster\e[0m"
if [ "$GRACEFULLY_SHUTDOWN" = true ]; then
echo -e "Try to cleanly shutdown all running KVM domains...\n"
acpi_shutdown
# Wait until all domains are shut down or timeout has reached.
END_TIME=$(date -d "$TIMEOUT seconds" +%s)
while [ $(date +%s) -lt "$END_TIME" ]; do
# Break while loop when no domains are left.
test -z "$(list_running_domains)" && break
# Wait a litte, we don't want to DoS libvirt.
sleep 3
done
fi
power_shutdown
sed -i '/# BEGIN ANSIBLE MANAGED BLOCK/,/# END ANSIBLE MANAGED BLOCK/d' ~/.ssh/config
sudo sed -i '/# BEGIN ANSIBLE MANAGED BLOCK/,/# END ANSIBLE MANAGED BLOCK/d' /etc/hosts
echo -e "\e[32m\e[1m==> All domains shutdown in ${GROUP} cluster :)\e[0m"
# list_inactive_domains
}
function restart {
shutdown
start
}
function renew-dhcp {
list_running_domains | while read -r -e DOMAIN; do
if [[ -n "$DOMAIN" && "$DOMAIN" =~ $CLUSTER_GROUP ]]; then
ssh -q -n root@${DOMAIN#"${GROUP}"} "nmcli con up id 'System eth0'" 2>&1 > /dev/null
echo "${DOMAIN#"${GROUP}"} network interface 'eth0' restarted"
fi
done
}
function list_running_networks() {
virsh net-list --persistent --name | grep -v '^[[:space:]]*$'
}
function list_inactive_networks() {
virsh net-list --inactive --name | grep -v '^[[:space:]]*$'
}
function list_inactive_domains() {
virsh list --inactive --name | grep -v '^[[:space:]]*$'
}
function list_running_domains() {
virsh list --state-running --name | grep -v '^[[:space:]]*$'
}
function list_all_domains() {
virsh list --name --all | grep -v '^[[:space:]]*$'
}
function list_all_snapshots() {
DOMAIN=$1
virsh snapshot-list --domain "$DOMAIN" --name | grep -v '^[[:space:]]*$'
}
function add-disk {
list_running_domains | while read -r DOMAIN; do
if [[ -n "$DOMAIN" && "$DOMAIN" =~ $CLUSTER_GROUP ]]; then
sudo qemu-img create -f qcow2 "${DISK_DIRECTORY}/${DOMAIN}/disk-${DISK_SIZE}.qcow2" "${DISK_SIZE}G"
virsh attach-disk --domain "$DOMAIN" --source "${DISK_DIRECTORY}/${DOMAIN}/disk-${DISK_SIZE}.qcow2" --subdriver qcow2 --target vdd --live --cache none
echo "Disk added to $DOMAIN domain"
fi
done
}
function remove-disk {
list_running_domains | while read -r DOMAIN; do
if [[ -n "$DOMAIN" && "$DOMAIN" =~ $CLUSTER_GROUP ]]; then
virsh detach-disk --domain "$DOMAIN" --target vdd --live
sudo rm "${DISK_DIRECTORY}/${DOMAIN}/disk-${DISK_SIZE}.qcow2"
echo "Disk removed from $DOMAIN domain"
fi
done
}
function snap-create {
local snap_exists=0
SNAPSHOT_NAME=$1
if [[ -z $SNAPSHOT_NAME ]]; then
echo -en "error: expected syntax: virtual-manage --snap-create <string>\n\n"
exit 0
fi
# BEGIN without qemu-guest-agent
# shutdown domains
shutdown
while read -r DOMAIN; do
while read -r SNAP; do
if [[ "$SNAPSHOT_NAME" = "$SNAP" ]]; then
echo "Warning : snapshot $SNAPSHOT_NAME already exists on $DOMAIN"
snap_exists=1
break 2
fi
done <<< "$(list_all_snapshots "$DOMAIN")"
done <<< "$(list_inactive_domains)"
if [[ $snap_exists -eq 1 ]]; then
read -r -p "Snapshot $SNAPSHOT_NAME already exists. Recreate them (y/n) ?" -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
snap-delete "$SNAPSHOT_NAME"
else
return
fi
fi
list_inactive_domains | while read -r DOMAIN; do
if [[ -n "$DOMAIN" && "$DOMAIN" =~ $CLUSTER_GROUP ]]; then
virsh snapshot-create-as --domain "$DOMAIN" --name "$SNAPSHOT_NAME" --atomic | grep -E -v "^$"
echo "Snapshot $SNAPSHOT_NAME create for domain $DOMAIN"
fi
done
# start domains
start
# END without qemu-guest-agent
# # BEGIN with qemu-guest-agent
# while read -r DOMAIN; do
# while read -r SNAP; do
# if [[ "$SNAPSHOT_NAME" = "$SNAP" ]]; then
# echo "Warning : snapshot $SNAPSHOT_NAME already exists on $DOMAIN"
# snap_exists=1
# break 2
# fi
# done <<< "$(list_all_snapshots $DOMAIN)"
# done <<< "$(list_running_domains)"
# if [[ $snap_exists -eq 1 ]]; then
# read -r -p "Snapshot $SNAPSHOT_NAME already exists. Recreate them (y/n) ?" -n 1 -r
# if [[ $REPLY =~ ^[Yy]$ ]]; then
# snap-delete $SNAPSHOT_NAME
# else
# return
# fi
# fi
# list_running_domains | while read -r DOMAIN; do
# if [[ "$DOMAIN" =~ $node ]]; then
# virsh snapshot-create-as --domain $DOMAIN --name $SNAPSHOT_NAME --atomic --quiesce --disk-only | egrep -v "^$"
# echo "Snapshot $SNAPSHOT_NAME create for domain $DOMAIN"
# fi
# done
# # END with qemu-guest-agent
}
function snap-delete {
SNAPSHOT_NAME=$1
if [[ -z $SNAPSHOT_NAME ]]; then
echo -en "error: expected syntax: virtual-manage --snap-delete <string>\n\n"
echo -en "snapshots:\n"
snap-list
exit 0
fi
shutdown
list_inactive_domains | while read -r DOMAIN; do
if [[ -n "$DOMAIN" && "$DOMAIN" =~ $CLUSTER_GROUP ]]; then
virsh snapshot-delete "$DOMAIN" --snapshotname "$SNAPSHOT_NAME"
echo "Snapshot $SNAPSHOT_NAME remove for domain $DOMAIN"
fi
done
# start
}
function snap-revert {
SNAPSHOT_NAME=$1
if [[ -z $SNAPSHOT_NAME ]]; then
echo -en "error: expected syntax: virtual-manage --snap-revert <string>\n\n"
echo -en "snapshots:\n"
snap-list
exit 0
fi
shutdown
list_inactive_domains | while read -r DOMAIN; do
if [[ -n "$DOMAIN" && "$DOMAIN" =~ $CLUSTER_GROUP ]]; then
virsh snapshot-revert "$DOMAIN" --snapshotname "$SNAPSHOT_NAME"
echo "Snapshot $SNAPSHOT_NAME revert for domain $DOMAIN"
fi
done
start
}
function snap-list {
list_running_domains | while read -r DOMAIN; do
if [[ -n "$DOMAIN" && "$DOMAIN" =~ $CLUSTER_GROUP ]]; then
# echo -e "\e[33m$DOMAIN [$(virsh snapshot-info $DOMAIN --current | grep Name | cut -d ":" -f2 | sed -e 's/^[ ]*//')]\e[0m"
echo -e "$DOMAIN \e[92m\e[1m[$(virsh snapshot-current --name "$DOMAIN")]\e[0m"
virsh -q snapshot-list --tree --domain "$DOMAIN" 2>&1
fi
done
}
function usage {
cat <<EOF
Usage: $0 COMMAND
Commands:
--all Start all defined Libvirt VMs
--cleanup Cleanup (destroy & undefine) cluster
--clone <VM name> Clone VM to <VM name>_clone
--renew-dhcp Renew DHCP
--restart Restart cluster
--start Start cluster
--shutdown Shutdown cluster
--status Cluster status
--snap-create <snapshot name> Create cluster snapshot
--snap-delete <snapshot name> Delete cluster snapshot
--snap-revert <snapshot name> Revert cluster snapshot
--snap-list List cluster snapshots
--add-disk <disk size> Add disk to VMs
--remove-disk <disk size> Remove disk from VMs
EOF
}
function precheck() {
if [[ -z ${GROUP} ]]; then
echo -en "Error: \$GROUP variable must be defined\n\n"
exit 0
fi
# if [[ ! ${GROUP} =~ ^[[:upper:]]+$ ]]; then
# echo -en "Error: \$GROUP must be uppercase, but get: ${GROUP}\n\n"
# exit 0
# fi
}
LONG_OPTS="all,clone:,renew-dhcp,cleanup,restart,start,shutdown,status,snap-delete:,snap-create:,snap-revert:,snap-list,add-disk:,remove-disk:,help"
ARGS=$(getopt -l "${LONG_OPTS}" --name "$0" -- "$@") || { usage >&2; exit 2; }
while [ "$#" -gt 0 ]; do
case "$1" in
(--all)
ALL_VMS=1
shift 1
;;
(--cleanup)
precheck
cleanup
exit 0
;;
(--clone)
precheck
VM_NAME="$2"
shift 2
clone "$VM_NAME"
exit 0
;;
(--restart)
precheck
restart
exit 0
;;
(--renew-dhcp)
precheck
renew-dhcp
exit 0
;;
(--start)
precheck
start_networks
start
exit 0
;;
(--shutdown)
precheck
shutdown
shutdown_networks
exit 0
;;
(--status)
status
exit 0
;;
(--snap-delete)
precheck
SNAPSHOT_NAME="$2"
shift 2
snap-delete "$SNAPSHOT_NAME"
exit 0
;;
(--add-disk)
precheck
DISK_SIZE="$2"
shift 2
add-disk
exit 0
;;
(--remove-disk)
precheck
DISK_SIZE="$2"
shift 2
remove-disk
exit 0
;;
(--snap-create)
precheck
SNAPSHOT_NAME="$2"
shift 2
snap-create "$SNAPSHOT_NAME"
exit 0
;;
(--snap-revert)
precheck
SNAPSHOT_NAME="$2"
shift 2
snap-revert "$SNAPSHOT_NAME"
exit 0
;;
(--snap-list)
precheck
snap-list
exit 0
;;
(--help|-h)
usage
exit 0
;;
(*)
usage
exit 3
;;
esac
done