#!/usr/bin/env python3 """End-to-end installer test (QEMU + QMP + USB mass storage). Boots the live USB image as an IDE disk, attaches a copy of the same image as a USB mass-storage device (the installer's source), adds a blank target disk, types `install ide1` on the terminal through QMP and confirms it. The test then checks that the target is a byte-identical clone of the image's used sectors and boots the target in BIOS (and UEFI when the image has an ESP and OVMF is available). Options: --xhci put the USB source on an xHCI controller instead of EHCI/UHCI --keep keep the temporary directory (target image and logs) Requires: usb-efi.img (or usb.img), python3, QEMU, and for the UEFI boot an OVMF firmware (OVMF=/path or the usual distribution paths). Exit code 0 = pass. """ import json import os import shutil import socket import subprocess import sys import tempfile import time ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) OVMF_CANDIDATES = [ os.environ.get("OVMF", ""), "/usr/share/ovmf/x64/OVMF.4m.fd", "/usr/share/OVMF/OVMF.4m.fd", ] def qmp_recv(s): buf = b"" while True: chunk = s.recv(4096) if not chunk: raise RuntimeError("QMP connection closed") buf += chunk while b"\n" in buf: line, buf = buf.split(b"\n", 1) line = line.strip() if line: return json.loads(line) def qmp_cmd(s, execute, arguments=None): req = {"execute": execute} if arguments is not None: req["arguments"] = arguments s.sendall((json.dumps(req) + "\n").encode()) while True: msg = qmp_recv(s) if "return" in msg or "error" in msg: if "error" in msg: print("QMP error:", msg) return msg def send_key(s, key): qmp_cmd(s, "send-key", {"keys": [{"type": "qcode", "data": key}]}) time.sleep(0.15) def send_text(s, text): for ch in text: if ch == " ": send_key(s, "spc") elif ch == "\n": send_key(s, "ret") else: send_key(s, ch) def rd32(p): return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24) def image_used(data): """Used image length from the MBR's highest partition end.""" if len(data) < 512 or data[510:512] != b"\x55\xaa": return 0 used = 0 for i in range(4): e = 0x1BE + i * 16 if data[e + 4] == 0: continue start = rd32(data[e + 8:e + 12]) count = rd32(data[e + 12:e + 16]) if not count or start + count > len(data) // 512: return 0 used = max(used, start + count) return used def image_has_esp(data): for i in range(4): if data[0x1BE + i * 16 + 4] == 0xEF: return True return False def serial_has(path, needle): try: with open(path, "rb") as f: return needle.encode() in f.read() except FileNotFoundError: return False def wait_serial(path, needles, timeout): deadline = time.time() + timeout while time.time() < deadline: try: with open(path, "rb") as f: data = f.read() except FileNotFoundError: data = b"" for n in needles: if n.encode() in data: return n time.sleep(0.5) return None def run_boot(image, out_log, uefi=False, timeout=45): if uefi: ovmf = next((p for p in OVMF_CANDIDATES if p and os.path.exists(p)), None) if not ovmf: return None qemu = ["qemu-system-x86_64", "-M", "pc", "-m", "512M", "-bios", ovmf] else: qemu = ["qemu-system-i386", "-M", "pc", "-m", "512M"] qemu += ["-boot", "c", "-drive", "file=%s,format=raw,if=ide" % image, "-serial", "file:%s" % out_log, "-display", "none", "-no-reboot"] try: subprocess.run(qemu, timeout=timeout, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) except subprocess.TimeoutExpired: pass return serial_has(out_log, "booted") def main(): xhci = "--xhci" in sys.argv keep = "--keep" in sys.argv dual = os.path.join(ROOT, "usb-efi.img") legacy = os.path.join(ROOT, "usb.img") image = dual if os.path.exists(dual) else legacy if not os.path.exists(image): print("usb.img/usb-efi.img missing - run `make all`/`make uefi` first") return 1 work = tempfile.mkdtemp(prefix="nebos-install-") src = os.path.join(work, "source.img") target = os.path.join(work, "target.img") live_log = os.path.join(work, "live.log") bios_log = os.path.join(work, "bios.log") uefi_log = os.path.join(work, "uefi.log") sock = os.path.join(work, "qmp.sock") shutil.copyfile(image, src) with open(target, "wb") as f: f.truncate(32 * 1024 * 1024) if xhci: # qemu-xhci's first port is SuperSpeed: this exercises the USB3 # path used by the Chromebook's USB 3.0 ports. The HID devices # land on the USB2 ports of the same controller. usb = ["-device", "qemu-xhci", "-device", "usb-storage,drive=src", "-device", "usb-mouse", "-device", "usb-kbd"] else: usb = ["-device", "usb-ehci,id=ehci0", "-device", "piix3-usb-uhci,id=uhci0", "-device", "usb-storage,bus=ehci0.0,drive=src", "-device", "usb-mouse,bus=uhci0.0", "-device", "usb-kbd,bus=uhci0.0"] qemu = ["qemu-system-i386", "-M", "pc", "-m", "512M", "-boot", "c"] + usb + [ "-drive", "if=none,id=src,file=%s,format=raw,readonly=on" % src, "-drive", "file=%s,format=raw,if=ide" % image, "-drive", "file=%s,format=raw,if=ide" % target, "-display", "none", "-qmp", "unix:%s,server,nowait" % sock, "-serial", "file:%s" % live_log, "-no-reboot"] proc = subprocess.Popen(qemu, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) ok = False try: for _ in range(100): if os.path.exists(sock): break time.sleep(0.1) s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.connect(sock) qmp_recv(s) # greeting qmp_cmd(s, "qmp_capabilities") time.sleep(10.0) # boot the live system send_text(s, "install ide1\n") time.sleep(1.5) for _ in range(4): # the prompt may not be in raw mode yet send_key(s, "y") time.sleep(0.7) result = wait_serial(live_log, ["install: done", "install: failed"], 120) qmp_cmd(s, "quit") try: proc.wait(timeout=10) except subprocess.TimeoutExpired: pass msc = serial_has(live_log, "usbmsc:") used_line = serial_has(live_log, "install: usb0 -> ide1") print("usbmsc source:", "ok" if msc else "MISSING") print("installer used usb0:", "ok" if used_line else "NO") print("install result:", result or "TIMEOUT") src_data = open(src, "rb").read() used = image_used(src_data) tgt_data = open(target, "rb").read() clone = used and tgt_data[:used * 512] == src_data[:used * 512] print("image used: %d sectors, clone: %s" % (used, "ok" if clone else "MISMATCH")) bios = run_boot(target, bios_log, uefi=False) print("target BIOS boot:", "ok" if bios else "FAILED") uefi = None if image_has_esp(src_data): uefi = run_boot(target, uefi_log, uefi=True) if uefi is None: print("target UEFI boot: skipped (no OVMF)") else: print("target UEFI boot:", "ok" if uefi else "FAILED") else: print("target UEFI boot: skipped (source has no ESP)") ok = bool(msc and used_line and result == "install: done" and clone and bios and (uefi is None or uefi)) finally: if proc.poll() is None: proc.kill() proc.wait() if keep: print("kept:", work) else: shutil.rmtree(work, ignore_errors=True) return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())