Unverified Commit 26d6fa9f authored by Kroese's avatar Kroese Committed by GitHub
Browse files

feat: Improve shutdown

* feat: Improve shutdown
parent b9f3e52b
Loading
Loading
Loading
Loading
+13 −5
Original line number Diff line number Diff line
#!/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"
+12 −5
Original line number Diff line number Diff line
@@ -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

+11 −1
Original line number Diff line number Diff line
@@ -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