#!/usr/bin/env bash
#
# vm.sh — boot state/disk.raw headless, with ssh forwarded to $SSH_PORT.
#
# No qemu monitor socket. The obvious place for one is state/, and under a
# session scratch path that exceeds the 108-byte limit a unix socket path has,
# which fails with a message about socket paths that reads as unrelated to
# anything being done. ssh is the control channel here, so the monitor was
# never needed; vmtest keeps one because it drives a ratatui installer through
# screendumps and has nothing else to talk to.
set -euo pipefail

# shellcheck source=build/layertest/common.sh
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common.sh"

[ -f "$STATE/disk.raw" ] || die "no state/disk.raw; run install-disk.sh first"
[ -f "$STATE/OVMF_VARS.fd" ] || cp /usr/share/OVMF/OVMF_VARS_4M.fd "$STATE/OVMF_VARS.fd"

exec qemu-system-x86_64 \
  -enable-kvm -machine q35 -cpu host -m 4096 -smp 4 \
  -drive if=pflash,format=raw,unit=0,readonly=on,file=/usr/share/OVMF/OVMF_CODE_4M.fd \
  -drive "if=pflash,format=raw,unit=1,file=$STATE/OVMF_VARS.fd" \
  -drive "file=$STATE/disk.raw,if=virtio,format=raw" \
  -netdev "user,id=n0,hostfwd=tcp:127.0.0.1:$SSH_PORT-:22" \
  -device virtio-net-pci,netdev=n0 \
  -display none \
  -serial "file:$STATE/serial.log"
