-
Notifications
You must be signed in to change notification settings - Fork 12
/
shutdown-kvm-guests.sh
58 lines (49 loc) · 1.45 KB
/
shutdown-kvm-guests.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
#!/bin/sh
### BEGIN INIT INFO
# Provides: shutdown-kvm-guests
# Required-Start:
# Required-Stop: shutdown-kvm-guests $remote_fs
# Should-Stop:
# Default-Start:
# Default-Stop: 0 1 6
# Short-Description: Cleanly shut down all running KVM domains.
# Description:
### END INIT INFO
# Inspired by https://bugs.launchpad.net/ubuntu/+source/libvirt/+bug/350936.
# Configure timeout (in seconds).
TIMEOUT=300
VIRSH=/usr/bin/virsh
# List running domains.
list_running_domains() {
$VIRSH list | grep running | awk '{ print $2}'
}
case "$1" in
start,reload,restart,force-reload)
# We don't do anything here.
;;
stop)
echo "Try to cleanly shut down all running KVM domains..."
# Create some sort of semaphore.
touch /tmp/shutdown-kvm-guests
# Try to shutdown each domain, one by one.
list_running_domains | while read DOMAIN; do
# Try to shutdown given domain.
$VIRSH shutdown $DOMAIN
done
# 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 1
done
# Clean up left over domains, one by one.
list_running_domains | while read DOMAIN; do
# Try to shutdown given domain.
$VIRSH destroy $DOMAIN
# Give libvirt some time for killing off the domain.
sleep 3
done
;;
esac