Switch to using bash traps to destroy bhyve vms.

This commit is contained in:
Tom Alexander
2023-07-14 11:18:38 -04:00
parent fb759470f5
commit cd6d8f3f69
2 changed files with 30 additions and 5 deletions

View File

@@ -35,6 +35,33 @@ if [ "$VERBOSE" = "YES" ]; then
set -x
fi
############## Setup #########################
function cleanup {
for vm in "${vms[@]}"; do
log "Destroying bhyve vm $f"
bhyvectl "--vm=$vm" --destroy
log "Destroyed bhyve vm $f"
done
}
vms=()
for sig in EXIT INT QUIT HUP TERM; do
trap "set +e; cleanup" "$sig"
done
function die {
local status_code="$1"
shift
(>&2 echo "${@}")
exit "$status_code"
}
function log {
(>&2 echo "${@}")
}
############## Program #########################
function main {
local cmd="$1"
shift 1
@@ -122,6 +149,7 @@ function start_vm {
if [ "$VNC_ENABLE" = "YES" ]; then
additional_args+=("-s" "29,fbuf,tcp=$VNC_LISTEN,w=1920,h=1080")
fi
vms+=("$name")
while true; do
set -x
set +e
@@ -156,9 +184,6 @@ function start_vm {
break
fi
done
bhyvectl "--vm=$name" --destroy
echo "Destroyed bhyve vm."
}
function detect_available_link {