Improve VM lifecycle tooling

Make spawned VMs easier to use and restore from the host.

Add shared DNS and runtime helpers, publish <vm-name>.vm records through mapdns, and teach run/customize/interactive/restore to persist the metadata needed for SSH, DNS cleanup, and clean restores.

Seed per-VM /home and /var disks from the rootfs snapshot so package state is present on first boot, add an interactive customization entrypoint plus ssh.sh and human-friendly list output, and let stop/kill/rm operate on multiple VM identifiers.

Tear down stale TAP, dm, and loop state when VMs stop so restore can recreate them safely, and validate the updated scripts with bash -n plus targeted dry-run harnesses for teardown and restore paths.
This commit is contained in:
Thales Maciel 2026-03-15 17:48:47 -03:00
parent a8078f2393
commit 9191b7e370
No known key found for this signature in database
GPG key ID: 33112E6833C34679
11 changed files with 966 additions and 144 deletions

View file

@ -13,17 +13,20 @@ Restarts a VM using existing disks and COW snapshot.
EOF
}
find_vm_meta() {
local query="$1"
local meta_json match_count=0 match=""
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$DIR/dns.sh"
for meta_json in state/vms/*/meta.json; do
[[ -f "$meta_json" ]] || continue
find_vm_json() {
local query="$1"
local vm_json match_count=0 match=""
for vm_json in "$DIR"/state/vms/*/vm.json; do
[[ -f "$vm_json" ]] || continue
local id name
id="$(jq -r '.id // empty' "$meta_json")"
name="$(jq -r '.name // empty' "$meta_json")"
id="$(jq -r '.meta.id // empty' "$vm_json")"
name="$(jq -r '.meta.name // empty' "$vm_json")"
if [[ "$id" == "$query"* || "$name" == "$query"* ]]; then
match="$meta_json"
match="$vm_json"
match_count=$((match_count + 1))
fi
done
@ -41,12 +44,15 @@ find_vm_meta() {
}
QUERY="${1:-}"
if [[ -z "$QUERY" || "$QUERY" == "-h" || "$QUERY" == "--help" ]]; then
if [[ "$QUERY" == "-h" || "$QUERY" == "--help" ]]; then
usage
exit 0
fi
if [[ -z "$QUERY" ]]; then
usage
exit 1
fi
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
FC_BIN="$DIR/firecracker"
if ! command -v jq >/dev/null 2>&1; then
@ -62,31 +68,39 @@ if ! command -v e2cp >/dev/null 2>&1 || ! command -v e2rm >/dev/null 2>&1; then
exit 1
fi
META_JSON="$(find_vm_meta "$QUERY")"
VM_DIR="$(dirname "$META_JSON")"
VM_JSON="$VM_DIR/vm.json"
VM_JSON="$(find_vm_json "$QUERY")"
VM_DIR="$(dirname "$VM_JSON")"
ROOTFS="$(jq -r '.rootfs // empty' "$META_JSON")"
KERNEL="$(jq -r '.kernel // empty' "$META_JSON")"
HOME_PATH="$(jq -r '.home_path // empty' "$META_JSON")"
VAR_PATH="$(jq -r '.var_path // empty' "$META_JSON")"
TAP_DEV="$(jq -r '.tap // empty' "$META_JSON")"
API_SOCK="$(jq -r '.api_sock // empty' "$META_JSON")"
LOG_FILE="$(jq -r '.log // empty' "$META_JSON")"
GUEST_IP="$(jq -r '.guest_ip // empty' "$META_JSON")"
DM_NAME="$(jq -r '.dm_name // empty' "$META_JSON")"
COW_FILE="$(jq -r '.cow_file // empty' "$META_JSON")"
VM_ID="$(jq -r '.meta.id // empty' "$VM_JSON")"
VM_NAME="$(jq -r '.meta.name // empty' "$VM_JSON")"
PID="$(jq -r '.meta.pid // empty' "$VM_JSON")"
ROOTFS="$(jq -r '.meta.rootfs // empty' "$VM_JSON")"
KERNEL="$(jq -r '.meta.kernel // empty' "$VM_JSON")"
HOME_PATH="$(jq -r '.meta.home_path // empty' "$VM_JSON")"
VAR_PATH="$(jq -r '.meta.var_path // empty' "$VM_JSON")"
TAP_DEV="$(jq -r '.meta.tap // empty' "$VM_JSON")"
API_SOCK="$(jq -r '.meta.api_sock // empty' "$VM_JSON")"
LOG_FILE="$(jq -r '.meta.log // empty' "$VM_JSON")"
GUEST_IP="$(jq -r '.meta.guest_ip // empty' "$VM_JSON")"
DM_NAME="$(jq -r '.meta.dm_name // empty' "$VM_JSON")"
DM_DEV_OLD="$(jq -r '.meta.dm_dev // empty' "$VM_JSON")"
BASE_LOOP_OLD="$(jq -r '.meta.base_loop // empty' "$VM_JSON")"
COW_FILE="$(jq -r '.meta.cow_file // empty' "$VM_JSON")"
COW_LOOP_OLD="$(jq -r '.meta.cow_loop // empty' "$VM_JSON")"
INITRD_PATH="$(jq -r '.config["boot-source"].initrd_path // empty' "$VM_JSON")"
DNS_NAME="$(banger_dns_name "$VM_NAME")"
if [[ -z "$ROOTFS" || -z "$KERNEL" || -z "$HOME_PATH" || -z "$VAR_PATH" || -z "$API_SOCK" || -z "$TAP_DEV" || -z "$GUEST_IP" || -z "$DM_NAME" || -z "$COW_FILE" ]]; then
log "meta.json missing required fields"
log "vm.json missing required fields"
exit 1
fi
if [[ ! -f "$ROOTFS" || ! -f "$KERNEL" || ! -f "$HOME_PATH" || ! -f "$VAR_PATH" || ! -f "$COW_FILE" || ! -f "$FC_BIN" ]]; then
log "missing disk/kernel file(s)"
exit 1
fi
if [[ ! -f "$VM_JSON" ]]; then
log "vm.json missing: $VM_JSON"
if banger_vm_process_running "$PID" "$API_SOCK"; then
log "vm is already running: $VM_NAME"
exit 1
fi
@ -99,11 +113,16 @@ DNS_SERVER="1.1.1.1"
VM_STARTED=0
cleanup() {
local dm_dev_cleanup="${DM_DEV:-$DM_DEV_OLD}"
local cow_loop_cleanup="${COW_LOOP:-$COW_LOOP_OLD}"
local base_loop_cleanup="${BASE_LOOP:-$BASE_LOOP_OLD}"
if [[ "$VM_STARTED" -eq 1 ]]; then
return
fi
sudo ip link del "$TAP_DEV" 2>/dev/null || true
rm -f "$API_SOCK"
banger_teardown_vm_runtime "$TAP_DEV" "$API_SOCK" "$DM_NAME" "$dm_dev_cleanup" "$cow_loop_cleanup" "$base_loop_cleanup"
banger_mark_vm_stopped "$VM_JSON"
banger_dns_remove_record_name "${DNS_NAME:-}"
}
trap cleanup EXIT
@ -121,8 +140,10 @@ sock_dir="$(dirname "$API_SOCK")"
sudo mkdir -p "$sock_dir"
sudo chown "$(id -u):$(id -g)" "$sock_dir"
banger_teardown_vm_runtime "$TAP_DEV" "$API_SOCK" "$DM_NAME" "$DM_DEV_OLD" "$COW_LOOP_OLD" "$BASE_LOOP_OLD"
banger_mark_vm_stopped "$VM_JSON"
# Recreate dm-snapshot
sudo dmsetup remove "$DM_NAME" 2>/dev/null || true
BASE_LOOP="$(sudo losetup -f --show --read-only "$ROOTFS")"
COW_LOOP="$(sudo losetup -f --show "$COW_FILE")"
SECTORS="$(sudo blockdev --getsz "$BASE_LOOP")"
@ -133,16 +154,16 @@ jq \
--arg base_loop "$BASE_LOOP" \
--arg cow_loop "$COW_LOOP" \
--arg dm_dev "$DM_DEV" \
'.base_loop=$base_loop | .cow_loop=$cow_loop | .dm_dev=$dm_dev' \
"$META_JSON" > "$META_JSON.tmp" && mv "$META_JSON.tmp" "$META_JSON"
'.meta.base_loop=$base_loop | .meta.cow_loop=$cow_loop | .meta.dm_dev=$dm_dev' \
"$VM_JSON" > "$VM_JSON.tmp" && mv "$VM_JSON.tmp" "$VM_JSON"
# Update /etc/resolv.conf and hostname in snapshot
RESOLV_TMP="$VM_DIR/resolv.conf"
HOSTNAME_TMP="$VM_DIR/hostname"
HOSTS_TMP="$VM_DIR/hosts"
printf 'nameserver %s\n' "$DNS_SERVER" >"$RESOLV_TMP"
printf '%s\n' "$(jq -r '.name // empty' "$META_JSON")" >"$HOSTNAME_TMP"
printf '127.0.0.1 localhost\n127.0.1.1 %s\n' "$(jq -r '.name // empty' "$META_JSON")" >"$HOSTS_TMP"
printf '%s\n' "$VM_NAME" >"$HOSTNAME_TMP"
printf '127.0.0.1 localhost\n127.0.1.1 %s\n' "$VM_NAME" >"$HOSTS_TMP"
sudo e2rm "$DM_DEV:/etc/resolv.conf" >/dev/null 2>&1 || true
sudo e2rm "$DM_DEV:/etc/hostname" >/dev/null 2>&1 || true
sudo e2rm "$DM_DEV:/etc/hosts" >/dev/null 2>&1 || true
@ -171,22 +192,31 @@ for _ in $(seq 1 200); do
done
[[ -S "$API_SOCK" ]] || { log "firecracker api socket not ready"; exit 1; }
SUDO_CHILD_PID="$(pgrep -n -f "$API_SOCK" || true)"
if [[ -n "$SUDO_CHILD_PID" ]]; then
FC_PID="$SUDO_CHILD_PID"
fi
log "configuring machine"
/usr/bin/sudo /usr/bin/curl --unix-socket "$API_SOCK" -X PUT http://localhost/machine-config \
-H "Content-Type: application/json" \
-d "$(jq -c '."machine-config"' "$VM_JSON")" >/dev/null
-d "$(jq -c '.config["machine-config"]' "$VM_JSON")" >/dev/null
boot_args="$(jq -r '."boot-source".boot_args' "$VM_JSON")"
boot_args="$(jq -r '.config["boot-source"].boot_args // empty' "$VM_JSON")"
boot_args="$(printf '%s' "$boot_args" | sed -E 's/(^| )hostname=[^ ]+//g; s/(^| )ip=[^ ]+//g' | awk '{$1=$1; print}')"
boot_args="$boot_args ip=${GUEST_IP}::${BR_IP}:255.255.255.0::eth0:off:${DNS_SERVER}"
boot_args="$boot_args hostname=$(jq -r '.name // empty' "$META_JSON")"
boot_args="$boot_args hostname=$VM_NAME"
INITRD_JSON=""
if [[ -n "$INITRD_PATH" ]]; then
INITRD_JSON=", \"initrd_path\": \"$INITRD_PATH\""
fi
log "configuring boot source"
/usr/bin/sudo /usr/bin/curl --unix-socket "$API_SOCK" -X PUT http://localhost/boot-source \
-H "Content-Type: application/json" \
-d "{
\"kernel_image_path\": \"$KERNEL\",
\"boot_args\": \"$boot_args\"
\"boot_args\": \"$boot_args\"${INITRD_JSON}
}" >/dev/null
log "attaching drives"
@ -229,16 +259,21 @@ log "starting virtual machine"
/usr/bin/sudo /usr/bin/curl --unix-socket "$API_SOCK" -X PUT http://localhost/actions \
-H "Content-Type: application/json" \
-d '{ "action_type": "InstanceStart" }' >/dev/null
VM_STARTED=1
CREATED_AT="$(date -Iseconds)"
banger_dns_write_record "$VM_NAME" "$GUEST_IP"
jq \
--arg pid "$FC_PID" \
--arg created_at "$CREATED_AT" \
'.pid=$pid | .created_at=$created_at' \
"$META_JSON" > "$META_JSON.tmp" && mv "$META_JSON.tmp" "$META_JSON"
--arg dns_name "$DNS_NAME" \
'.meta.pid=$pid | .meta.created_at=$created_at | .meta.dns_name=$dns_name | del(.meta.dns_file)' \
"$VM_JSON" > "$VM_JSON.tmp" && mv "$VM_JSON.tmp" "$VM_JSON"
VM_CONFIG_JSON="$(/usr/bin/sudo /usr/bin/curl --unix-socket "$API_SOCK" -sS http://localhost/vm/config)"
jq '.' <<<"$VM_CONFIG_JSON" > "$VM_JSON"
jq \
--argjson config "$VM_CONFIG_JSON" \
'.config=$config' \
"$VM_JSON" > "$VM_JSON.tmp" && mv "$VM_JSON.tmp" "$VM_JSON"
VM_STARTED=1
log "restored"