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:
parent
a8078f2393
commit
9191b7e370
11 changed files with 966 additions and 144 deletions
45
customize.sh
45
customize.sh
|
|
@ -30,6 +30,7 @@ parse_size() {
|
|||
}
|
||||
|
||||
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
source "$DIR/dns.sh"
|
||||
STATE="$DIR/state"
|
||||
VM_ROOT="$STATE/vms"
|
||||
mkdir -p "$VM_ROOT"
|
||||
|
|
@ -161,6 +162,7 @@ mkdir -p "$VM_DIR"
|
|||
API_SOCK="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/banger/fc-$VM_TAG.sock"
|
||||
LOG_FILE="$VM_DIR/firecracker.log"
|
||||
TAP_DEV="tap-fc-$VM_TAG"
|
||||
DNS_NAME=""
|
||||
|
||||
# Allocate guest IP
|
||||
NEXT_IP_FILE="$STATE/next_ip"
|
||||
|
|
@ -174,6 +176,7 @@ cleanup() {
|
|||
sudo kill "${FC_PID:-}" 2>/dev/null || true
|
||||
sudo ip link del "$TAP_DEV" 2>/dev/null || true
|
||||
rm -f "$API_SOCK"
|
||||
banger_dns_remove_record_name "${DNS_NAME:-}"
|
||||
rm -rf "$VM_DIR"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
|
@ -211,23 +214,6 @@ for _ in $(seq 1 200); do
|
|||
done
|
||||
[[ -S "$API_SOCK" ]] || { log "firecracker api socket not ready"; exit 1; }
|
||||
|
||||
VM_CONFIG_JSON="$(sudo -E curl --unix-socket "$API_SOCK" -sS http://localhost/vm/config)"
|
||||
CREATED_AT="$(date -Iseconds)"
|
||||
jq -n \
|
||||
--arg id "$VM_ID" \
|
||||
--arg name "$VM_NAME" \
|
||||
--arg pid "$FC_PID" \
|
||||
--arg created_at "$CREATED_AT" \
|
||||
--arg guest_ip "$GUEST_IP" \
|
||||
--arg tap "$TAP_DEV" \
|
||||
--arg api_sock "$API_SOCK" \
|
||||
--arg log "$LOG_FILE" \
|
||||
--arg rootfs "$OUT_ROOTFS" \
|
||||
--arg kernel "$KERNEL" \
|
||||
--argjson config "$VM_CONFIG_JSON" \
|
||||
'{meta:{id:$id,name:$name,pid:$pid,created_at:$created_at,guest_ip:$guest_ip,tap:$tap,api_sock:$api_sock,log:$log,rootfs:$rootfs,kernel:$kernel},config:$config}' \
|
||||
> "$VM_DIR/vm.json"
|
||||
|
||||
log "configuring machine"
|
||||
sudo -E curl --unix-socket "$API_SOCK" -X PUT http://localhost/machine-config \
|
||||
-H "Content-Type: application/json" \
|
||||
|
|
@ -271,6 +257,31 @@ sudo -E curl --unix-socket "$API_SOCK" -X PUT http://localhost/actions \
|
|||
-H "Content-Type: application/json" \
|
||||
-d '{ "action_type": "InstanceStart" }' >/dev/null
|
||||
|
||||
SUDO_CHILD_PID="$(pgrep -n -f "$API_SOCK" || true)"
|
||||
if [[ -n "$SUDO_CHILD_PID" ]]; then
|
||||
FC_PID="$SUDO_CHILD_PID"
|
||||
fi
|
||||
|
||||
VM_CONFIG_JSON="$(sudo -E curl --unix-socket "$API_SOCK" -sS http://localhost/vm/config)"
|
||||
CREATED_AT="$(date -Iseconds)"
|
||||
DNS_NAME="$(banger_dns_name "$VM_NAME")"
|
||||
banger_dns_write_record "$VM_NAME" "$GUEST_IP"
|
||||
jq -n \
|
||||
--arg id "$VM_ID" \
|
||||
--arg name "$VM_NAME" \
|
||||
--arg pid "$FC_PID" \
|
||||
--arg created_at "$CREATED_AT" \
|
||||
--arg guest_ip "$GUEST_IP" \
|
||||
--arg tap "$TAP_DEV" \
|
||||
--arg api_sock "$API_SOCK" \
|
||||
--arg log "$LOG_FILE" \
|
||||
--arg rootfs "$OUT_ROOTFS" \
|
||||
--arg kernel "$KERNEL" \
|
||||
--arg dns_name "$DNS_NAME" \
|
||||
--argjson config "$VM_CONFIG_JSON" \
|
||||
'{meta:{id:$id,name:$name,pid:$pid,created_at:$created_at,guest_ip:$guest_ip,tap:$tap,api_sock:$api_sock,log:$log,rootfs:$rootfs,kernel:$kernel,dns_name:$dns_name},config:$config}' \
|
||||
> "$VM_DIR/vm.json"
|
||||
|
||||
log "enabling NAT for customization"
|
||||
sudo -E ./nat.sh up "$VM_TAG" >/dev/null
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue