#!/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 the cloned P1..P3, the new /data partition (partition 4), writes a file into /data, runs a second install (which must keep it), and boots the target in BIOS and UEFI. 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, mtools, 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", ] DATA_LBA = 34816 DATA_OFF = DATA_LBA * 512 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 live_qemu(image, src, target, xhci, sock, log): 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"] return ["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" % log, "-no-reboot"] def run_live_install(image, src, target, xhci, sock, log, timeout=300, cmd="install ide1"): """Boot the live image, run one install command, confirm, and return.""" qemu = live_qemu(image, src, target, xhci, sock, log) qemu_log = log + ".qemu" qerr = open(qemu_log, "wb") proc = subprocess.Popen(qemu, stdout=qerr, stderr=subprocess.STDOUT) try: for _ in range(100): if os.path.exists(sock) or proc.poll() is not None: break time.sleep(0.1) s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: s.connect(sock) except OSError: qerr.flush() print("QEMU failed to start (see %s):" % qemu_log) print(open(qemu_log, "rb").read().decode("latin-1", "replace")) raise qmp_recv(s) # greeting qmp_cmd(s, "qmp_capabilities") time.sleep(10.0) # boot the live system send_text(s, cmd + "\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(log, ["install: done", "install: self done", "install: failed"], timeout) s.settimeout(2) try: qmp_cmd(s, "quit") except Exception: pass s.close() try: proc.wait(timeout=10) except subprocess.TimeoutExpired: proc.terminate() proc.wait(timeout=5) return result finally: if proc.poll() is None: proc.kill() proc.wait() qerr.close() def mcopy_to_p4(target, src_file, dest): return subprocess.run( ["mcopy", "-i", "%s@@%d" % (target, DATA_OFF), src_file, "::/" + dest], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode == 0 def mcopy_from_p4(target, src, out_file): return subprocess.run( ["mcopy", "-i", "%s@@%d" % (target, DATA_OFF), "::/" + src, out_file], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode == 0 def p4_label(data): if len(data) < DATA_OFF + 512: return "" boot = data[DATA_OFF:DATA_OFF + 512] # FAT12/16 label at 0x2B, FAT32 at 0x47. Our test target is small, so # the formatter chooses FAT16; accept either offset. for off in (0x2B, 0x47): label = boot[off:off + 11].decode("latin-1").rstrip() if label: return label return "" 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-") live = os.path.join(work, "live.img") src = os.path.join(work, "source.img") target = os.path.join(work, "target.img") live1_log = os.path.join(work, "live1.log") live2_log = os.path.join(work, "live2.log") live3_log = os.path.join(work, "live3.log") bios_log = os.path.join(work, "bios.log") uefi_log = os.path.join(work, "uefi.log") sock1 = os.path.join(work, "qmp1.sock") sock2 = os.path.join(work, "qmp2.sock") sock3 = os.path.join(work, "qmp3.sock") keep_file = os.path.join(work, "KEEP.TXT") out_file = os.path.join(work, "KEEP.OUT") shutil.copyfile(image, live) shutil.copyfile(image, src) with open(target, "wb") as f: f.truncate(32 * 1024 * 1024) with open(keep_file, "w") as f: f.write("persistent data\n") ok = False try: result1 = run_live_install(live, src, target, xhci, sock1, live1_log) print("install 1:", result1 or "TIMEOUT") src_data = open(src, "rb").read() used = image_used(src_data) tgt_data = open(target, "rb").read() p1_mbr = tgt_data[0x1BE:0x1BE + 48] == src_data[0x1BE:0x1BE + 48] clone = used and tgt_data[512:used * 512] == src_data[512:used * 512] print("image used: %d sectors, P1-P3 clone: %s" % (used, "ok" if clone else "MISMATCH")) p4 = tgt_data[0x1BE + 3 * 16:0x1BE + 4 * 16] p4_start = rd32(p4[8:12]) p4_count = rd32(p4[12:16]) label = p4_label(tgt_data) print("partition 4: start=%d count=%d label=%r" % (p4_start, p4_count, label)) p4_ok = (p4_start == DATA_LBA and p4_count >= 2048 and label == "NEBOSDATA") print("partition 4 layout:", "ok" if p4_ok else "BAD") p4_boot_before = tgt_data[DATA_OFF:DATA_OFF + 512] file_written = mcopy_to_p4(target, keep_file, "KEEP.TXT") print("write /data/KEEP.TXT:", "ok" if file_written else "FAILED") result2 = run_live_install(live, src, target, xhci, sock2, live2_log) print("install 2:", result2 or "TIMEOUT") tgt_data = open(target, "rb").read() clone2 = used and tgt_data[512:used * 512] == src_data[512:used * 512] p4_boot_after = tgt_data[DATA_OFF:DATA_OFF + 512] p4_kept = p4_boot_before == p4_boot_after file_ok = mcopy_from_p4(target, "KEEP.TXT", out_file) file_data = "" if file_ok: file_data = open(out_file).read() print("P1-P3 clone after reinstall:", "ok" if clone2 else "MISMATCH") print("P4 boot sector kept:", "ok" if p4_kept else "CHANGED") print("KEEP.TXT kept:", "ok" if file_data == "persistent data\n" else "FAILED") # `install self` writes the running pack/kernel, with no # source disk. It must also keep P4 and the file. p4_boot_self = tgt_data[DATA_OFF:DATA_OFF + 512] result3 = run_live_install(live, src, target, xhci, sock3, live3_log, cmd="install ide1 self") print("install self:", result3 or "TIMEOUT") tgt_data = open(target, "rb").read() clone3 = used and tgt_data[512:used * 512] == src_data[512:used * 512] p4_kept3 = p4_boot_self == tgt_data[DATA_OFF:DATA_OFF + 512] file_ok3 = mcopy_from_p4(target, "KEEP.TXT", out_file) file_data3 = open(out_file).read() if file_ok3 else "" print("P1-P3 clone after self:", "ok" if clone3 else "MISMATCH") print("P4 boot sector after self:", "ok" if p4_kept3 else "CHANGED") print("KEEP.TXT after self:", "ok" if file_data3 == "persistent data\n" else "FAILED") bios = run_boot(target, bios_log, uefi=False) print("target BIOS boot:", "ok" if bios else "FAILED") print("target /data mount:", "ok" if serial_has(bios_log, "vfs: data FAT") else "MISSING") uefi = None disk_boot = False if image_has_esp(src_data): uefi = run_boot(target, uefi_log, uefi=True) disk_boot = serial_has(uefi_log, "kernel + rootfs loaded from disk") if uefi is None: print("target UEFI boot: skipped (no OVMF)") else: print("target UEFI boot:", "ok" if uefi else "FAILED") print("UEFI disk image:", "ok" if disk_boot else "MISSING") else: print("target UEFI boot: skipped (source has no ESP)") ok = bool(result1 == "install: done" and result2 == "install: done" and result3 in ("install: done", "install: self done") and clone and clone2 and clone3 and p1_mbr and p4_ok and p4_kept and p4_kept3 and file_ok and file_data == "persistent data\n" and file_ok3 and file_data3 == "persistent data\n" and bios and serial_has(bios_log, "vfs: data FAT") and (uefi is None or (uefi and disk_boot))) finally: if keep: print("kept:", work) else: shutil.rmtree(work, ignore_errors=True) return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())