feat: Improve shutdown (#448)

* feat: Improve shutdown
This commit is contained in:
Kroese 2023-12-10 09:22:35 +01:00 committed by GitHub
parent 03d2665725
commit b9f3e52ba4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 10 deletions

View File

@ -20,4 +20,4 @@ services:
volumes: volumes:
- /opt/dsm:/storage - /opt/dsm:/storage
restart: on-failure restart: on-failure
stop_grace_period: 1m stop_grace_period: 2m

View File

@ -41,7 +41,7 @@ services:
volumes: volumes:
- /opt/dsm:/storage - /opt/dsm:/storage
restart: on-failure restart: on-failure
stop_grace_period: 1m stop_grace_period: 2m
``` ```
Via `docker run` Via `docker run`

View File

@ -5,12 +5,12 @@ set -Eeuo pipefail
[ -f "/run/qemu.count" ] && echo "QEMU is shutting down.." && exit 1 [ -f "/run/qemu.count" ] && echo "QEMU is shutting down.." && exit 1
file="/run/dsm.url" file="/run/dsm.url"
url="http://127.0.0.1:2210/read?command=10"
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 30 -sk http://127.0.0.1:2210/read?command=10); rc=$?; } || :
(( 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=$?; } || :

View File

@ -156,11 +156,15 @@ closeNetwork () {
if [[ "$DHCP" == [Yy1]* ]]; then if [[ "$DHCP" == [Yy1]* ]]; then
{ pkill -f server.sh || true; } 2>/dev/null
ip link set "$VM_NET_TAP" down || true ip link set "$VM_NET_TAP" down || true
ip link delete "$VM_NET_TAP" || true ip link delete "$VM_NET_TAP" || true
else else
{ pkill -f dnsmasq || true; } 2>/dev/null
ip link set "$VM_NET_TAP" down promisc off || true ip link set "$VM_NET_TAP" down promisc off || true
ip link delete "$VM_NET_TAP" || true ip link delete "$VM_NET_TAP" || true

View File

@ -33,11 +33,12 @@ _graceful_shutdown() {
# 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
response=$(curl -sk -m 30 -S http://127.0.0.1:2210/read?command=6 2>&1) url="http://127.0.0.1:2210/read?command=6&timeout=50"
response=$(curl -sk -m 60 -S "$url" 2>&1)
if [[ ! "$response" =~ "\"success\"" ]]; then if [[ ! "$response" =~ "\"success\"" ]]; then
echo && error "Failed to send shutdown command ( $response )." echo && error "Failed to send shutdown command (${response#*message\"\: \"})."
kill -15 "$(cat "$QEMU_PID")" kill -15 "$(cat "$QEMU_PID")"
pkill -f qemu-system-x86_64 || true pkill -f qemu-system-x86_64 || true
@ -64,7 +65,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
closeNetwork closeNetwork
sleep 1
return return
} }

View File

@ -5,16 +5,18 @@ 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"
url="http://127.0.0.1:2210/read?command=10"
while [ ! -f "$file" ] while [ ! -f "$file" ]
do do
sleep 3 sleep 3
[ -f "$file" ] && continue
# Healthcheck may have intervened
[ -f "$file" ] && break
# Retrieve IP from guest VM # Retrieve IP from guest VM
{ json=$(curl -m 20 -sk "$url"); rc=$?; } || :
{ json=$(curl -m 30 -sk http://127.0.0.1:2210/read?command=10); rc=$?; } || :
(( 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=$?; } || :