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:
@@ -1,5 +1,8 @@
|
||||
# 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
|
||||
@@ -35,3 +38,141 @@ backend_check() {
|
||||
_c_warn "qemu-bridge-helper not found: unprivileged bridge attach will fail"
|
||||
fi
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------ layout
|
||||
qemu_disk() { 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"; }
|
||||
|
||||
# Deterministic MAC from the last two octets: 10.10.10.31 -> 52:54:00:0a:1f
|
||||
qemu_mac_for() {
|
||||
local ip=$1
|
||||
local b3=${ip##*.}
|
||||
local rest=${ip%.*}
|
||||
local b2=${rest##*.}
|
||||
printf '52:54:00:%02x:%02x' "$b2" "$b3"
|
||||
}
|
||||
|
||||
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_disk "$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 i
|
||||
backend_running "$name" || { info " $name already stopped"; return 0; }
|
||||
|
||||
sock=$(qemu_monitor "$name")
|
||||
pid=$(<"$(qemu_pidfile "$name")")
|
||||
|
||||
if [[ -S $sock ]]; then
|
||||
printf 'system_powerdown\n' | socat - "UNIX-CONNECT:$sock" >/dev/null 2>&1 || true
|
||||
fi
|
||||
|
||||
for i in $(seq 1 30); 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 2
|
||||
fi
|
||||
rm -f "$(qemu_pidfile "$name")"
|
||||
ok "$name stopped"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user