virtual-dsm/src/power.sh

74 lines
1.6 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
[ ! -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, shutting down..."
# 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
RESPONSE=$(curl -sk -m 30 -S http://127.0.0.1:2210/read?command=6 2>&1)
if [[ ! "${RESPONSE}" =~ "\"success\"" ]]; then
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}"
2023-11-11 17:05:58 +01:00
[[ "${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
2023-07-04 18:21:37 +02:00
closeNetwork
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"