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:
@@ -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 )))"
|
||||
}
|
||||
Reference in New Issue
Block a user