From b8da0bf643fd087e478014416a204bcc8967cdba Mon Sep 17 00:00:00 2001 From: Hans Rosenfeld Date: Tue, 24 Mar 2026 11:19:51 +0100 Subject: [PATCH] brand/bhyve: Prevent long delays when shutting down unresponsive VMs Sometimes a VM can get in a state where it doesn't respond in reasonable time to shutdown request, such as when the OS isn't running yet or isn't running anymore, having crashed or otherwise locked up. In such a case, halting the zone can take a very long time. This patch reduces the shutdown timeout from 30min to 20s in the default zone shutdown code, and introduces a force-poweroff in case the guest didn't shut down in time. --- src/brand/bhyve/support | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/brand/bhyve/support b/src/brand/bhyve/support index d68cb929b..9eef77c6f 100755 --- a/src/brand/bhyve/support +++ b/src/brand/bhyve/support @@ -21,15 +21,29 @@ bhyve_shutdown() { typeset -i delay="${1:-60}" log "Killing bhyve process - %d" $delay - pkill -z "$ZONENAME" bhyve + pkill -TERM -z "$ZONENAME" bhyve while (( delay > 0 )) && pgrep -z "$ZONENAME" bhyve >/dev/null; do - ((delay % 30 == 0)) && log "Waiting for bhyve to exit" + ((delay % 5 == 0)) && log "Waiting for bhyve to exit" ((delay = delay - 1)) sleep 1 done log "bhyve shutdown done" } +bhyve_halt() +{ + typeset -i force_halt=0 + while pgrep -z "$ZONENAME" bhyve >/dev/null; do + force_halt=1 + log "Force bhyve poweroff" + /usr/sbin/bhyvectl --vm=$ZONENAME --force-poweroff + sleep 5 + done + if ((force_halt)); then + log "bhyve poweroff done" + fi +} + cmd="${1:?cmd}"; shift ZONENAME="${1:?zonename}"; shift [ -n "$1" ] && { ZONEPATH="$1"; shift; } @@ -50,7 +64,8 @@ case $cmd in ;; $ZONE_STATE_RUNNING:$ZONE_STATE_CMD_HALT) # halting (even as part of rebooting) - bhyve_shutdown 1800 + bhyve_shutdown 20 + bhyve_halt ;; esac ;;