phoebus_os/installer/tests/vm/appliance-driver.py
Phoebus 25f7381b4a
Some checks are pending
ci / test (push) Waiting to run
feat(desktop): add Mango + DankMaterialShell profile with rollback, assistant and release foundations
- Mango + DankMaterialShell desktop option (desktop.profile = "mango") through
  the GURU overlay with scoped ~amd64 keywords, a sealed session entry point and
  a dwl fallback session (ADR 0009).
- Coherent rollback/recovery: coordinated @ + @var snapshots, fstab and BLS
  root-subvolume rewriting, matching-kernel selection, snapshot recovery entry,
  retention/space gates and sys undo (ADR 0007).
- Installed assistant (sys ask) with an OS-level systemd sandbox boundary and
  fail-closed typed plans; resolved package effects bound to approval.
- Nix build isolation enforced (sandbox, no fallback) plus lifecycle/cleanup.
- Desktop helpers (menu/lock/idle), transaction log, release gates with atomic
  publish and signing helpers, and a Debian-Live appliance VM harness.
2026-09-21 03:46:52 -07:00

156 lines
5.8 KiB
Bash

#!/usr/bin/env python3
"""Headless serial driver for the Debian-Live Phoebus appliance ISO.
The committed `serial-driver.py` drives a *Gentoo* live ISO with an explicit
kernel/initrd. The appliance (`installer/live/build.sh`) boots its own GRUB on
UEFI, so it needs its own driver.
Two modes:
install : boot the appliance, log in as `user`/`live`, fetch a guest script
over QEMU user networking, install Phoebus non-interactively, then
power off.
boot : boot the installed disk alone (no ISO), unlock LUKS, log in and run
verification commands.
Everything is parameterised through environment variables. `DISK` must be a
qcow2 image; this script never touches a physical disk. Secrets are served from
the host as 0600 files and fetched inside the guest, so they never appear in
the serial transcript.
Host setup (see docs/DEVELOPMENT.md):
mkdir -p /path/serve && cd /path/serve
cp /path/to/installer/live/phoebus-installer-amd64.iso .
cp <stage3>.tar.xz stage3.tar.xz && sha256sum stage3.tar.xz
cp /path/to/installer/tests/vm/appliance-guest.sh .
cp /path/to/installer/tests/vm/appliance-test.conf.example test.conf # set STAGE3_SHA256
printf 'phoebus-luks-test' > luks.pw && chmod 600 luks.pw
printf 'phoebus-user-test' > user.pw && chmod 600 user.pw
python3 -m http.server 8000 --bind 127.0.0.1
"""
import os
import sys
import pexpect
QEMU = os.environ.get("QEMU_BIN", "qemu-system-x86_64")
OVMF_CODE = os.environ["OVMF_CODE"]
OVMF_VARS = os.environ["OVMF_VARS"]
DISK = os.environ["DISK"]
ISO = os.environ.get("ISO", "")
SERIAL_LOG = os.environ.get("SERIAL_LOG", "/tmp/phoebus-appliance-serial.log")
SERVE_URL = os.environ.get("SERVE_URL", "http://10.0.2.2:8000")
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")
LIVE_USER = os.environ.get("LIVE_USER", "user")
LIVE_PW = os.environ.get("LIVE_PASSWORD", "live")
MEM = os.environ.get("VM_MEM", "8192")
SMP = os.environ.get("VM_SMP", "8")
INSTALL_TIMEOUT = int(os.environ.get("INSTALL_TIMEOUT", "10800"))
def base_args():
return [
"-machine", "q35,accel=kvm",
"-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",
]
def spawn(args):
child = pexpect.spawn(QEMU, args, timeout=900, encoding="utf-8",
codec_errors="replace", maxread=65536)
child.delaybeforesend = 0.4
return child
def login_live(child):
# The live medium may auto-login or present a login prompt; its hostname is
# typically `debian` (standard live) or `live`.
idx = child.expect([r"(?:live|debian) login:", r"[$#] $"], timeout=900)
if idx == 0:
child.sendline(LIVE_USER)
child.expect(r"Password:", timeout=60)
child.sendline(LIVE_PW)
child.expect(r"[$#] $", timeout=120)
def do_install():
args = base_args()
if not ISO:
raise SystemExit("ISO is required for install mode")
args += ["-drive", f"file={ISO},media=cdrom,readonly=on"]
log = open(SERIAL_LOG, "w")
child = spawn(args)
child.logfile = log
try:
login_live(child)
child.sendline(
f"curl -fsSL {SERVE_URL}/appliance-guest.sh -o /tmp/gi.sh && bash /tmp/gi.sh"
)
child.expect([r"===INSTALL-RC=([0-9]+)===", "===FETCH-FAIL===",
"Kernel panic", pexpect.EOF, pexpect.TIMEOUT],
timeout=INSTALL_TIMEOUT)
if "INSTALL-RC" in (child.after or ""):
print("INSTALL_RC", child.match.group(1), flush=True)
else:
print("INSTALL did not report a status", 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():
args = base_args() + ["-boot", "order=c"]
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; "
"/usr/local/bin/sys rollback status 2>&1 || true; "
"echo ===CHECKS-$(printf END)===",
)
log = open(SERIAL_LOG, "w")
child = spawn(args)
child.logfile = log
try:
child.expect([r"[Pp]assphrase", r"login:"], timeout=600)
if "assphrase" in (child.after or ""):
child.sendline(LUKS_PW)
for _ in range(5):
child.expect(r"phoebus login:", timeout=300)
child.sendline(USER)
idx = child.expect([r"[Pp]assword:", r"phoebus login:", r"Login incorrect"],
timeout=60)
if idx == 0:
child.sendline(USER_PW)
if child.expect([r"[$#] $", r"Login incorrect", r"phoebus login:"],
timeout=120) == 0:
break
child.sendline(checks)
child.expect("===CHECKS-END===", timeout=int(os.environ.get("CHECKS_TIMEOUT", "300")))
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)