#!/usr/bin/env python3 """Headless driver for the Phoebus graphical live/installer ISO. The live medium boots into Mango + DankMaterialShell and opens the branded welcome window. This driver boots it with a graphical device and a serial console, checks the launcher/branding payload, drives the graphical installer with `wtype` over the live session, and captures QEMU framebuffer screenshots. Modes: gui : boot, log in on serial, verify the welcome/branding payload, open the installer, prove duplicate prevention, walk the first steps and capture screenshots. install : boot, log in on serial, fetch a non-interactive config and 0600 secrets over QEMU user networking, run the deterministic install with the static binary, scan the log for a secret leak, power off. boot : boot the installed disk alone (no ISO), unlock LUKS, log in and run verification commands. `DISK` must always be a disposable qcow2 image. This script never touches a physical disk. """ import os import socket import sys import time import pexpect QEMU = os.environ.get("QEMU_BIN", "qemu-system-x86_64") OVMF_CODE = os.environ["OVMF_CODE"] OVMF_VARS = os.environ["OVMF_VARS"] ISO = os.environ.get("ISO", "") DISK = os.environ.get("DISK", "") SERIAL_LOG = os.environ.get("SERIAL_LOG", "/tmp/phoebus-gui-serial.log") MONITOR = os.environ.get("MONITOR", "/tmp/phoebus-gui-monitor.sock") SHOT_DIR = os.environ.get("SHOT_DIR", "/tmp/phoebus-gui-shots") LIVE_USER = os.environ.get("LIVE_USER", "phoebus") LIVE_PW = os.environ.get("LIVE_PASSWORD", "phoebus") USER = os.environ.get("USERNAME", "phoebus") USER_PW = os.environ.get("USER_PASSWORD", "phoebus-user-test") LUKS_PW = os.environ.get("LUKS_PASSPHRASE", "phoebus-luks-test") SERVE_URL = os.environ.get("SERVE_URL", "http://10.0.2.2:8000") MEM = os.environ.get("VM_MEM", "8192") SMP = os.environ.get("VM_SMP", "8") BOOT_TIMEOUT = int(os.environ.get("BOOT_TIMEOUT", "900")) INSTALL_TIMEOUT = int(os.environ.get("INSTALL_TIMEOUT", "10800")) WAYLAND_ENV = "XDG_RUNTIME_DIR=/run/user/1000" def args(attach_iso=True, boot_disk=False): a = [ "-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}", ] if DISK: a += ["-drive", f"file={DISK},if=virtio,format=qcow2"] a += [ # A real GPU device so the compositor gets a DRM node and a seat. "-device", "virtio-gpu-pci", "-netdev", "user,id=n0", "-device", "virtio-net-pci,netdev=n0,romfile=", "-device", "virtio-rng-pci", ] if attach_iso and ISO: a += ["-drive", f"file={ISO},media=cdrom,readonly=on"] a += [ "-boot", "order=c" if boot_disk else "order=d", "-vga", "none", "-display", "none", "-serial", "stdio", "-monitor", f"unix:{MONITOR},server,nowait", "-no-reboot", ] return a def spawn(attach_iso=True, boot_disk=False): if os.path.exists(MONITOR): os.unlink(MONITOR) child = pexpect.spawn(QEMU, args(attach_iso, boot_disk), timeout=BOOT_TIMEOUT, encoding="utf-8", codec_errors="replace", maxread=65536) child.delaybeforesend = 0.4 return child def screendump(name): """Capture the QEMU framebuffer to SHOT_DIR/.ppm (and .png if ImageMagick is available).""" os.makedirs(SHOT_DIR, exist_ok=True) target = os.path.join(SHOT_DIR, name + ".ppm") if not os.path.exists(MONITOR): return None s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.settimeout(20) try: s.connect(MONITOR) time.sleep(0.5) try: s.recv(65536) except socket.timeout: pass s.sendall(b"screendump %s\n" % target.encode()) time.sleep(2) try: s.recv(65536) except socket.timeout: pass finally: s.close() if not os.path.exists(target): return None os.system(f"magick '{target}' '{target[:-4]}.png' 2>/dev/null || " f"convert '{target}' '{target[:-4]}.png' 2>/dev/null") return target def login_live(child): # The live serial getty shows ` login:` and the password is the # known live-only credential. idx = child.expect([r"login:", r"[$#] $"], timeout=BOOT_TIMEOUT) if idx == 0: child.sendline(LIVE_USER) child.expect(r"[Pp]assword:", timeout=90) child.sendline(LIVE_PW) child.expect(r"[$#] $", timeout=180) def live_shell(child, command, end_marker, timeout=180): child.sendline(command) child.expect(end_marker, timeout=timeout) def set_wayland(child): # Export the live user's Wayland socket for wtype-based GUI driving. child.sendline( 'export XDG_RUNTIME_DIR=/run/user/1000; ' 'export WAYLAND_DISPLAY="$(basename "$(ls /run/user/1000/wayland-* 2>/dev/null | head -1)")"; ' 'echo WAYLAND=$WAYLAND_DISPLAY' ) child.expect(r"WAYLAND=wayland-\d+", timeout=60) def sendkey(*keys): """Inject keys through the QEMU monitor (hardware keyboard level). This is independent of the guest's input-method/portal setup.""" if not os.path.exists(MONITOR): return s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.settimeout(10) try: s.connect(MONITOR) time.sleep(0.3) try: s.recv(65536) except socket.timeout: pass for k in keys: s.sendall(("sendkey %s\n" % k).encode()) time.sleep(0.2) time.sleep(0.3) try: s.recv(65536) except socket.timeout: pass finally: s.close() def key(child, *wtype_args): quoted = " ".join(f"'{a}'" for a in wtype_args) child.sendline(f"wtype {quoted} >/dev/null 2>&1; echo KEY-DONE") child.expect("KEY-DONE", timeout=60) def do_gui(): child = spawn() log = open(SERIAL_LOG, "w") child.logfile = log try: login_live(child) # Give greetd/the session time to bring up Mango + DMS + the welcome # window (it starts after the shell is ready). time.sleep(int(os.environ.get("SESSION_SETTLE", "30"))) for _ in range(12): child.sendline( "echo WELCOME=$(pgrep -fc 'quickshell.*phoebus-welcome' 2>/dev/null || echo 0)" ) child.expect(r"WELCOME=(\d+)", timeout=60) if int(child.match.group(1)) > 0: break time.sleep(5) # Payload checks (no secrets here). checks = ( "echo ===GUI-CHECKS-$(printf START)===; " "test -e /etc/phoebus/live && echo LIVE-MARKER; " "test -s /usr/share/phoebus/branding/wallpaper.svg && echo BRAND-WALLPAPER; " "test -s /usr/share/icons/hicolor/scalable/apps/phoebus.svg && echo BRAND-ICON; " "test -s /etc/xdg/quickshell/phoebus-install/shell.qml && echo INSTALL-QML; " "test -s /usr/share/applications/phoebus-install.desktop && echo DESKTOP-ENTRY; " "test -f /home/phoebus/.config/DankMaterialShell/.firstlaunch && echo ONBOARDING-SUPPRESSED; " "echo ===GUI-CHECKS-$(printf END)===" ) live_shell(child, checks, "===GUI-CHECKS-END===", timeout=180) shot = screendump("01-welcome") print("welcome screenshot:", shot, flush=True) set_wayland(child) # Backend disk protection on the real VM: the live medium must be # reported protected and never selectable. child.sendline( "printf '%s\\n%s\\n' '{\"id\":1,\"op\":\"disks\"}' '{\"id\":2,\"op\":\"quit\"}' " "| sudo -n /usr/local/bin/phoebus-installer --gui-bridge 2>/dev/null " "| head -1 > /tmp/disks.json; " "echo DISKS=$(wc -c /dev/null || echo 0)") child.expect(r"INST=(\d+)", timeout=60) if int(child.match.group(1)) == 0: # The window manager may not have focused the welcome; fall back to # the launcher so the rest of the journey can still be exercised. child.sendline( "setsid /usr/local/bin/phoebus-install >/tmp/install-gui.log 2>&1 & echo LAUNCHED" ) child.expect("LAUNCHED", timeout=30) time.sleep(7) # Surface the installer frontend's own log (it contains bridge errors). child.sendline( "echo QMLLOG-$(printf START); " "cat /run/user/1000/quickshell/by-id/*/log.qslog 2>/dev/null | " "grep -aiE 'phoebus-bridge|error|failed|sudo|handshake' | tail -30; " "echo NOTTY-SUDO; setsid sh -c 'sudo -n true' ; echo NOTTYSUDO=$?; " "echo QMLLOG-$(printf END)" ) child.expect("QMLLOG-END", timeout=60) screendump("02-installer-language") # The first handshake probes hardware and disks; wait for it before # driving the wizard so the backend is ready to validate. time.sleep(int(os.environ.get("BACKEND_SETTLE", "30"))) # Close the welcome so the installer window has the keyboard focus # (Mango tiles both windows; the welcome would otherwise receive keys). child.sendline("pkill -f 'quickshell.*phoebus-wel[c]ome' || true; sleep 2; echo CLOSED") child.expect("CLOSED", timeout=30) # Duplicate prevention: a second launch must not create a second session. child.sendline( "pgrep -f 'quickshell.*phoebus-install' | wc -l > /tmp/n1; " "/usr/local/bin/phoebus-install >/tmp/install-gui2.log 2>&1; " "pgrep -f 'quickshell.*phoebus-install' | wc -l > /tmp/n2; " "echo DUP n1=$(cat /tmp/n1) n2=$(cat /tmp/n2)" ) child.expect(r"DUP n1=(\d+) n2=(\d+)", timeout=60) print("duplicate check:", child.match.group(0), flush=True) # Move forward through the wizard with the keyboard shortcut. sendkey("ctrl-right") time.sleep(2) screendump("03-network-prerequisites") sendkey("ctrl-right") time.sleep(2) screendump("04-ai-model") # Advancing without a connected model must be refused (blocked prereq). sendkey("ctrl-right") time.sleep(2) screendump("05-ai-blocked") print("gui checks complete", flush=True) finally: child.close(force=True) log.close() def do_install(): child = spawn() log = open(SERIAL_LOG, "w") child.logfile = log try: login_live(child) child.sendline( f"curl -fsSL {SERVE_URL}/appliance-guest-gui.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(): # Detached: no ISO, boot from the installed disk only. child = spawn(attach_iso=False, boot_disk=True) log = open(SERIAL_LOG, "w") child.logfile = log 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)===", ) try: child.expect([r"[Pp]assphrase", r"login:"], timeout=BOOT_TIMEOUT) 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 == "gui": do_gui() elif mode == "install": do_install() elif mode == "boot": do_boot() else: print(__doc__) sys.exit(2)