forked from vdsm/virtual-dsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower.sh
40 lines (28 loc) · 1.12 KB
/
power.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
#!/usr/bin/env bash
set -eu
# Configure QEMU for graceful shutdown
QEMU_MONPORT=7100
QEMU_POWERDOWN_TIMEOUT=30
_graceful_shutdown() {
local COUNT=0
local QEMU_MONPORT="${QEMU_MONPORT:-7100}"
local QEMU_POWERDOWN_TIMEOUT="${QEMU_POWERDOWN_TIMEOUT:-120}"
set +e
echo "Trying to shutdown gracefully.."
# Send a NMI interrupt which will be detected by the agent
# echo 'nmi' | nc -q 1 localhost "${QEMU_MONPORT}">/dev/null 2>&1
echo 'system_powerdown' | nc -q 1 localhost "${QEMU_MONPORT}">/dev/null 2>&1
echo ""
while echo 'info version'|nc -q 1 localhost "${QEMU_MONPORT:-7100}">/dev/null 2>&1 && [ "${COUNT}" -lt "${QEMU_POWERDOWN_TIMEOUT}" ]; do
(( COUNT++ )) || true
echo "Shutting down, waiting... (${COUNT}/${QEMU_POWERDOWN_TIMEOUT})"
sleep 1
done
if echo 'info version'|nc -q 1 localhost "${QEMU_MONPORT:-7100}">/dev/null 2>&1; then
echo "Killing the VM.."
echo 'quit' | nc -q 1 localhost "${QEMU_MONPORT}">/dev/null 2>&1 || true
fi
echo "Exiting..."
}
trap _graceful_shutdown SIGINT SIGTERM SIGHUP
KVM_MON_OPTS="-monitor telnet:localhost:${QEMU_MONPORT:-7100},server,nowait,nodelay"