Phase 3: unattended install, seed ISOs and VM lifecycle

autounattend template (q35/AHCI/e1000, no virtio injection), per-VM seed
ISOs with a bootstrap that configures the static IP, enables WinRM/RDP and
chains into the role script. build --dry-run, up/down/status/vnc verbs.

Static addressing happens in PowerShell rather than in the unattend file:
Microsoft-Windows-TCPIP matches adapters by connection name and fails
silently when the name differs.
This commit is contained in:
2026-10-21 18:46:31 -04:00
parent 5a8f988f30
commit c66b5641b6
11 changed files with 719 additions and 24 deletions
+149
View File
@@ -0,0 +1,149 @@
# 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"
}
cmd_up() {
local name
step "start"
for name in "${VM_NAMES[@]}"; do
if backend_running "$name"; then
info " $name already running"
else
backend_launch "$name" run || fail "$name did not start"
fi
done
}
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 )))"
}
+51
View File
@@ -0,0 +1,51 @@
# shellcheck shell=bash
# cmd_isos — fetch the Windows media into LAB_ISO_CACHE.
# Read-only content, so an NTFS/FUSE cache directory is fine here.
# name|url|minimum size in MB
ISO_ITEMS=(
"SERVER_EVAL_x64FRE_en-us.iso|https://go.microsoft.com/fwlink/?linkid=2195280|2000"
"virtio-win.iso|https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso|200"
)
cmd_isos() {
have_cmd curl || die "curl is required"
mkdir -p "$LAB_ISO_CACHE" || die "cannot create $LAB_ISO_CACHE"
local item name url min_mb target tmp size_mb
for item in "${ISO_ITEMS[@]}"; do
IFS='|' read -r name url min_mb <<< "$item"
target="$LAB_ISO_CACHE/$name"
tmp="$target.part"
if [[ -f $target ]]; then
size_mb=$(( $(stat -c %s "$target") / 1024 / 1024 ))
if (( size_mb >= min_mb )); then
ok "$name present (${size_mb} MB)"
continue
fi
warn "$name is only ${size_mb} MB, re-downloading"
rm -f "$target"
fi
step "download $name"
info " from $url"
local curl_opts=(-L --fail --retry 3 --retry-delay 5 -C - -o "$tmp")
if [[ -t 1 ]]; then
curl "${curl_opts[@]}" --progress-bar "$url" || { rm -f "$tmp"; die "download failed: $name"; }
else
curl "${curl_opts[@]}" -sS "$url" || { rm -f "$tmp"; die "download failed: $name"; }
fi
mv -f "$tmp" "$target"
size_mb=$(( $(stat -c %s "$target") / 1024 / 1024 ))
if (( size_mb < min_mb )); then
die "$name looks truncated: ${size_mb} MB (expected >= ${min_mb} MB)"
fi
ok "$name ready (${size_mb} MB)"
done
printf '\n'
ok "ISO cache complete: ${LAB_ISO_CACHE}"
}
+66
View File
@@ -0,0 +1,66 @@
# shellcheck shell=bash
# cmd_seeds — render one unattend + payload per VM and pack a seed ISO.
# Nothing here needs root or a downloaded ISO; it is pure file assembly.
cmd_seeds() {
local tmpl="$LAB_DIR/seed/Autounattend.xml.tmpl"
local payload="$LAB_DIR/seed/payload"
[[ -r $tmpl ]] || die "missing template: $tmpl"
[[ -d $payload ]] || die "missing payload dir: $payload"
have_cmd genisoimage || have_cmd xorriso || die "genisoimage or xorriso is required"
: "${LAB_ADMIN_PASSWORD:?LAB_ADMIN_PASSWORD is not set in lab.conf}"
mkdir -p "$LAB_ROOT/seeds"
local i name ip role work iso volid dns
for i in "${!VM_NAMES[@]}"; do
name=${VM_NAMES[$i]}
ip=${VM_IPS[$i]}
role=${VM_ROLES[$i]}
dns=$LAB_DC_IP # every lab VM resolves through the DC
work="$LAB_ROOT/seeds/$name"
rm -rf "$work"
mkdir -p "$work/norbourg/roles"
sed -e "s|@@HOSTNAME@@|${name}|g" \
-e "s|@@ADMIN_PASSWORD@@|${LAB_ADMIN_PASSWORD}|g" \
-e "s|@@IMAGE_INDEX@@|${WINDOWS_IMAGE_INDEX:-2}|g" \
"$tmpl" > "$work/Autounattend.xml"
cp "$payload/bootstrap.cmd" "$work/norbourg/"
cp "$payload/bootstrap.ps1" "$work/norbourg/"
cat > "$work/norbourg/config.ps1" <<EOF
\$Config = @{
Hostname = '${name}'
IP = '${ip}'
Prefix = ${LAB_NETMASK}
Gateway = '${LAB_GATEWAY}'
DNS = '${dns}'
Role = '${role}'
Domain = '${DOMAIN}'
}
EOF
# A malformed answer file means a 20-minute silent install failure; catch it here.
python3 - "$work/Autounattend.xml" <<'PY' || die "rendered answer file is not valid XML for ${name}"
import sys, xml.etree.ElementTree as ET
ET.parse(sys.argv[1])
PY
iso="$LAB_ROOT/seeds/${name}-seed.iso"
volid="NRBG_${name^^}"
if have_cmd genisoimage; then
genisoimage -quiet -volid "$volid" -J -R -o "$iso" "$work"
else
xorriso -as mkisofs -quiet -V "$volid" -J -R -o "$iso" "$work"
fi
ok "$(printf '%-6s %-14s %-4s %s' "$name" "$ip" "$role" "$iso")"
done
printf '\n'
ok "${#VM_NAMES[@]} seed ISO(s) in ${LAB_ROOT}/seeds"
}
+17
View File
@@ -79,6 +79,23 @@ load_vms() {
(( ${#VM_NAMES[@]} > 0 )) || die "no VMs defined in VMS (lab.conf)"
}
vm_index() {
local name=$1 i
for i in "${!VM_NAMES[@]}"; do
if [[ ${VM_NAMES[$i]} == "$name" ]]; then
printf '%s\n' "$i"
return 0
fi
done
return 1
}
# Bash-native TCP probe: no nc dependency, 1 s timeout.
tcp_open() {
local host=$1 port=$2
timeout 1 bash -c "exec 3<>/dev/tcp/${host}/${port}" 2>/dev/null
}
sum_vm_ram_mb() {
local total=0 r
for r in "${VM_RAM[@]}"; do total=$(( total + r )); done