Compare commits

...

3 Commits
v4.43 ... v4.44

Author SHA1 Message Date
Kroese
007d20c315 fix: Message variables
* fix: Message variables
2023-12-10 16:54:02 +01:00
Kroese
26d6fa9fcc feat: Improve shutdown
* feat: Improve shutdown
2023-12-10 15:58:37 +01:00
Kroese
b9f3e52ba4 feat: Improve shutdown (#448)
* feat: Improve shutdown
2023-12-10 09:22:35 +01:00
7 changed files with 66 additions and 26 deletions

View File

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

View File

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

View File

@@ -1,21 +1,30 @@
#!/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"
url="http://127.0.0.1:2210/read?command=10"
active_msg="QEMU not running yet.."
shutdown_msg="QEMU is shutting down.."
resp_err="Guest returned an invalid response:"
jq_err="Failed to parse response from guest: jq error"
[ ! -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=$?; } || :
{ json=$(curl -m 30 -sk http://127.0.0.1:2210/read?command=10); 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=$?; } || :
(( rc != 0 )) && echo "Failed to parse response from guest: jq error $rc ( $json )" && exit 1
[[ "$result" == "null" ]] && echo "Guest returned invalid response: $json" && exit 1
(( rc != 0 )) && echo "$jq_err $rc ( $json )" && exit 1
[[ "$result" == "null" ]] && echo "$resp_err $json" && exit 1
if [[ "$result" != "success" ]] ; then
{ msg=$(echo "$json" | jq -r '.message'); rc=$?; } || :
@@ -23,24 +32,26 @@ if [ ! -f "$file" ]; then
fi
{ port=$(echo "$json" | jq -r '.data.data.dsm_setting.data.http_port'); rc=$?; } || :
(( rc != 0 )) && echo "Failed to parse response from guest: jq error $rc ( $json )" && exit 1
[[ "$port" == "null" ]] && echo "Guest has not set a portnumber yet.." && exit 1
(( rc != 0 )) && echo "$jq_err $rc ( $json )" && exit 1
[[ "$port" == "null" ]] && echo "$resp_err $json" && exit 1
[ -z "$port" ] && echo "Guest has not set a portnumber yet.." && exit 1
{ ip=$(echo "$json" | jq -r '.data.data.ip.data[] | select((.name=="eth0") and has("ip")).ip'); rc=$?; } || :
(( rc != 0 )) && echo "Failed to parse response from guest: jq error $rc ( $json )" && exit 1
[[ "$ip" == "null" ]] && echo "Guest returned invalid response: $json" && exit 1
(( rc != 0 )) && echo "$jq_err $rc ( $json )" && exit 1
[[ "$ip" == "null" ]] && echo "$resp_err $json" && exit 1
[ -z "$ip" ] && echo "Guest has not received an IP yet.." && exit 1
echo "$ip:$port" > $file
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"

View File

@@ -29,6 +29,7 @@ DL_GLOBAL="https://global.synologydownload.com/download/DSM"
if [ -z "$DL" ]; then
[ -z "$COUNTRY" ] && setCountry
[ -z "$COUNTRY" ] && info "Warning: could not detect country to select mirror!"
[[ "${COUNTRY^^}" == "CN" ]] && DL="$DL_CHINA" || DL="$DL_GLOBAL"
fi

View File

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

View File

@@ -27,17 +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
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 52 -S "$url" 2>&1)
if [[ ! "$response" =~ "\"success\"" ]]; then
if [[ "$response" =~ "\"success\"" ]]; then
echo && error "Failed to send shutdown command ( $response )."
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
@@ -64,7 +70,11 @@ _graceful_shutdown() {
echo && echo " Quitting..."
echo 'quit' | nc -q 1 -w 1 localhost "$QEMU_PORT" >/dev/null 2>&1 || true
{ pkill -f print.sh || true; } 2>/dev/null
{ pkill -f host.bin || true; } 2>/dev/null
closeNetwork
sleep 1
return
}

View File

@@ -5,21 +5,33 @@ 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"
resp_err="Guest returned an invalid response:"
jq_err="Failed to parse response from guest: jq error"
while [ ! -f "$file" ]
do
# Check if not shutting down
[ -f "$shutdown" ] && exit 1
sleep 3
[ -f "$file" ] && continue
[ -f "$shutdown" ] && exit 1
# Healthcheck may have intervened
[ -f "$file" ] && break
# 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=$?; } || :
[ -f "$shutdown" ] && exit 1
(( rc != 0 )) && error "Failed to connect to guest: curl error $rc" && continue
{ result=$(echo "$json" | jq -r '.status'); rc=$?; } || :
(( rc != 0 )) && error "Failed to parse response from guest: jq error $rc ( $json )" && continue
[[ "$result" == "null" ]] && error "Guest returned invalid response: $json" && continue
(( rc != 0 )) && error "$jq_err $rc ( $json )" && continue
[[ "$result" == "null" ]] && error "$resp_err $json" && continue
if [[ "$result" != "success" ]] ; then
{ msg=$(echo "$json" | jq -r '.message'); rc=$?; } || :
@@ -27,19 +39,21 @@ do
fi
{ port=$(echo "$json" | jq -r '.data.data.dsm_setting.data.http_port'); rc=$?; } || :
(( rc != 0 )) && error "Failed to parse response from guest: jq error $rc ( $json )" && continue
[[ "$port" == "null" ]] && error "Guest returned invalid response: $json" && continue
(( rc != 0 )) && error "$jq_err $rc ( $json )" && continue
[[ "$port" == "null" ]] && error "$resp_err $json" && continue
[ -z "$port" ] && continue
{ ip=$(echo "$json" | jq -r '.data.data.ip.data[] | select((.name=="eth0") and has("ip")).ip'); rc=$?; } || :
(( rc != 0 )) && error "Failed to parse response from guest: jq error $rc ( $json )" && continue
[[ "$ip" == "null" ]] && error "Guest returned invalid response: $json" && continue
(( rc != 0 )) && error "$jq_err $rc ( $json )" && continue
[[ "$ip" == "null" ]] && error "$resp_err $json" && continue
[ -z "$ip" ] && continue
echo "$ip:$port" > $file
done
[ -f "$shutdown" ] && exit 1
location=$(cat "$file")
if [[ "$location" != "20.20"* ]]; then