qmp_install_test.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #!/usr/bin/env python3
  2. """End-to-end installer test (QEMU + QMP + USB mass storage).
  3. Boots the live USB image as an IDE disk, attaches a copy of the same image as
  4. a USB mass-storage device (the installer's source), adds a blank target disk,
  5. types `install ide1` on the terminal through QMP and confirms it. The test
  6. then checks the cloned P1..P3, the new /data partition (partition 4), writes a
  7. file into /data, runs a second install (which must keep it), and boots the
  8. target in BIOS and UEFI.
  9. Options:
  10. --xhci put the USB source on an xHCI controller instead of EHCI/UHCI
  11. --keep keep the temporary directory (target image and logs)
  12. Requires: usb-efi.img (or usb.img), python3, QEMU, mtools, and for the UEFI
  13. boot an OVMF firmware (OVMF=/path or the usual distribution paths).
  14. Exit code 0 = pass.
  15. """
  16. import json
  17. import os
  18. import shutil
  19. import socket
  20. import subprocess
  21. import sys
  22. import tempfile
  23. import time
  24. ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  25. OVMF_CANDIDATES = [
  26. os.environ.get("OVMF", ""),
  27. "/usr/share/ovmf/x64/OVMF.4m.fd",
  28. "/usr/share/OVMF/OVMF.4m.fd",
  29. ]
  30. DATA_LBA = 34816
  31. DATA_OFF = DATA_LBA * 512
  32. def qmp_recv(s):
  33. buf = b""
  34. while True:
  35. chunk = s.recv(4096)
  36. if not chunk:
  37. raise RuntimeError("QMP connection closed")
  38. buf += chunk
  39. while b"\n" in buf:
  40. line, buf = buf.split(b"\n", 1)
  41. line = line.strip()
  42. if line:
  43. return json.loads(line)
  44. def qmp_cmd(s, execute, arguments=None):
  45. req = {"execute": execute}
  46. if arguments is not None:
  47. req["arguments"] = arguments
  48. s.sendall((json.dumps(req) + "\n").encode())
  49. while True:
  50. msg = qmp_recv(s)
  51. if "return" in msg or "error" in msg:
  52. if "error" in msg:
  53. print("QMP error:", msg)
  54. return msg
  55. def send_key(s, key):
  56. qmp_cmd(s, "send-key", {"keys": [{"type": "qcode", "data": key}]})
  57. time.sleep(0.15)
  58. def send_text(s, text):
  59. for ch in text:
  60. if ch == " ":
  61. send_key(s, "spc")
  62. elif ch == "\n":
  63. send_key(s, "ret")
  64. else:
  65. send_key(s, ch)
  66. def rd32(p):
  67. return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24)
  68. def image_used(data):
  69. """Used image length from the MBR's highest partition end."""
  70. if len(data) < 512 or data[510:512] != b"\x55\xaa":
  71. return 0
  72. used = 0
  73. for i in range(4):
  74. e = 0x1BE + i * 16
  75. if data[e + 4] == 0:
  76. continue
  77. start = rd32(data[e + 8:e + 12])
  78. count = rd32(data[e + 12:e + 16])
  79. if not count or start + count > len(data) // 512:
  80. return 0
  81. used = max(used, start + count)
  82. return used
  83. def image_has_esp(data):
  84. for i in range(4):
  85. if data[0x1BE + i * 16 + 4] == 0xEF:
  86. return True
  87. return False
  88. def serial_has(path, needle):
  89. try:
  90. with open(path, "rb") as f:
  91. return needle.encode() in f.read()
  92. except FileNotFoundError:
  93. return False
  94. def wait_serial(path, needles, timeout):
  95. deadline = time.time() + timeout
  96. while time.time() < deadline:
  97. try:
  98. with open(path, "rb") as f:
  99. data = f.read()
  100. except FileNotFoundError:
  101. data = b""
  102. for n in needles:
  103. if n.encode() in data:
  104. return n
  105. time.sleep(0.5)
  106. return None
  107. def run_boot(image, out_log, uefi=False, timeout=45):
  108. if uefi:
  109. ovmf = next((p for p in OVMF_CANDIDATES if p and os.path.exists(p)), None)
  110. if not ovmf:
  111. return None
  112. qemu = ["qemu-system-x86_64", "-M", "pc", "-m", "512M", "-bios", ovmf]
  113. else:
  114. qemu = ["qemu-system-i386", "-M", "pc", "-m", "512M"]
  115. qemu += ["-boot", "c", "-drive", "file=%s,format=raw,if=ide" % image,
  116. "-serial", "file:%s" % out_log, "-display", "none",
  117. "-no-reboot"]
  118. try:
  119. subprocess.run(qemu, timeout=timeout, stdout=subprocess.DEVNULL,
  120. stderr=subprocess.DEVNULL)
  121. except subprocess.TimeoutExpired:
  122. pass
  123. return serial_has(out_log, "booted")
  124. def live_qemu(image, src, target, xhci, sock, log):
  125. if xhci:
  126. # qemu-xhci's first port is SuperSpeed: this exercises the USB3
  127. # path used by the Chromebook's USB 3.0 ports. The HID devices
  128. # land on the USB2 ports of the same controller.
  129. usb = ["-device", "qemu-xhci",
  130. "-device", "usb-storage,drive=src",
  131. "-device", "usb-mouse", "-device", "usb-kbd"]
  132. else:
  133. usb = ["-device", "usb-ehci,id=ehci0",
  134. "-device", "piix3-usb-uhci,id=uhci0",
  135. "-device", "usb-storage,bus=ehci0.0,drive=src",
  136. "-device", "usb-mouse,bus=uhci0.0",
  137. "-device", "usb-kbd,bus=uhci0.0"]
  138. return ["qemu-system-i386", "-M", "pc", "-m", "512M", "-boot", "c"] + usb + [
  139. "-drive", "if=none,id=src,file=%s,format=raw,readonly=on" % src,
  140. "-drive", "file=%s,format=raw,if=ide" % image,
  141. "-drive", "file=%s,format=raw,if=ide" % target,
  142. "-display", "none", "-qmp", "unix:%s,server,nowait" % sock,
  143. "-serial", "file:%s" % log, "-no-reboot"]
  144. def run_live_install(image, src, target, xhci, sock, log, timeout=300,
  145. cmd="install ide1"):
  146. """Boot the live image, run one install command, confirm, and return."""
  147. qemu = live_qemu(image, src, target, xhci, sock, log)
  148. qemu_log = log + ".qemu"
  149. qerr = open(qemu_log, "wb")
  150. proc = subprocess.Popen(qemu, stdout=qerr, stderr=subprocess.STDOUT)
  151. try:
  152. for _ in range(100):
  153. if os.path.exists(sock) or proc.poll() is not None:
  154. break
  155. time.sleep(0.1)
  156. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  157. try:
  158. s.connect(sock)
  159. except OSError:
  160. qerr.flush()
  161. print("QEMU failed to start (see %s):" % qemu_log)
  162. print(open(qemu_log, "rb").read().decode("latin-1", "replace"))
  163. raise
  164. qmp_recv(s) # greeting
  165. qmp_cmd(s, "qmp_capabilities")
  166. time.sleep(10.0) # boot the live system
  167. send_text(s, cmd + "\n")
  168. time.sleep(1.5)
  169. for _ in range(4): # the prompt may not be in raw mode yet
  170. send_key(s, "y")
  171. time.sleep(0.7)
  172. result = wait_serial(log, ["install: done", "install: self done",
  173. "install: failed"], timeout)
  174. s.settimeout(2)
  175. try:
  176. qmp_cmd(s, "quit")
  177. except Exception:
  178. pass
  179. s.close()
  180. try:
  181. proc.wait(timeout=10)
  182. except subprocess.TimeoutExpired:
  183. proc.terminate()
  184. proc.wait(timeout=5)
  185. return result
  186. finally:
  187. if proc.poll() is None:
  188. proc.kill()
  189. proc.wait()
  190. qerr.close()
  191. def mcopy_to_p4(target, src_file, dest):
  192. return subprocess.run(
  193. ["mcopy", "-i", "%s@@%d" % (target, DATA_OFF), src_file, "::/" + dest],
  194. stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode == 0
  195. def mcopy_from_p4(target, src, out_file):
  196. return subprocess.run(
  197. ["mcopy", "-i", "%s@@%d" % (target, DATA_OFF), "::/" + src, out_file],
  198. stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode == 0
  199. def p4_label(data):
  200. if len(data) < DATA_OFF + 512:
  201. return ""
  202. boot = data[DATA_OFF:DATA_OFF + 512]
  203. # FAT12/16 label at 0x2B, FAT32 at 0x47. Our test target is small, so
  204. # the formatter chooses FAT16; accept either offset.
  205. for off in (0x2B, 0x47):
  206. label = boot[off:off + 11].decode("latin-1").rstrip()
  207. if label:
  208. return label
  209. return ""
  210. def main():
  211. xhci = "--xhci" in sys.argv
  212. keep = "--keep" in sys.argv
  213. dual = os.path.join(ROOT, "usb-efi.img")
  214. legacy = os.path.join(ROOT, "usb.img")
  215. image = dual if os.path.exists(dual) else legacy
  216. if not os.path.exists(image):
  217. print("usb.img/usb-efi.img missing - run `make all`/`make uefi` first")
  218. return 1
  219. work = tempfile.mkdtemp(prefix="nebos-install-")
  220. live = os.path.join(work, "live.img")
  221. src = os.path.join(work, "source.img")
  222. target = os.path.join(work, "target.img")
  223. live1_log = os.path.join(work, "live1.log")
  224. live2_log = os.path.join(work, "live2.log")
  225. live3_log = os.path.join(work, "live3.log")
  226. bios_log = os.path.join(work, "bios.log")
  227. uefi_log = os.path.join(work, "uefi.log")
  228. sock1 = os.path.join(work, "qmp1.sock")
  229. sock2 = os.path.join(work, "qmp2.sock")
  230. sock3 = os.path.join(work, "qmp3.sock")
  231. keep_file = os.path.join(work, "KEEP.TXT")
  232. out_file = os.path.join(work, "KEEP.OUT")
  233. shutil.copyfile(image, live)
  234. shutil.copyfile(image, src)
  235. with open(target, "wb") as f:
  236. f.truncate(32 * 1024 * 1024)
  237. with open(keep_file, "w") as f:
  238. f.write("persistent data\n")
  239. ok = False
  240. try:
  241. result1 = run_live_install(live, src, target, xhci, sock1, live1_log)
  242. print("install 1:", result1 or "TIMEOUT")
  243. src_data = open(src, "rb").read()
  244. used = image_used(src_data)
  245. tgt_data = open(target, "rb").read()
  246. p1_mbr = tgt_data[0x1BE:0x1BE + 48] == src_data[0x1BE:0x1BE + 48]
  247. clone = used and tgt_data[512:used * 512] == src_data[512:used * 512]
  248. print("image used: %d sectors, P1-P3 clone: %s" %
  249. (used, "ok" if clone else "MISMATCH"))
  250. p4 = tgt_data[0x1BE + 3 * 16:0x1BE + 4 * 16]
  251. p4_start = rd32(p4[8:12])
  252. p4_count = rd32(p4[12:16])
  253. label = p4_label(tgt_data)
  254. print("partition 4: start=%d count=%d label=%r" %
  255. (p4_start, p4_count, label))
  256. p4_ok = (p4_start == DATA_LBA and p4_count >= 2048 and
  257. label == "NEBOSDATA")
  258. print("partition 4 layout:", "ok" if p4_ok else "BAD")
  259. p4_boot_before = tgt_data[DATA_OFF:DATA_OFF + 512]
  260. file_written = mcopy_to_p4(target, keep_file, "KEEP.TXT")
  261. print("write /data/KEEP.TXT:", "ok" if file_written else "FAILED")
  262. result2 = run_live_install(live, src, target, xhci, sock2, live2_log)
  263. print("install 2:", result2 or "TIMEOUT")
  264. tgt_data = open(target, "rb").read()
  265. clone2 = used and tgt_data[512:used * 512] == src_data[512:used * 512]
  266. p4_boot_after = tgt_data[DATA_OFF:DATA_OFF + 512]
  267. p4_kept = p4_boot_before == p4_boot_after
  268. file_ok = mcopy_from_p4(target, "KEEP.TXT", out_file)
  269. file_data = ""
  270. if file_ok:
  271. file_data = open(out_file).read()
  272. print("P1-P3 clone after reinstall:", "ok" if clone2 else "MISMATCH")
  273. print("P4 boot sector kept:", "ok" if p4_kept else "CHANGED")
  274. print("KEEP.TXT kept:", "ok" if file_data == "persistent data\n" else "FAILED")
  275. # `install <target> self` writes the running pack/kernel, with no
  276. # source disk. It must also keep P4 and the file.
  277. p4_boot_self = tgt_data[DATA_OFF:DATA_OFF + 512]
  278. result3 = run_live_install(live, src, target, xhci, sock3,
  279. live3_log, cmd="install ide1 self")
  280. print("install self:", result3 or "TIMEOUT")
  281. tgt_data = open(target, "rb").read()
  282. clone3 = used and tgt_data[512:used * 512] == src_data[512:used * 512]
  283. p4_kept3 = p4_boot_self == tgt_data[DATA_OFF:DATA_OFF + 512]
  284. file_ok3 = mcopy_from_p4(target, "KEEP.TXT", out_file)
  285. file_data3 = open(out_file).read() if file_ok3 else ""
  286. print("P1-P3 clone after self:", "ok" if clone3 else "MISMATCH")
  287. print("P4 boot sector after self:", "ok" if p4_kept3 else "CHANGED")
  288. print("KEEP.TXT after self:", "ok" if file_data3 == "persistent data\n"
  289. else "FAILED")
  290. bios = run_boot(target, bios_log, uefi=False)
  291. print("target BIOS boot:", "ok" if bios else "FAILED")
  292. print("target /data mount:", "ok" if serial_has(bios_log, "vfs: data FAT")
  293. else "MISSING")
  294. uefi = None
  295. disk_boot = False
  296. if image_has_esp(src_data):
  297. uefi = run_boot(target, uefi_log, uefi=True)
  298. disk_boot = serial_has(uefi_log, "kernel + rootfs loaded from disk")
  299. if uefi is None:
  300. print("target UEFI boot: skipped (no OVMF)")
  301. else:
  302. print("target UEFI boot:", "ok" if uefi else "FAILED")
  303. print("UEFI disk image:", "ok" if disk_boot else "MISSING")
  304. else:
  305. print("target UEFI boot: skipped (source has no ESP)")
  306. ok = bool(result1 == "install: done" and result2 == "install: done" and
  307. result3 in ("install: done", "install: self done") and
  308. clone and clone2 and clone3 and p1_mbr and p4_ok and
  309. p4_kept and p4_kept3 and file_ok and
  310. file_data == "persistent data\n" and file_ok3 and
  311. file_data3 == "persistent data\n" and
  312. bios and serial_has(bios_log, "vfs: data FAT") and
  313. (uefi is None or (uefi and disk_boot)))
  314. finally:
  315. if keep:
  316. print("kept:", work)
  317. else:
  318. shutil.rmtree(work, ignore_errors=True)
  319. return 0 if ok else 1
  320. if __name__ == "__main__":
  321. sys.exit(main())