Files
pentest.intro/content/lib/cmd_snapshot.sh
T
quoije b9bc0eb079 Add 'save': the closing step for any change
save = snapshot --update + start, and falls back to a first-time snapshot
when no baseline exists yet. On the qemu platform it folds the working
overlays into the base images and brings the lab back up; on proxmox it
re-templates.

Documented as the rule it is: every deliberate change to the lab ends with
save, so reset always returns to the state you meant to freeze.
2026-10-24 02:13:18 -04:00

81 lines
1.8 KiB
Bash

# shellcheck shell=bash
# cmd_snapshot / cmd_reset — freeze a clean baseline and revert to it.
# qemu backend: the golden disk becomes a read-only base, each run boots a
# thin overlay. Reset deletes the overlay, so it takes about a second.
cmd_snapshot() {
local name update=0
for name in "$@"; do
case $name in
--update) update=1 ;;
-h|--help) info "usage: ./lab.sh snapshot [--update]"; return 0 ;;
*) die "unknown argument: $name" ;;
esac
done
step "stop"
for name in "${VM_NAMES[@]}"; do
backend_stop "$name"
done
if (( update )); then
step "commit current state into the baseline"
for name in "${VM_NAMES[@]}"; do
backend_commit "$name" || die "commit failed for $name"
done
printf '\n'
ok "baseline updated"
info " ./lab.sh up to start · ./lab.sh reset to revert to this state"
return 0
fi
step "freeze baseline"
for name in "${VM_NAMES[@]}"; do
backend_snapshot "$name" || die "snapshot failed for $name"
done
printf '\n'
ok "baseline ready"
info " start with ./lab.sh up · revert with ./lab.sh reset"
}
cmd_reset() {
local name
step "revert to baseline"
for name in "${VM_NAMES[@]}"; do
backend_reset "$name" || die "reset failed for $name"
done
step "start"
_start_lab
printf '\n'
ok "laboratory reset — previous state discarded"
}
# The closing step of every change: freeze the current state into the
# baseline, then bring the lab back up. Falls back to a first-time snapshot
# when no baseline exists yet.
cmd_save() {
local name need_full=0
if declare -F backend_has_baseline >/dev/null; then
for name in "${VM_NAMES[@]}"; do
backend_has_baseline "$name" || need_full=1
done
else
need_full=1
fi
if (( need_full )); then
cmd_snapshot
else
cmd_snapshot --update
fi
step "start"
_start_lab
}