2023-04-02 21:38:34 +02:00
|
|
|
|
#!/usr/bin/env bash
|
2023-05-03 19:00:36 +02:00
|
|
|
|
set -Eeuo pipefail
|
2023-04-02 21:38:34 +02:00
|
|
|
|
|
|
|
|
|
# Configure QEMU for graceful shutdown
|
|
|
|
|
|
2023-11-15 19:58:51 +01:00
|
|
|
|
QEMU_PORT=7100
|
|
|
|
|
QEMU_TIMEOUT=50
|
2023-04-14 15:34:09 +02:00
|
|
|
|
|
2023-11-15 19:58:51 +01:00
|
|
|
|
QEMU_PID=/run/qemu.pid
|
|
|
|
|
QEMU_COUNT=/run/qemu.count
|
2023-04-02 21:38:34 +02:00
|
|
|
|
|
2023-11-15 19:58:51 +01:00
|
|
|
|
rm -f "${QEMU_PID}"
|
|
|
|
|
rm -f "${QEMU_COUNT}"
|
2023-04-14 15:34:09 +02:00
|
|
|
|
|
2023-04-10 21:05:34 +02:00
|
|
|
|
_trap(){
|
|
|
|
|
func="$1" ; shift
|
|
|
|
|
for sig ; do
|
2023-04-11 05:55:21 +02:00
|
|
|
|
trap "$func $sig" "$sig"
|
2023-04-10 21:05:34 +02:00
|
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 18:23:25 +02:00
|
|
|
|
_graceful_shutdown() {
|
2023-04-02 21:38:34 +02:00
|
|
|
|
|
2023-04-28 23:21:36 +02:00
|
|
|
|
set +e
|
|
|
|
|
|
2023-11-15 19:58:51 +01:00
|
|
|
|
[ ! -f "${QEMU_PID}" ] && exit 130
|
|
|
|
|
[ -f "${QEMU_COUNT}" ] && return
|
2023-04-02 21:38:34 +02:00
|
|
|
|
|
2023-05-11 04:02:01 +02:00
|
|
|
|
echo && info "Received $1 signal, shutting down..."
|
2023-11-15 19:58:51 +01:00
|
|
|
|
echo 0 > "${QEMU_COUNT}"
|
2023-04-10 21:05:34 +02:00
|
|
|
|
|
2023-04-11 20:29:09 +02:00
|
|
|
|
# Don't send the powerdown signal because vDSM ignores ACPI signals
|
2023-11-15 19:58:51 +01:00
|
|
|
|
# echo 'system_powerdown' | nc -q 1 -w 1 localhost "${QEMU_PORT}" > /dev/null
|
2023-04-02 21:38:34 +02:00
|
|
|
|
|
2023-05-05 17:24:06 +02:00
|
|
|
|
# Send shutdown command to guest agent via serial port
|
2023-05-08 17:09:24 +02:00
|
|
|
|
RESPONSE=$(curl -s -m 5 -S http://127.0.0.1:2210/read?command=6 2>&1)
|
2023-04-11 20:29:09 +02:00
|
|
|
|
|
|
|
|
|
if [[ ! "${RESPONSE}" =~ "\"success\"" ]] ; then
|
|
|
|
|
|
2023-05-11 04:02:01 +02:00
|
|
|
|
echo && error "Could not send shutdown command to the guest ($RESPONSE)"
|
2023-04-11 20:29:09 +02:00
|
|
|
|
|
2023-11-15 19:58:51 +01:00
|
|
|
|
kill -15 "$(cat "${QEMU_PID}")"
|
2023-11-08 04:32:50 +01:00
|
|
|
|
pkill -f qemu-system-x86_64 || true
|
2023-10-17 19:24:42 +02:00
|
|
|
|
|
2023-04-10 21:05:34 +02:00
|
|
|
|
fi
|
|
|
|
|
|
2023-11-15 19:58:51 +01:00
|
|
|
|
while [ "$(cat ${QEMU_COUNT})" -lt "${QEMU_TIMEOUT}" ]; do
|
2023-04-10 21:05:34 +02:00
|
|
|
|
|
|
|
|
|
# Increase the counter
|
2023-11-15 19:58:51 +01:00
|
|
|
|
echo $(($(cat ${QEMU_COUNT})+1)) > ${QEMU_COUNT}
|
2023-04-02 21:38:34 +02:00
|
|
|
|
|
2023-04-10 21:05:34 +02:00
|
|
|
|
# Try to connect to qemu
|
2023-11-15 19:58:51 +01:00
|
|
|
|
if echo 'info version'| nc -q 1 -w 1 localhost "${QEMU_PORT}" >/dev/null 2>&1 ; then
|
2023-04-02 21:38:34 +02:00
|
|
|
|
|
2023-04-10 21:05:34 +02:00
|
|
|
|
sleep 1
|
2023-11-08 04:32:50 +01:00
|
|
|
|
|
2023-11-15 19:58:51 +01:00
|
|
|
|
CNT="$(cat ${QEMU_COUNT})/${QEMU_TIMEOUT}"
|
2023-11-11 17:05:58 +01:00
|
|
|
|
[[ "${DEBUG}" == [Yy1]* ]] && info "Shutting down, waiting... (${CNT})"
|
2023-04-10 21:05:34 +02:00
|
|
|
|
|
|
|
|
|
fi
|
2023-04-17 05:13:46 +02:00
|
|
|
|
|
2023-04-02 21:38:34 +02:00
|
|
|
|
done
|
|
|
|
|
|
2023-05-11 05:51:05 +02:00
|
|
|
|
echo && echo "❯ Quitting..."
|
2023-11-15 19:58:51 +01:00
|
|
|
|
echo 'quit' | nc -q 1 -w 1 localhost "${QEMU_PORT}" >/dev/null 2>&1 || true
|
2023-04-02 21:38:34 +02:00
|
|
|
|
|
2023-07-04 18:21:37 +02:00
|
|
|
|
closeNetwork
|
|
|
|
|
|
2023-04-10 21:05:34 +02:00
|
|
|
|
return
|
2023-04-02 21:38:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 21:05:34 +02:00
|
|
|
|
_trap _graceful_shutdown SIGTERM SIGHUP SIGINT SIGABRT SIGQUIT
|
2023-04-02 21:38:34 +02:00
|
|
|
|
|
2023-11-15 19:58:51 +01:00
|
|
|
|
MON_OPTS="-monitor telnet:localhost:${QEMU_PORT},server,nowait,nodelay"
|