phoebus_os/installer/tests/vm/serial-driver.py
Phoebus 54701bd1ef test(vm): record static-binary install and detached boot evidence
The committed harness now installs with the static Rust binary and boots the
disk detached: systemd PID1, running, 0 failed units, LUKS2 unlock, login and
networking. Also fixes the boot check marker so expect() does not match the
shell's echoed command.
2026-09-20 03:54:53 -07:00

137 lines
4.9 KiB
Bash

#!/usr/bin/env python3
"""Headless serial driver for the disposable UEFI VM.
Two modes:
install : boot the live ISO kernel/initrd, run a guest script that installs
Phoebus non-interactively, then power off.
boot : boot the installed disk alone (no ISO, no -kernel), unlock LUKS,
log in and run verification commands.
Everything is parameterised through environment variables so the script is not
tied to one host. See tests/vm/README.md for the variables.
This script never touches a physical disk: DISK is always a qcow2 image.
"""
import os
import sys
import pexpect
QEMU = os.environ.get("QEMU_BIN", "qemu-system-x86_64")
LIBDIR = os.environ.get("QEMU_LIBDIR", "")
DATADIR = os.environ.get("QEMU_DATADIR", "")
OVMF_CODE = os.environ["OVMF_CODE"]
OVMF_VARS = os.environ["OVMF_VARS"]
DISK = os.environ["DISK"]
ISO = os.environ.get("ISO", "")
KERNEL = os.environ.get("KERNEL", "")
INITRD = os.environ.get("INITRD", "")
SERIAL_LOG = os.environ.get("SERIAL_LOG", "/tmp/phoebus-vm-serial.log")
LUKS_PW = os.environ.get("LUKS_PASSPHRASE", "phoebus-luks-test")
USER_PW = os.environ.get("USER_PASSWORD", "phoebus-user-test")
USER = os.environ.get("USERNAME", "phoebus")
MEM = os.environ.get("VM_MEM", "8192")
SMP = os.environ.get("VM_SMP", "8")
BLS_APPEND = (
"root=live:CDLABEL=gentoo-amd64-livegui rd.live.dir=/ "
"rd.live.squashimg=image.squashfs cdroot console=ttyS0,115200"
)
def base_args():
env = dict(os.environ)
if LIBDIR:
env["LD_LIBRARY_PATH"] = LIBDIR
args = ["-machine", "q35,accel=kvm"]
if DATADIR:
args += ["-L", DATADIR]
args += [
"-cpu", "host", "-smp", SMP, "-m", MEM,
"-drive", f"if=pflash,format=raw,readonly=on,file={OVMF_CODE}",
"-drive", f"if=pflash,format=raw,file={OVMF_VARS}",
"-drive", f"file={DISK},if=virtio,format=qcow2",
"-netdev", "user,id=n0", "-device", "virtio-net-pci,netdev=n0,romfile=",
"-device", "virtio-rng-pci", "-vga", "std", "-display", "none",
"-serial", "stdio", "-monitor", "none", "-no-reboot",
]
return env, args
def spawn(args, env):
child = pexpect.spawn(QEMU, args, env=env, timeout=600,
encoding="utf-8", codec_errors="replace",
maxread=65536)
child.delaybeforesend = 0.3
return child
def do_install():
env, args = base_args()
args += ["-drive", f"file={ISO},media=cdrom,readonly=on",
"-kernel", KERNEL, "-initrd", INITRD, "-append", BLS_APPEND]
log = open(SERIAL_LOG, "w")
child = spawn(args, env)
child.logfile = log
try:
child.expect(r"root@livecd[^\n]*# ", timeout=600)
url = os.environ.get("INSTALL_URL", "http://10.0.2.2:8000/install.sh")
child.sendline(f"curl -fsSL {url} -o /tmp/run.sh && bash /tmp/run.sh")
child.expect([r"===INSTALL-RC=([0-9]+)===", "===FETCH-FAIL===",
"Kernel panic", pexpect.EOF, pexpect.TIMEOUT], timeout=9000)
if "INSTALL-RC" in (child.after or ""):
print("INSTALL_RC", child.match.group(1), flush=True)
child.expect(["===POWEROFF===", pexpect.EOF, pexpect.TIMEOUT], timeout=600)
child.expect(pexpect.EOF, timeout=300)
finally:
child.close(force=True)
log.close()
def do_boot():
env, args = base_args()
args += ["-boot", "order=c"]
# The end marker is built with a command substitution so the shell's echo of
# the typed line does not itself contain it; otherwise `expect` would match
# the echo and close the console before the checks ran.
checks = os.environ.get(
"BOOT_CHECKS",
"echo ===CHECKS-$(printf START)===; "
"cat /proc/1/comm; systemctl is-system-running; "
"systemctl --failed --no-pager; ip -brief addr; "
"lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS; echo ===CHECKS-$(printf END)===",
)
log = open(SERIAL_LOG, "w")
child = spawn(args, env)
child.logfile = log
try:
child.expect([r"[Pp]assphrase", r"login:"], timeout=300)
if "assphrase" in (child.after or ""):
child.sendline(LUKS_PW)
for _ in range(5):
child.expect(r"phoebus login:", timeout=180)
child.sendline(USER)
idx = child.expect([r"[Pp]assword:", r"phoebus login:",
r"Login incorrect"], timeout=30)
if idx == 0:
child.sendline(USER_PW)
if child.expect([r"[$#] $", r"Login incorrect",
r"phoebus login:"], timeout=60) == 0:
break
child.sendline(checks)
child.expect("===CHECKS-END===", timeout=180)
print("checks complete", flush=True)
finally:
child.close(force=True)
log.close()
if __name__ == "__main__":
mode = sys.argv[1] if len(sys.argv) > 1 else ""
if mode == "install":
do_install()
elif mode == "boot":
do_boot()
else:
print(__doc__)
sys.exit(2)