proxmox: fix the status table and the template lifecycle

Found on the first real deployment:

- status used the qemu implementation (pidfile + VNC display) and died with
  "qemu_pidfile: command not found". The Proxmox override shows VMID, state
  and role, plus the Kali seats once they exist.
- snapshot templated the working VM in place (id 1100) while reset cloned
  from base+100 (1200), so reset always failed and up could not start a
  template. snapshot now clones the VM into 1200 and templates the clone;
  reset and launch clone 1200 back into 1100.
- working clones are full copies: snapshot --update destroys and rebuilds
  the template, which would orphan a linked clone.
- vnc prints the noVNC URL instead of a localhost display that does not
  exist on Proxmox.

Checked against a stubbed qm: status table, snapshot, reset and
launch-without-a-VM all behave.
This commit is contained in:
2026-10-26 12:29:47 -04:00
parent 30d806c06c
commit 4dc27a4208
2 changed files with 69 additions and 8 deletions
+1 -1
View File
@@ -169,7 +169,7 @@ de continuer si elle ne correspond pas à la machine.
./lab.sh users alice bob carol # comptes Proxmox + URL de console ./lab.sh users alice bob carol # comptes Proxmox + URL de console
``` ```
`reset` détruit la VM et la reclone depuis le gabarit : quelques secondes, pas de réinstallation. `reset` détruit la VM et la reclone depuis le gabarit : une copie de disque (quelques dizaines de secondes), pas de réinstallation.
`reset` ne touche pas aux Kali : `./lab.sh kali reset` les recrée séparément. `reset` ne touche pas aux Kali : `./lab.sh kali reset` les recrée séparément.
+68 -7
View File
@@ -181,6 +181,19 @@ backend_launch() {
fi fi
pve_running "$vmid" && { info " $name already running"; return 0; } pve_running "$vmid" && { info " $name already running"; return 0; }
if pve_is_template "$vmid"; then
fail "$name is a template, not a working VM (./lab.sh destroy && ./lab.sh build)"
return 1
fi
if ! pve_exists "$vmid"; then
local tpl
tpl=$(pve_template_vmid "$name")
pve_exists "$tpl" || { fail "$name has no VM and no template (run ./lab.sh build)"; return 1; }
qm clone "$tpl" "$vmid" --name "norbourg-$name" --full 1 >/dev/null \
|| { fail "qm clone failed for $name"; return 1; }
ok "$(printf '%-6s cloned from template %s' "$name" "$tpl")"
fi
qm start "$vmid" >/dev/null || { fail "qm start failed for $name"; return 1; } qm start "$vmid" >/dev/null || { fail "qm start failed for $name"; return 1; }
ok "$(printf '%-6s started (vm %s)' "$name" "$vmid")" ok "$(printf '%-6s started (vm %s)' "$name" "$vmid")"
} }
@@ -206,17 +219,22 @@ backend_snapshot() {
tpl=$(pve_template_vmid "$name") tpl=$(pve_template_vmid "$name")
pve_running "$vmid" && { fail "$name is still running"; return 1; } pve_running "$vmid" && { fail "$name is still running"; return 1; }
if pve_exists "$tpl"; then
ok "$(printf '%-6s template %s already exists' "$name" "$tpl")"
return 0
fi
pve_exists "$vmid" || { fail "$name has no VM to freeze"; return 1; } pve_exists "$vmid" || { fail "$name has no VM to freeze"; return 1; }
qm template "$vmid" >/dev/null || { fail "qm template failed for $name"; return 1; } # The working VM keeps its id; the baseline is an independent full clone
# converted to a template, so reset can always recreate the working VM.
if pve_exists "$tpl"; then
qm destroy "$tpl" --purge 1 >/dev/null 2>&1 || true
fi
qm clone "$vmid" "$tpl" --name "norbourg-${name}-tpl" --full 1 >/dev/null \
|| { fail "qm clone failed for $name"; return 1; }
qm template "$tpl" >/dev/null || { fail "qm template failed for $name"; return 1; }
ok "$(printf '%-6s frozen as template %s' "$name" "$tpl")" ok "$(printf '%-6s frozen as template %s' "$name" "$tpl")"
} }
# `save` commits the current state into the baseline; same operation here.
backend_commit() { backend_snapshot "$@"; }
backend_has_baseline() { pve_exists "$(pve_template_vmid "$1")"; } backend_has_baseline() { pve_exists "$(pve_template_vmid "$1")"; }
backend_reset() { backend_reset() {
@@ -228,7 +246,7 @@ backend_reset() {
pve_exists "$tpl" || { fail "$name has no template — run ./lab.sh snapshot first"; return 1; } pve_exists "$tpl" || { fail "$name has no template — run ./lab.sh snapshot first"; return 1; }
pve_exists "$vmid" && qm destroy "$vmid" --purge 1 >/dev/null 2>&1 pve_exists "$vmid" && qm destroy "$vmid" --purge 1 >/dev/null 2>&1
qm clone "$tpl" "$vmid" --name "norbourg-$name" >/dev/null \ qm clone "$tpl" "$vmid" --name "norbourg-$name" --full 1 >/dev/null \
|| { fail "qm clone failed for $name"; return 1; } || { fail "qm clone failed for $name"; return 1; }
ok "$(printf '%-6s reverted from template %s' "$name" "$tpl")" ok "$(printf '%-6s reverted from template %s' "$name" "$tpl")"
} }
@@ -735,3 +753,46 @@ cmd_users() {
printf '\n' printf '\n'
info "Guest credentials inside Kali: kali / kali" info "Guest credentials inside Kali: kali / kali"
} }
# ------------------------------------------------------------------ status
# The shared table reads a qemu pidfile and a VNC display; neither exists
# here, so show the Proxmox view instead.
cmd_status() {
local i name ip vmid state role n
printf '\n%s%-7s %-14s %-7s %-9s %s%s\n' "$C_BOLD" VM IP VMID STATE ROLE "$C_RESET"
for i in "${!VM_NAMES[@]}"; do
name=${VM_NAMES[$i]}; ip=${VM_IPS[$i]}; role=${VM_ROLES[$i]}
vmid=$(pve_vmid "$name")
if ! pve_exists "$vmid"; then state="absent"
elif pve_running "$vmid"; then state="running"
else state="stopped"; fi
printf '%-7s %-14s %-7s %-9s %s\n' "$name" "$ip" "$vmid" "$state" "$role"
done
printf '\n'
for i in "${!VM_NAMES[@]}"; do
name=${VM_NAMES[$i]}; ip=${VM_IPS[$i]}
pve_running "$(pve_vmid "$name")" || continue
if tcp_open "$ip" 5985; then state="yes"; else state="no"; fi
printf ' %-6s winrm: %s\n' "$name" "$state"
done
for n in $(seq 1 "${KALI_COUNT:-0}"); do
vmid=$(kali_vmid "$n")
pve_exists "$vmid" || continue
if pve_running "$vmid"; then state="running"; else state="stopped"; fi
printf ' %-10s vm %-6s %-12s %s\n' "kali-$(printf '%02d' "$n")" "$vmid" "$(kali_ip "$n")" "$state"
done
printf '\n'
}
cmd_vnc() {
local name=${1:-} vmid node host
[[ -n $name ]] || die "usage: ./lab.sh vnc <vm> (VMs: ${VM_NAMES[*]})"
vmid=$(pve_vmid "$name") || die "unknown vm: $name"
node=$(pve_node)
host=${PVE_WEB_HOST:-$(hostname -f 2>/dev/null || hostname)}
info "console: https://${host}:8006/?console=kvm&novnc=1&vmid=${vmid}&node=${node}&resize=scale"
info "or from this host: qm terminal ${vmid} (Linux guests only)"
}