Files
pentest.intro/content/lib/cmd_build.sh
T
quoije 890dd58700 Split into qemu and proxmox platforms over a shared content tree
platforms/qemu and platforms/proxmox each have their own lab.sh and
lab.conf; content/ holds everything shared (roles, seed, site, helpers).
The dispatcher, preflight and command implementations are common and take
platform differences from the backend.

Proxmox backend: qm create with q35/SATA/e1000, ISOs and seed ISOs in an
ISO storage, provisioning over qm guest exec (no Python packages needed on
the host), template + clone for reset, and peers/ to emit WireGuard configs.
The role scripts now ride on the seed ISO, so provisioning transfers nothing.

build-bundle.sh produces a self-contained tarball per platform, for hosts
that cannot reach the git server.

Both configs are generic: no absolute path from this machine remains.
2026-10-22 19:23:16 -04:00

167 lines
4.2 KiB
Bash

# shellcheck shell=bash
# cmd_build / cmd_up / cmd_down / cmd_status — VM lifecycle for the qemu backend.
cmd_build() {
local dry=0 arg
for arg in "$@"; do
case $arg in
--dry-run) dry=1 ;;
-h|--help) info "usage: ./lab.sh build [--dry-run]"; return 0 ;;
*) die "unknown argument: $arg" ;;
esac
done
step "answer files"
if (( dry )); then
info " + render ${#VM_NAMES[@]} unattend file(s) and pack seed ISOs"
else
cmd_seeds >/dev/null || die "seed build failed"
ok "${#VM_NAMES[@]} seed ISO(s) ready"
fi
if [[ ! -f $WINDOWS_ISO ]]; then
(( dry )) || die "missing Windows ISO: $WINDOWS_ISO (run ./lab.sh isos)"
fi
step "virtual disks"
local name
for name in "${VM_NAMES[@]}"; do
backend_create_disk "$name" "$dry"
done
step "launch"
for name in "${VM_NAMES[@]}"; do
if backend_running "$name"; then
info " $name already running"
continue
fi
backend_launch "$name" install "$dry" || die "could not start $name"
done
if (( dry )); then
printf '\n'
ok "dry run complete — nothing was created"
return 0
fi
_wait_for_boot
}
_wait_for_boot() {
local deadline=$(( SECONDS + ${BUILD_TIMEOUT:-2700} ))
local i name ip
local -A pending=()
for i in "${!VM_NAMES[@]}"; do
backend_running "${VM_NAMES[$i]}" && pending["${VM_NAMES[$i]}"]=${VM_IPS[$i]}
done
step "waiting for unattended installs"
info " poll every 15 s, up to $(( ${BUILD_TIMEOUT:-2700} / 60 )) min"
info " watch one: ./lab.sh vnc <vm> · logs: ${LAB_ROOT}/run/"
local reported=1
while (( ${#pending[@]} > 0 )); do
if (( SECONDS > deadline )); then
fail "timed out waiting for: ${!pending[*]}"
return 1
fi
for name in "${!pending[@]}"; do
if tcp_open "${pending[$name]}" 5985; then
ok "$(printf '%-6s installed and reachable on 5985' "$name")"
unset 'pending[$name]'
fi
done
if (( ${#pending[@]} > 0 )); then
if (( SECONDS - reported >= 60 )); then
info " still waiting: ${!pending[*]}"
reported=$SECONDS
fi
sleep 15
fi
done
printf '\n'
ok "all VMs installed and reachable"
info " next: ./lab.sh status"
}
# Start the DC first and wait for it: a member that boots before the DC
# loses its machine-account channel and fails to authenticate (NO_LOGON_SERVERS).
_start_lab() {
local name
if vm_index dc01 >/dev/null 2>&1; then
if backend_running dc01; then
info " dc01 already running"
else
backend_launch dc01 run || { fail "dc01 did not start"; return 1; }
fi
_wait_winrm "$LAB_DC_IP" 240 || warn "dc01 did not answer WinRM in time"
fi
for name in "${VM_NAMES[@]}"; do
[[ $name == dc01 ]] && continue
if backend_running "$name"; then
info " $name already running"
else
backend_launch "$name" run || { fail "$name did not start"; return 1; }
fi
done
}
cmd_up() {
step "start"
_start_lab
}
cmd_down() {
local name
step "stop"
for name in "${VM_NAMES[@]}"; do
backend_stop "$name"
done
}
cmd_status() {
local i name ip pid state sync
printf '\n%s%-7s %-14s %-9s %-8s %s%s\n' "$C_BOLD" VM IP STATE PID VNC "$C_RESET"
for i in "${!VM_NAMES[@]}"; do
name=${VM_NAMES[$i]}
ip=${VM_IPS[$i]}
if backend_running "$name"; then
state="running"
pid=$(<"$(qemu_pidfile "$name")")
else
state="stopped"
pid="-"
fi
printf '%-7s %-14s %-9s %-8s %s\n' "$name" "$ip" "$state" "$pid" "$(( 5900 + i + 1 ))"
done
# Reachability is only meaningful once the guest has bootstrapped.
printf '\n'
for i in "${!VM_NAMES[@]}"; do
name=${VM_NAMES[$i]}
ip=${VM_IPS[$i]}
backend_running "$name" || continue
sync="no"
tcp_open "$ip" 5985 && sync="yes"
printf ' %-6s winrm: %s\n' "$name" "$sync"
done
}
cmd_vnc() {
local name=${1:-}
local i
[[ -n $name ]] || die "usage: ./lab.sh vnc <vm> (VMs: ${VM_NAMES[*]})"
i=$(vm_index "$name") || die "unknown vm: $name"
local port=$(( 5900 + i + 1 ))
if have_cmd remote-viewer; then
exec remote-viewer "vnc://127.0.0.1:${port}"
elif have_cmd vncviewer; then
exec vncviewer "127.0.0.1:${port}"
fi
info "no VNC client found. Connect to 127.0.0.1:${port} (display :$(( i + 1 )))"
}