feat: Improve shutdown

* feat: Improve shutdown
This commit is contained in:
Kroese 2023-12-10 15:58:37 +01:00 committed by GitHub
parent b9f3e52ba4
commit 26d6fa9fcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 11 deletions

View File

@ -1,16 +1,22 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -Eeuo pipefail set -Eeuo pipefail
[ ! -f "/run/qemu.pid" ] && echo "QEMU not running yet.." && exit 0
[ -f "/run/qemu.count" ] && echo "QEMU is shutting down.." && exit 1
file="/run/dsm.url" file="/run/dsm.url"
active="/run/qemu.pid"
shutdown="/run/qemu.count"
active_msg="QEMU not running yet.."
shutdown_msg="QEMU is shutting down.."
url="http://127.0.0.1:2210/read?command=10" url="http://127.0.0.1:2210/read?command=10"
[ ! -f "$active" ] && echo "$active_msg" && exit 0
[ -f "$shutdown" ] && echo "$shutdown_msg" && exit 1
if [ ! -f "$file" ]; then if [ ! -f "$file" ]; then
# Retrieve IP from guest VM for Docker healthcheck # Retrieve IP from guest VM for Docker healthcheck
{ json=$(curl -m 20 -sk "$url"); rc=$?; } || : { json=$(curl -m 20 -sk "$url"); rc=$?; } || :
[ -f "$shutdown" ] && echo "$shutdown_msg" && exit 1
(( rc != 0 )) && echo "Failed to connect to guest: curl error $rc" && exit 1 (( rc != 0 )) && echo "Failed to connect to guest: curl error $rc" && exit 1
{ result=$(echo "$json" | jq -r '.status'); rc=$?; } || : { result=$(echo "$json" | jq -r '.status'); rc=$?; } || :
@ -36,11 +42,13 @@ if [ ! -f "$file" ]; then
fi fi
[ -f "$shutdown" ] && echo "$shutdown_msg" && exit 1
location=$(cat "$file") location=$(cat "$file")
if ! curl -m 20 -ILfSs "http://$location/" > /dev/null; then if ! curl -m 20 -ILfSs "http://$location/" > /dev/null; then
echo "Failed to reach http://$location" [ -f "$shutdown" ] && echo "$shutdown_msg" && exit 1
exit 1 echo "Failed to reach page at http://$location" && exit 1
fi fi
echo "Healthcheck OK" echo "Healthcheck OK"

View File

@ -27,18 +27,23 @@ _graceful_shutdown() {
[ -f "$QEMU_COUNT" ] && return [ -f "$QEMU_COUNT" ] && return
echo 0 > "$QEMU_COUNT" echo 0 > "$QEMU_COUNT"
echo && info "Received $1 signal, shutting down..." echo && info "Received $1 signal, sending shutdown command..."
# Don't send the powerdown signal because vDSM ignores ACPI signals # Don't send the powerdown signal because vDSM ignores ACPI signals
# echo 'system_powerdown' | nc -q 1 -w 1 localhost "${QEMU_PORT}" > /dev/null # echo 'system_powerdown' | nc -q 1 -w 1 localhost "${QEMU_PORT}" > /dev/null
# Send shutdown command to guest agent via serial port # Send shutdown command to guest agent via serial port
url="http://127.0.0.1:2210/read?command=6&timeout=50" url="http://127.0.0.1:2210/read?command=6&timeout=50"
response=$(curl -sk -m 60 -S "$url" 2>&1) response=$(curl -sk -m 52 -S "$url" 2>&1)
if [[ ! "$response" =~ "\"success\"" ]]; then if [[ "$response" =~ "\"success\"" ]]; then
echo && error "Failed to send shutdown command (${response#*message\"\: \"})." 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")" kill -15 "$(cat "$QEMU_PID")"
pkill -f qemu-system-x86_64 || true pkill -f qemu-system-x86_64 || true
@ -65,7 +70,9 @@ _graceful_shutdown() {
echo && echo " Quitting..." echo && echo " Quitting..."
echo 'quit' | nc -q 1 -w 1 localhost "$QEMU_PORT" >/dev/null 2>&1 || true echo 'quit' | nc -q 1 -w 1 localhost "$QEMU_PORT" >/dev/null 2>&1 || true
pkill -f host.bin || true { pkill -f print.sh || true; } 2>/dev/null
{ pkill -f host.bin || true; } 2>/dev/null
closeNetwork closeNetwork
sleep 1 sleep 1

View File

@ -5,18 +5,26 @@ info () { printf "%b%s%b" "\E[1;34m \E[1;36m" "$1" "\E[0m\n" >&2; }
error () { printf "%b%s%b" "\E[1;31m " "ERROR: $1" "\E[0m\n" >&2; } error () { printf "%b%s%b" "\E[1;31m " "ERROR: $1" "\E[0m\n" >&2; }
file="/run/dsm.url" file="/run/dsm.url"
shutdown="/run/qemu.count"
url="http://127.0.0.1:2210/read?command=10" url="http://127.0.0.1:2210/read?command=10"
while [ ! -f "$file" ] while [ ! -f "$file" ]
do do
# Check if not shutting down
[ -f "$shutdown" ] && exit 1
sleep 3 sleep 3
[ -f "$shutdown" ] && exit 1
# Healthcheck may have intervened # Healthcheck may have intervened
[ -f "$file" ] && break [ -f "$file" ] && break
# Retrieve IP from guest VM # Retrieve IP from guest VM
{ json=$(curl -m 20 -sk "$url"); rc=$?; } || : { json=$(curl -m 20 -sk "$url"); rc=$?; } || :
[ -f "$shutdown" ] && exit 1
(( rc != 0 )) && error "Failed to connect to guest: curl error $rc" && continue (( rc != 0 )) && error "Failed to connect to guest: curl error $rc" && continue
{ result=$(echo "$json" | jq -r '.status'); rc=$?; } || : { result=$(echo "$json" | jq -r '.status'); rc=$?; } || :
@ -42,6 +50,8 @@ do
done done
[ -f "$shutdown" ] && exit 1
location=$(cat "$file") location=$(cat "$file")
if [[ "$location" != "20.20"* ]]; then if [[ "$location" != "20.20"* ]]; then