# 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 · 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 backend_ready dc01 240 || warn "dc01 did not answer 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 (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 )))" }