From 26d6fa9fcc08bc44e43269794e43cc474ca0a095 Mon Sep 17 00:00:00 2001 From: Kroese Date: Sun, 10 Dec 2023 15:58:37 +0100 Subject: [PATCH] feat: Improve shutdown * feat: Improve shutdown --- src/check.sh | 18 +++++++++++++----- src/power.sh | 17 ++++++++++++----- src/print.sh | 12 +++++++++++- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/check.sh b/src/check.sh index 061c495..1247d69 100644 --- a/src/check.sh +++ b/src/check.sh @@ -1,16 +1,22 @@ #!/usr/bin/env bash 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" +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" +[ ! -f "$active" ] && echo "$active_msg" && exit 0 +[ -f "$shutdown" ] && echo "$shutdown_msg" && exit 1 + if [ ! -f "$file" ]; then # Retrieve IP from guest VM for Docker healthcheck { 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 { result=$(echo "$json" | jq -r '.status'); rc=$?; } || : @@ -36,11 +42,13 @@ if [ ! -f "$file" ]; then fi +[ -f "$shutdown" ] && echo "$shutdown_msg" && exit 1 + location=$(cat "$file") if ! curl -m 20 -ILfSs "http://$location/" > /dev/null; then - echo "Failed to reach http://$location" - exit 1 + [ -f "$shutdown" ] && echo "$shutdown_msg" && exit 1 + echo "Failed to reach page at http://$location" && exit 1 fi echo "Healthcheck OK" diff --git a/src/power.sh b/src/power.sh index 61fa332..6fd105b 100644 --- a/src/power.sh +++ b/src/power.sh @@ -27,18 +27,23 @@ _graceful_shutdown() { [ -f "$QEMU_COUNT" ] && return 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 # echo 'system_powerdown' | nc -q 1 -w 1 localhost "${QEMU_PORT}" > /dev/null # 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 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")" pkill -f qemu-system-x86_64 || true @@ -65,7 +70,9 @@ _graceful_shutdown() { echo && echo "❯ Quitting..." 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 sleep 1 diff --git a/src/print.sh b/src/print.sh index 9285cd1..cd829ba 100644 --- a/src/print.sh +++ b/src/print.sh @@ -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; } file="/run/dsm.url" +shutdown="/run/qemu.count" url="http://127.0.0.1:2210/read?command=10" - + while [ ! -f "$file" ] do + # Check if not shutting down + [ -f "$shutdown" ] && exit 1 + sleep 3 + [ -f "$shutdown" ] && exit 1 + # Healthcheck may have intervened [ -f "$file" ] && break # Retrieve IP from guest VM { json=$(curl -m 20 -sk "$url"); rc=$?; } || : + + [ -f "$shutdown" ] && exit 1 (( rc != 0 )) && error "Failed to connect to guest: curl error $rc" && continue { result=$(echo "$json" | jq -r '.status'); rc=$?; } || : @@ -42,6 +50,8 @@ do done +[ -f "$shutdown" ] && exit 1 + location=$(cat "$file") if [[ "$location" != "20.20"* ]]; then