virtual-dsm/run/power.sh

90 lines
2.3 KiB
Bash
Raw Normal View History

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
QEMU_MONPORT=7100
QEMU_POWERDOWN_TIMEOUT=50
2023-04-14 15:34:09 +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}"
_trap(){
func="$1" ; shift
for sig ; do
2023-04-11 05:55:21 +02:00
trap "$func $sig" "$sig"
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-04-21 18:24:19 +02:00
[ ! -f "${_QEMU_PID}" ] && return
2023-04-14 15:34:09 +02:00
[ -f "${_QEMU_SHUTDOWN_COUNTER}" ] && return
2023-04-02 21:38:34 +02:00
2023-05-11 04:02:01 +02:00
echo && info "Received $1 signal, shutting down..."
echo 0 > "${_QEMU_SHUTDOWN_COUNTER}"
# 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-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)
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-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-21 18:23:25 +02:00
if ((AGENT_VERSION > 1)); then
# Send a NMI interrupt which will be detected by the kernel
2023-05-08 19:30:25 +02:00
if ! echo 'nmi' | nc -q 1 -w 1 localhost "${QEMU_MONPORT}" > /dev/null ; then
AGENT_VERSION=0
fi
2023-05-08 14:26:19 +02:00
2023-05-08 19:30:25 +02:00
fi
2023-05-08 21:26:58 +02:00
2023-05-08 19:30:25 +02:00
if ((AGENT_VERSION < 2)); then
2023-04-17 05:13:46 +02:00
2023-05-11 04:02:01 +02:00
echo && info "Please update the VirtualDSM Agent to allow for gracefull shutdowns..."
2023-04-17 05:13:46 +02:00
2023-04-28 23:21:36 +02:00
kill -15 "$(cat "${_QEMU_PID}")"
2023-05-03 06:20:42 +02:00
pkill -f qemu-system-x86_64 || true
2023-04-17 05:13:46 +02:00
fi
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
# Try to connect to qemu
2023-05-08 19:30:25 +02:00
if echo 'info version'| nc -q 1 -w 1 localhost "${QEMU_MONPORT}" >/dev/null 2>&1 ; then
2023-04-02 21:38:34 +02:00
sleep 1
2023-05-11 04:02:01 +02:00
#info "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-05-11 04:02:01 +02:00
echo && info "Quitting..."
2023-05-08 19:30:25 +02:00
echo 'quit' | nc -q 1 -w 1 localhost "${QEMU_MONPORT}" >/dev/null 2>&1 || true
2023-04-02 21:38:34 +02:00
return
2023-04-02 21:38: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"