virtual-dsm/src/power.sh

133 lines
2.7 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
API_CMD=6
API_TIMEOUT=50
API_HOST="127.0.0.1:2210"
QEMU_PORT=7100
QEMU_TIMEOUT=50
2023-12-17 10:04:00 +01:00
QEMU_PID="/run/qemu.pid"
QEMU_COUNT="/run/qemu.count"
2023-04-02 21:38:34 +02:00
if [[ "$KVM" == [Nn]* ]]; then
API_TIMEOUT=$(( API_TIMEOUT*2 ))
QEMU_TIMEOUT=$(( QEMU_TIMEOUT*2 ))
fi
rm -f "$QEMU_PID"
rm -f "$QEMU_COUNT"
2023-04-14 15:34:09 +02:00
_trap() {
func="$1" ; shift
for sig ; do
trap "$func $sig" "$sig"
done
}
finish() {
local pid
local reason=$1
if [ -f "$QEMU_PID" ]; then
pid="$(cat "$QEMU_PID")"
echo && error "Forcefully terminating QEMU process, reason: $reason..."
{ kill -15 "$pid" || true; } 2>/dev/null
while isAlive "$pid"; do
sleep 1
# Workaround for zombie pid
[ ! -f "$QEMU_PID" ] && break
done
fi
fKill "print.sh"
fKill "host.bin"
closeNetwork
sleep 1
echo && echo " Shutdown completed!"
exit "$reason"
}
2023-04-21 18:23:25 +02:00
_graceful_shutdown() {
2023-04-02 21:38:34 +02:00
local code=$?
local pid cnt response
2023-04-28 23:21:36 +02:00
set +e
if [ -f "$QEMU_COUNT" ]; then
echo && info "Ignored $1 signal, already shutting down..."
return
fi
echo 0 > "$QEMU_COUNT"
echo && info "Received $1 signal, sending shutdown command..."
if [ ! -f "$QEMU_PID" ]; then
echo && error "QEMU PID file does not exist?"
finish "$code" && return "$code"
fi
pid="$(cat "$QEMU_PID")"
if ! isAlive "$pid"; then
echo && error "QEMU process does not exist?"
finish "$code" && return "$code"
fi
# Don't send the powerdown signal because vDSM ignores ACPI signals
# 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
url="http://$API_HOST/read?command=$API_CMD&timeout=$API_TIMEOUT"
response=$(curl -sk -m "$(( API_TIMEOUT+2 ))" -S "$url" 2>&1)
if [[ "$response" =~ "\"success\"" ]]; then
echo && info "Virtual DSM is now ready to shutdown..."
else
response="${response#*message\"\: \"}"
[ -z "$response" ] && response="second signal"
echo && error "Forcefully terminating because of: ${response%%\"*}"
{ kill -15 "$pid" || true; } 2>/dev/null
2023-10-17 19:24:42 +02:00
fi
while [ "$(cat $QEMU_COUNT)" -lt "$QEMU_TIMEOUT" ]; do
! isAlive "$pid" && break
sleep 1
2023-12-20 02:00:07 +01:00
2023-12-20 12:09:25 +01:00
# Increase the counter
cnt=$(($(cat $QEMU_COUNT)+1))
echo $cnt > "$QEMU_COUNT"
2023-12-20 12:09:25 +01:00
[[ "$DEBUG" == [Yy1]* ]] && info "Shutting down, waiting... ($cnt/$QEMU_TIMEOUT)"
2023-04-17 05:13:46 +02:00
# Workaround for zombie pid
[ ! -f "$QEMU_PID" ] && break
2023-04-02 21:38:34 +02:00
done
2023-12-19 04:30:01 +01:00
if [ "$(cat $QEMU_COUNT)" -ge "$QEMU_TIMEOUT" ]; then
echo && error "Shutdown timeout reached!"
2023-12-19 04:30:01 +01:00
fi
finish "$code" && return "$code"
2023-04-02 21:38:34 +02:00
}
_trap _graceful_shutdown SIGTERM SIGHUP SIGINT SIGABRT SIGQUIT
2023-04-02 21:38:34 +02:00
MON_OPTS="-monitor telnet:localhost:$QEMU_PORT,server,nowait,nodelay"