2023-04-02 21:38:34 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -eu
|
|
|
|
|
|
|
|
# Configure QEMU for graceful shutdown
|
|
|
|
|
|
|
|
QEMU_MONPORT=7100
|
2023-04-11 20:29:09 +02:00
|
|
|
QEMU_POWERDOWN_TIMEOUT=50
|
2023-04-14 15:34:09 +02:00
|
|
|
|
2023-04-10 21:05:34 +02:00
|
|
|
_QEMU_PID=/run/qemu.pid
|
|
|
|
_QEMU_SHUTDOWN_COUNTER=/run/qemu.counter
|
2023-04-02 21:38:34 +02:00
|
|
|
|
2023-04-14 15:34:09 +02:00
|
|
|
rm -f "${_QEMU_PID}"
|
|
|
|
rm -f "${_QEMU_SHUTDOWN_COUNTER}"
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
_graceful_shutdown(){
|
2023-04-02 21:38:34 +02:00
|
|
|
|
2023-04-14 15:34:09 +02:00
|
|
|
[ -f "${_QEMU_SHUTDOWN_COUNTER}" ] && return
|
2023-04-02 21:38:34 +02:00
|
|
|
|
|
|
|
set +e
|
2023-04-17 05:13:46 +02:00
|
|
|
|
2023-04-15 00:32:02 +02:00
|
|
|
echo
|
2023-04-11 20:29:09 +02:00
|
|
|
echo "Received $1 signal, shutting down..."
|
2023-04-10 21:05:34 +02:00
|
|
|
echo 0 > "${_QEMU_SHUTDOWN_COUNTER}"
|
|
|
|
|
2023-04-11 20:29:09 +02:00
|
|
|
# Don't send the powerdown signal because vDSM ignores ACPI signals
|
2023-04-17 05:11:48 +02:00
|
|
|
# echo 'system_powerdown' | nc -q 1 -w 1 localhost "${QEMU_MONPORT}" > /dev/null
|
2023-04-02 21:38:34 +02:00
|
|
|
|
2023-04-17 19:01:28 +02:00
|
|
|
# Send shutdown command to host via serial port
|
2023-04-17 05:07:38 +02:00
|
|
|
RESPONSE=$(curl -s -m 2 -S http://127.0.0.1:2210/write?command=6 2>&1)
|
2023-04-11 20:29:09 +02:00
|
|
|
|
|
|
|
if [[ ! "${RESPONSE}" =~ "\"success\"" ]] ; then
|
|
|
|
|
2023-04-15 00:32:02 +02:00
|
|
|
echo
|
2023-04-11 20:29:09 +02:00
|
|
|
echo "Could not send shutdown command to guest, error: $RESPONSE"
|
|
|
|
|
2023-04-17 19:01:28 +02:00
|
|
|
# If we cannot shutdown the usual way, fallback to the NMI method
|
|
|
|
|
2023-04-17 03:24:43 +02:00
|
|
|
AGENT="${STORAGE}/${BASE}.agent"
|
2023-04-18 18:06:59 +02:00
|
|
|
[ -f "$AGENT" ] && AGENT_VERSION=$(cat "${AGENT}") || AGENT_VERSION=1
|
2023-04-11 20:29:09 +02:00
|
|
|
|
|
|
|
if ((AGENT_VERSION < 2)); then
|
2023-04-17 05:13:46 +02:00
|
|
|
|
2023-04-15 00:32:02 +02:00
|
|
|
echo
|
2023-04-17 18:45:42 +02:00
|
|
|
echo "Please update the VirtualDSM Agent to allow gracefull shutdowns..."
|
2023-04-17 05:13:46 +02:00
|
|
|
|
2023-04-11 20:29:09 +02:00
|
|
|
pkill -f qemu-system-x86_64
|
2023-04-17 05:13:46 +02:00
|
|
|
|
2023-04-11 20:29:09 +02:00
|
|
|
else
|
2023-04-17 05:13:46 +02:00
|
|
|
|
2023-04-11 20:29:09 +02:00
|
|
|
# Send a NMI interrupt which will be detected by the kernel
|
2023-04-17 05:11:48 +02:00
|
|
|
echo 'nmi' | nc -q 1 -w 1 localhost "${QEMU_MONPORT}" > /dev/null
|
2023-04-11 20:29:09 +02:00
|
|
|
|
2023-04-17 05:13:46 +02:00
|
|
|
fi
|
2023-04-10 21:05:34 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
while [ "$(cat ${_QEMU_SHUTDOWN_COUNTER})" -lt "${QEMU_POWERDOWN_TIMEOUT}" ]; do
|
|
|
|
|
|
|
|
# Increase the counter
|
|
|
|
echo $(($(cat ${_QEMU_SHUTDOWN_COUNTER})+1)) > ${_QEMU_SHUTDOWN_COUNTER}
|
2023-04-02 21:38:34 +02:00
|
|
|
|
2023-04-10 21:05:34 +02:00
|
|
|
# Try to connect to qemu
|
2023-04-17 05:11:48 +02:00
|
|
|
if echo 'info version'| nc -q 1 -w 1 localhost "${QEMU_MONPORT}" > /dev/null; then
|
2023-04-02 21:38:34 +02:00
|
|
|
|
2023-04-10 21:05:34 +02:00
|
|
|
sleep 1
|
|
|
|
#echo "Shutting down, waiting... ($(cat ${_QEMU_SHUTDOWN_COUNTER})/${QEMU_POWERDOWN_TIMEOUT})"
|
|
|
|
|
|
|
|
fi
|
2023-04-17 05:13:46 +02:00
|
|
|
|
2023-04-02 21:38:34 +02:00
|
|
|
done
|
|
|
|
|
2023-04-15 00:32:02 +02:00
|
|
|
echo
|
2023-04-11 20:29:09 +02:00
|
|
|
echo "Quitting..."
|
2023-04-17 05:11:48 +02:00
|
|
|
echo 'quit' | nc -q 1 -w 1 localhost "${QEMU_MONPORT}" > /dev/null || true
|
2023-04-02 21:38:34 +02:00
|
|
|
|
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-04-19 16:15:44 +02:00
|
|
|
MON_OPTS="-monitor telnet:localhost:${QEMU_MONPORT},server,nowait,nodelay"
|