- PVE has no win2k22 ostype; Server 2022 maps to win11 - drop --startup 0, which is not a valid order value - after install, repoint the boot order at sata0 and detach the installer ISO, so a stray keypress cannot re-enter Setup - ISO storage detection falls back to storage.cfg when pvesm has no --content flag - router wg0 is now 10.10.10.250/32 with the lab NIC at .251/24: two /24s on one host made routing ambiguous - nft rules use the router's own interface name (WG_LAB_IFACE), not vmbr10
312 lines
9.4 KiB
Bash
312 lines
9.4 KiB
Bash
# shellcheck shell=bash
|
|
# QEMU/KVM backend — local development and dry runs.
|
|
#
|
|
# Hardware model matches the Proxmox target on purpose: q35 + AHCI (SATA) and
|
|
# an e1000 NIC, so the unattended file never needs virtio drivers.
|
|
|
|
backend_check() {
|
|
if qemu-system-x86_64 -M help 2>/dev/null | grep -q '^q35'; then
|
|
_c_ok "qemu: q35 machine type available"
|
|
else
|
|
_c_fail "qemu: q35 machine type unavailable"
|
|
fi
|
|
|
|
if qemu-system-x86_64 -device help 2>/dev/null | grep -q 'name "e1000"'; then
|
|
_c_ok "qemu: e1000 NIC model available"
|
|
else
|
|
_c_warn "qemu: e1000 NIC model not listed"
|
|
fi
|
|
|
|
if qemu-system-x86_64 -device help 2>/dev/null | grep -q 'name "ich9-ahci"'; then
|
|
_c_ok "qemu: ich9-ahci controller available"
|
|
else
|
|
_c_warn "qemu: ich9-ahci controller not listed"
|
|
fi
|
|
|
|
# `-netdev help` omits bridge even when it is compiled in, so probe it instead:
|
|
# an unsupported type answers "Invalid netdev type", a supported one complains
|
|
# about the missing bridge device (br0).
|
|
if qemu-system-x86_64 -netdev bridge,id=probe 2>&1 | grep -qi 'invalid netdev'; then
|
|
_c_fail "qemu: bridge netdev not supported"
|
|
else
|
|
_c_ok "qemu: bridge netdev supported"
|
|
fi
|
|
|
|
if [[ -x /usr/libexec/qemu-bridge-helper ]]; then
|
|
_c_ok "qemu-bridge-helper present"
|
|
else
|
|
_c_warn "qemu-bridge-helper not found: unprivileged bridge attach will fail"
|
|
fi
|
|
}
|
|
|
|
# ------------------------------------------------------------------ layout
|
|
qemu_base() { printf '%s/images/base/%s-base.qcow2' "$LAB_ROOT" "$1"; }
|
|
qemu_overlay(){ printf '%s/images/overlays/%s.qcow2' "$LAB_ROOT" "$1"; }
|
|
|
|
# Boot the overlay when a baseline exists, the golden disk otherwise.
|
|
qemu_disk() {
|
|
local overlay
|
|
overlay=$(qemu_overlay "$1")
|
|
if [[ -f $overlay ]]; then printf '%s' "$overlay"; else printf '%s/images/%s.qcow2' "$LAB_ROOT" "$1"; fi
|
|
}
|
|
|
|
# The disk created by `build`, before it is frozen into a base image.
|
|
qemu_golden() { printf '%s/images/%s.qcow2' "$LAB_ROOT" "$1"; }
|
|
qemu_pidfile(){ printf '%s/run/%s.pid' "$LAB_ROOT" "$1"; }
|
|
qemu_log() { printf '%s/run/%s.log' "$LAB_ROOT" "$1"; }
|
|
qemu_monitor(){ printf '%s/run/%s.monitor' "$LAB_ROOT" "$1"; }
|
|
|
|
backend_running() {
|
|
local pidfile pid
|
|
pidfile=$(qemu_pidfile "$1")
|
|
[[ -r $pidfile ]] || return 1
|
|
pid=$(<"$pidfile")
|
|
[[ -n $pid ]] && kill -0 "$pid" 2>/dev/null
|
|
}
|
|
|
|
backend_create_disk() {
|
|
local name=$1 dry=${2:-0}
|
|
local disk i size
|
|
disk=$(qemu_golden "$name")
|
|
i=$(vm_index "$name") || die "unknown vm: $name"
|
|
size=${VM_DISK[$i]}
|
|
|
|
if [[ -f $disk ]]; then
|
|
ok "$(printf '%-6s disk present (%s)' "$name" "$(du -h "$disk" | cut -f1)")"
|
|
return
|
|
fi
|
|
mkdir -p "$LAB_ROOT/images"
|
|
if (( dry )); then
|
|
info " + qemu-img create -f qcow2 $disk ${size}G"
|
|
return
|
|
fi
|
|
qemu-img create -q -f qcow2 "$disk" "${size}G"
|
|
ok "$(printf '%-6s disk created (%sG)' "$name" "$size")"
|
|
}
|
|
|
|
backend_launch() {
|
|
local name=$1 mode=${2:-run} dry=${3:-0}
|
|
local i ip ram vcpu disk seed win vnc
|
|
local boot_iso="" args=() cd=1
|
|
|
|
i=$(vm_index "$name") || die "unknown vm: $name"
|
|
ip=${VM_IPS[$i]}; ram=${VM_RAM[$i]}; vcpu=${VM_VCPU[$i]}
|
|
disk=$(qemu_disk "$name")
|
|
seed="$LAB_ROOT/seeds/$name-seed.iso"
|
|
win=$WINDOWS_ISO
|
|
vnc=$(( i + 1 ))
|
|
|
|
[[ -f $seed ]] || (( dry )) || die "missing seed ISO for $name (run ./lab.sh seeds)"
|
|
[[ -f $disk ]] || (( dry )) || die "missing disk for $name (run ./lab.sh build)"
|
|
|
|
if [[ $mode == install ]] && ! (( dry )); then
|
|
[[ -f $win ]] || die "missing Windows ISO: $win (run ./lab.sh isos)"
|
|
fi
|
|
|
|
args=(
|
|
-name "norbourg-$name"
|
|
-machine q35,accel=kvm
|
|
-cpu host
|
|
-smp "${vcpu},sockets=1,cores=${vcpu}"
|
|
-m "$ram"
|
|
-rtc base=localtime,clock=host
|
|
-drive "if=none,id=disk,file=$disk,format=qcow2,cache=none,aio=native,discard=unmap"
|
|
-device ide-hd,drive=disk,bus=ide.0
|
|
-netdev "bridge,id=net0,br=$BRIDGE"
|
|
-device "e1000,netdev=net0,mac=$(qemu_mac_for "$ip")"
|
|
-monitor "unix:$(qemu_monitor "$name"),server,nowait"
|
|
-display none
|
|
-vnc "127.0.0.1:${vnc}"
|
|
)
|
|
|
|
if [[ $mode == install ]]; then
|
|
(( dry )) || [[ -f $win ]] || die "missing Windows ISO: $win (run ./lab.sh isos)"
|
|
args+=( -drive "if=none,id=cd-install,media=cdrom,file=$win"
|
|
-device "ide-cd,drive=cd-install,bus=ide.${cd}" )
|
|
cd=$(( cd + 1 ))
|
|
fi
|
|
args+=( -drive "if=none,id=cd-seed,media=cdrom,file=$seed"
|
|
-device "ide-cd,drive=cd-seed,bus=ide.${cd}" )
|
|
|
|
if [[ $mode == install ]]; then
|
|
# once=d: boot the install media this time only, then always from disk.
|
|
# Without `once`, Setup would re-enter itself after its own reboot.
|
|
args+=( -boot order=c,once=d )
|
|
else
|
|
args+=( -boot order=c )
|
|
fi
|
|
|
|
if (( dry )); then
|
|
info " + qemu-system-x86_64 ${args[*]}"
|
|
return
|
|
fi
|
|
|
|
mkdir -p "$LAB_ROOT/run"
|
|
nohup qemu-system-x86_64 "${args[@]}" >>"$(qemu_log "$name")" 2>&1 &
|
|
printf '%s\n' "$!" >"$(qemu_pidfile "$name")"
|
|
sleep 1
|
|
|
|
if backend_running "$name"; then
|
|
ok "$(printf '%-6s started · vnc 127.0.0.1:%d · pid %s' "$name" "$(( 5900 + vnc ))" "$(<"$(qemu_pidfile "$name")")")"
|
|
else
|
|
fail "$name failed to start — see $(qemu_log "$name")"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
backend_stop() {
|
|
local name=$1 sock pid ip i
|
|
|
|
backend_running "$name" || { info " $name already stopped"; return 0; }
|
|
|
|
sock=$(qemu_monitor "$name")
|
|
pid=$(<"$(qemu_pidfile "$name")")
|
|
ip=${VM_IPS[$(vm_index "$name")]}
|
|
|
|
# ACPI powerdown is best-effort; Server 2022 often ignores it. Asking the
|
|
# guest to shut itself down over WinRM is what actually works.
|
|
if backend_ps "$name" "shutdown.exe /s /t 3 /f" >/dev/null 2>&1; then
|
|
detail "shutdown requested over WinRM"
|
|
elif [[ -S $sock ]]; then
|
|
printf 'system_powerdown\n' | socat - "UNIX-CONNECT:$sock" >/dev/null 2>&1 || true
|
|
fi
|
|
|
|
for i in $(seq 1 90); do
|
|
kill -0 "$pid" 2>/dev/null || break
|
|
sleep 1
|
|
done
|
|
|
|
if kill -0 "$pid" 2>/dev/null; then
|
|
warn "$name did not shut down cleanly, sending TERM"
|
|
kill "$pid" 2>/dev/null || true
|
|
sleep 3
|
|
kill -9 "$pid" 2>/dev/null || true
|
|
fi
|
|
rm -f "$(qemu_pidfile "$name")"
|
|
ok "$name stopped"
|
|
}
|
|
|
|
# ------------------------------------------------------------------ baseline
|
|
backend_snapshot() {
|
|
local name=$1
|
|
local golden base overlay
|
|
golden=$(qemu_golden "$name")
|
|
base=$(qemu_base "$name")
|
|
overlay=$(qemu_overlay "$name")
|
|
|
|
if backend_running "$name"; then
|
|
fail "$name is still running"
|
|
return 1
|
|
fi
|
|
if [[ -f $base ]]; then
|
|
ok "$(printf '%-6s baseline already frozen' "$name")"
|
|
return 0
|
|
fi
|
|
if [[ ! -f $golden ]]; then
|
|
fail "$name has no disk to freeze"
|
|
return 1
|
|
fi
|
|
|
|
mkdir -p "$(dirname "$base")" "$(dirname "$overlay")"
|
|
mv "$golden" "$base"
|
|
# Relative backing path: the tree can move without breaking the chain.
|
|
qemu-img create -q -f qcow2 -F qcow2 -b "../base/${name}-base.qcow2" "$overlay"
|
|
ok "$(printf '%-6s baseline frozen (%s)' "$name" "$(du -h "$base" | cut -f1)")"
|
|
}
|
|
|
|
backend_reset() {
|
|
local name=$1
|
|
local base overlay
|
|
base=$(qemu_base "$name")
|
|
overlay=$(qemu_overlay "$name")
|
|
|
|
if backend_running "$name"; then
|
|
backend_stop "$name" >/dev/null 2>&1 || true
|
|
fi
|
|
if [[ ! -f $base ]]; then
|
|
fail "$name has no baseline — run ./lab.sh snapshot first"
|
|
return 1
|
|
fi
|
|
|
|
rm -f "$overlay"
|
|
qemu-img create -q -f qcow2 -F qcow2 -b "../base/${name}-base.qcow2" "$overlay"
|
|
ok "$(printf '%-6s reverted' "$name")"
|
|
}
|
|
|
|
# Fold the current overlay into the base, then start a fresh overlay.
|
|
# Used after a deliberate change to the lab, so `reset` keeps the change.
|
|
backend_commit() {
|
|
local name=$1
|
|
local base overlay
|
|
base=$(qemu_base "$name")
|
|
overlay=$(qemu_overlay "$name")
|
|
|
|
if backend_running "$name"; then
|
|
fail "$name is still running"
|
|
return 1
|
|
fi
|
|
if [[ ! -f $base || ! -f $overlay ]]; then
|
|
fail "$name has no base/overlay pair"
|
|
return 1
|
|
fi
|
|
|
|
qemu-img commit -q "$overlay" || { fail "commit failed for $name"; return 1; }
|
|
rm -f "$overlay"
|
|
qemu-img create -q -f qcow2 -F qcow2 -b "../base/${name}-base.qcow2" "$overlay"
|
|
ok "$(printf '%-6s overlay committed into the baseline' "$name")"
|
|
}
|
|
|
|
# ------------------------------------------------------------------ transport
|
|
# WinRM over the lab network. The guest scripts are already on disk, so this
|
|
# layer only runs commands and waits for markers.
|
|
_labwinrm() {
|
|
local name=$1; shift
|
|
local ip=${VM_IPS[$(vm_index "$name")]}
|
|
LAB_PASSWORD=$LAB_ADMIN_PASSWORD python3 "$CONTENT_DIR/scripts/labwinrm.py" "$ip" "$@" \
|
|
--user "$LAB_USER" --password "$LAB_ADMIN_PASSWORD"
|
|
}
|
|
|
|
backend_ps() {
|
|
local name=$1 script=$2
|
|
_labwinrm "$name" ps "$script"
|
|
}
|
|
|
|
backend_ready() {
|
|
local name=$1 timeout=${2:-300}
|
|
local deadline=$(( SECONDS + timeout ))
|
|
while (( SECONDS < deadline )); do
|
|
_labwinrm "$name" ps 'exit 0' >/dev/null 2>&1 && return 0
|
|
sleep 10
|
|
done
|
|
return 1
|
|
}
|
|
|
|
backend_start() {
|
|
local name=$1 remote=$2
|
|
_labwinrm "$name" start "$remote"
|
|
}
|
|
|
|
backend_wait() {
|
|
local name=$1 remote=$2 timeout=${3:-1800}
|
|
_labwinrm "$name" wait "$remote" "$timeout"
|
|
}
|
|
|
|
backend_destroy() {
|
|
local name=$1
|
|
local golden base overlay disk
|
|
golden=$(qemu_golden "$name")
|
|
base=$(qemu_base "$name")
|
|
overlay=$(qemu_overlay "$name")
|
|
|
|
backend_running "$name" && backend_stop "$name" >/dev/null 2>&1 || true
|
|
for disk in "$overlay" "$base" "$golden"; do
|
|
[[ -f $disk ]] && rm -f "$disk"
|
|
done
|
|
rm -f "$(qemu_pidfile "$name")" "$(qemu_monitor "$name")" "$(qemu_log "$name")"
|
|
ok "$name removed"
|
|
}
|
|
|
|
|
|
# Nothing to do: the boot order was set to "once=d" at launch time.
|
|
backend_finalize() { :; }
|