diff --git a/session1/docs/instructeur.md b/session1/docs/instructeur.md index 1c778c3..5495369 100644 --- a/session1/docs/instructeur.md +++ b/session1/docs/instructeur.md @@ -169,7 +169,7 @@ de continuer si elle ne correspond pas à la machine. ./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. diff --git a/session1/platforms/proxmox/backends/proxmox.sh b/session1/platforms/proxmox/backends/proxmox.sh index de663f3..d77ecc4 100644 --- a/session1/platforms/proxmox/backends/proxmox.sh +++ b/session1/platforms/proxmox/backends/proxmox.sh @@ -181,6 +181,19 @@ backend_launch() { fi 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; } ok "$(printf '%-6s started (vm %s)' "$name" "$vmid")" } @@ -206,17 +219,22 @@ backend_snapshot() { tpl=$(pve_template_vmid "$name") 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; } - 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")" } +# `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_reset() { @@ -228,7 +246,7 @@ backend_reset() { 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 - 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; } ok "$(printf '%-6s reverted from template %s' "$name" "$tpl")" } @@ -735,3 +753,46 @@ cmd_users() { printf '\n' 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 (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)" +}