virtual-dsm/src/power.sh

85 lines
1.9 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_PORT=7100
QEMU_TIMEOUT=50
QEMU_PID=/run/qemu.pid
QEMU_COUNT=/run/qemu.count
2023-04-02 21:38:34 +02:00
rm -f "$QEMU_PID"
rm -f "$QEMU_COUNT"
2023-04-14 15:34:09 +02:00
_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
local cnt response
2023-04-28 23:21:36 +02:00
[ ! -f "$QEMU_PID" ] && exit 130
[ -f "$QEMU_COUNT" ] && return
2023-04-02 21:38:34 +02:00
echo 0 > "$QEMU_COUNT"
echo && info "Received $1 signal, sending shutdown command..."
# 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://127.0.0.1:2210/read?command=6&timeout=50"
response=$(curl -sk -m 52 -S "$url" 2>&1)
if [[ "$response" =~ "\"success\"" ]]; then
echo && info "Virtual DSM is now ready to shutdown..."
else
response="${response#*message\"\: \"}"
echo && error "Failed to send shutdown command: ${response%%\"*}"
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
fi
while [ "$(cat $QEMU_COUNT)" -lt "$QEMU_TIMEOUT" ]; do
# Increase the counter
echo $(($(cat $QEMU_COUNT)+1)) > "$QEMU_COUNT"
2023-04-02 21:38:34 +02:00
# Try to connect to qemu
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
sleep 1
2023-11-08 04:32:50 +01:00
cnt="$(cat $QEMU_COUNT)/$QEMU_TIMEOUT"
[[ "$DEBUG" == [Yy1]* ]] && info "Shutting down, waiting... ($cnt)"
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..."
echo 'quit' | nc -q 1 -w 1 localhost "$QEMU_PORT" >/dev/null 2>&1 || true
2023-04-02 21:38:34 +02:00
{ pkill -f print.sh || true; } 2>/dev/null
{ pkill -f host.bin || true; } 2>/dev/null
2023-07-04 18:21:37 +02:00
closeNetwork
sleep 1
2023-07-04 18:21:37 +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
MON_OPTS="-monitor telnet:localhost:$QEMU_PORT,server,nowait,nodelay"