#!/usr/bin/env python3 """Launch an interactive QEMU VM on the user's host desktop with live payload updates.""" import os import shutil import subprocess import sys import threading import time from pathlib import Path import pexpect WORKDIR = Path("/var/tmp/phoebus-vm") DISK = WORKDIR / "disks" / "test-disk.qcow2" VARS = WORKDIR / "OVMF_VARS.fd" OVMF_CODE = Path("/usr/share/OVMF/OVMF_CODE_4M.fd") OVMF_VARS_SRC = Path("/usr/share/OVMF/OVMF_VARS_4M.fd") ISO = Path("/home/dominic/Source/phoebus_os/installer/live/phoebus-installer-amd64.iso") SERVE_URL = "http://10.0.2.2:8000" WORKDIR.mkdir(parents=True, exist_ok=True) (WORKDIR / "disks").mkdir(parents=True, exist_ok=True) print("[VM] Checking live ISO status...") subprocess.run(["bash", "/home/dominic/Source/phoebus_os/scripts/ensure-live-iso.sh"], check=True) if not DISK.exists(): print(f"Creating 30G scratch disk at {DISK}") subprocess.run(["qemu-img", "create", "-f", "qcow2", str(DISK), "30G"], check=True) shutil.copyfile(OVMF_VARS_SRC, VARS) accel = ["-enable-kvm"] if os.access("/dev/kvm", os.W_OK) else [] cmd = [ "qemu-system-x86_64", "-machine", "q35", *accel, "-smp", "4", "-m", "4096", "-cpu", "host", "-drive", f"if=pflash,format=raw,readonly=on,file={OVMF_CODE}", "-drive", f"if=pflash,format=raw,file={VARS}", "-drive", f"file={DISK},if=virtio,format=qcow2", "-drive", f"file={ISO},media=cdrom,readonly=on", "-boot", "order=d", "-device", "virtio-vga", "-display", "gtk,zoom-to-fit=on", "-netdev", "user,id=n0", "-device", "virtio-net-pci,netdev=n0", "-device", "virtio-rng-pci", "-serial", "stdio", ] print("=" * 60) print("Starting interactive Phoebus VM with display...") print(f"ISO: {ISO}") print(f"Disk: {DISK}") print("=" * 60) child = pexpect.spawn(cmd[0], cmd[1:], timeout=300, encoding="utf-8", codec_errors="replace") def background_updater(): try: print("[VM Helper] Waiting for live login prompt on serial...") idx = child.expect(["phoebus-live login:", pexpect.EOF], timeout=240) if idx != 0: return time.sleep(1) child.sendline("phoebus") child.expect("[Pp]assword:") child.sendline("phoebus") child.expect(r"[\$#] ") print("[VM Helper] Logged into live system over serial. Updating guest payload...") # Download update.sh and execute child.sendline( f"curl -fsSL {SERVE_URL}/update.sh -o /tmp/update.sh && " "echo phoebus | sudo -S -p '' bash /tmp/update.sh && " "echo GUEST_READY" ) child.expect("GUEST_READY", timeout=120) print("[VM Helper] Guest payload updated successfully! All latest UI changes & OpenCode forwarder are active.") print("[VM Helper] You can now interact with the VM window on your desktop.") except Exception as e: print(f"[VM Helper] Auto-update note: {e}") updater_thread = threading.Thread(target=background_updater, daemon=True) updater_thread.start() try: # Forward any output or keep process alive until user closes window while child.isalive(): try: line = child.readline() if line: pass except pexpect.TIMEOUT: pass except KeyboardInterrupt: print("Stopping VM...") child.terminate(force=True) child.wait() print("VM closed.")