qmp_install_test.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 that the target is a byte-identical clone of the image's used
  7. sectors and boots the target in BIOS (and UEFI when the image has an ESP and
  8. OVMF is available).
  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, and for the UEFI boot an
  13. 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. def qmp_recv(s):
  31. buf = b""
  32. while True:
  33. chunk = s.recv(4096)
  34. if not chunk:
  35. raise RuntimeError("QMP connection closed")
  36. buf += chunk
  37. while b"\n" in buf:
  38. line, buf = buf.split(b"\n", 1)
  39. line = line.strip()
  40. if line:
  41. return json.loads(line)
  42. def qmp_cmd(s, execute, arguments=None):
  43. req = {"execute": execute}
  44. if arguments is not None:
  45. req["arguments"] = arguments
  46. s.sendall((json.dumps(req) + "\n").encode())
  47. while True:
  48. msg = qmp_recv(s)
  49. if "return" in msg or "error" in msg:
  50. if "error" in msg:
  51. print("QMP error:", msg)
  52. return msg
  53. def send_key(s, key):
  54. qmp_cmd(s, "send-key", {"keys": [{"type": "qcode", "data": key}]})
  55. time.sleep(0.15)
  56. def send_text(s, text):
  57. for ch in text:
  58. if ch == " ":
  59. send_key(s, "spc")
  60. elif ch == "\n":
  61. send_key(s, "ret")
  62. else:
  63. send_key(s, ch)
  64. def rd32(p):
  65. return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24)
  66. def image_used(data):
  67. """Used image length from the MBR's highest partition end."""
  68. if len(data) < 512 or data[510:512] != b"\x55\xaa":
  69. return 0
  70. used = 0
  71. for i in range(4):
  72. e = 0x1BE + i * 16
  73. if data[e + 4] == 0:
  74. continue
  75. start = rd32(data[e + 8:e + 12])
  76. count = rd32(data[e + 12:e + 16])
  77. if not count or start + count > len(data) // 512:
  78. return 0
  79. used = max(used, start + count)
  80. return used
  81. def image_has_esp(data):
  82. for i in range(4):
  83. if data[0x1BE + i * 16 + 4] == 0xEF:
  84. return True
  85. return False
  86. def serial_has(path, needle):
  87. try:
  88. with open(path, "rb") as f:
  89. return needle.encode() in f.read()
  90. except FileNotFoundError:
  91. return False
  92. def wait_serial(path, needles, timeout):
  93. deadline = time.time() + timeout
  94. while time.time() < deadline:
  95. try:
  96. with open(path, "rb") as f:
  97. data = f.read()
  98. except FileNotFoundError:
  99. data = b""
  100. for n in needles:
  101. if n.encode() in data:
  102. return n
  103. time.sleep(0.5)
  104. return None
  105. def run_boot(image, out_log, uefi=False, timeout=45):
  106. if uefi:
  107. ovmf = next((p for p in OVMF_CANDIDATES if p and os.path.exists(p)), None)
  108. if not ovmf:
  109. return None
  110. qemu = ["qemu-system-x86_64", "-M", "pc", "-m", "512M", "-bios", ovmf]
  111. else:
  112. qemu = ["qemu-system-i386", "-M", "pc", "-m", "512M"]
  113. qemu += ["-boot", "c", "-drive", "file=%s,format=raw,if=ide" % image,
  114. "-serial", "file:%s" % out_log, "-display", "none",
  115. "-no-reboot"]
  116. try:
  117. subprocess.run(qemu, timeout=timeout, stdout=subprocess.DEVNULL,
  118. stderr=subprocess.DEVNULL)
  119. except subprocess.TimeoutExpired:
  120. pass
  121. return serial_has(out_log, "booted")
  122. def main():
  123. xhci = "--xhci" in sys.argv
  124. keep = "--keep" in sys.argv
  125. dual = os.path.join(ROOT, "usb-efi.img")
  126. legacy = os.path.join(ROOT, "usb.img")
  127. image = dual if os.path.exists(dual) else legacy
  128. if not os.path.exists(image):
  129. print("usb.img/usb-efi.img missing - run `make all`/`make uefi` first")
  130. return 1
  131. work = tempfile.mkdtemp(prefix="nebos-install-")
  132. src = os.path.join(work, "source.img")
  133. target = os.path.join(work, "target.img")
  134. live_log = os.path.join(work, "live.log")
  135. bios_log = os.path.join(work, "bios.log")
  136. uefi_log = os.path.join(work, "uefi.log")
  137. sock = os.path.join(work, "qmp.sock")
  138. shutil.copyfile(image, src)
  139. with open(target, "wb") as f:
  140. f.truncate(32 * 1024 * 1024)
  141. if xhci:
  142. # p3=0 keeps the whole xHCI bus at USB2 speeds; the kernel's xHCI
  143. # driver deliberately does not drive USB3 SuperSpeed ports.
  144. usb = ["-device", "qemu-xhci,p2=4,p3=0",
  145. "-device", "usb-storage,drive=src",
  146. "-device", "usb-mouse", "-device", "usb-kbd"]
  147. else:
  148. usb = ["-device", "usb-ehci,id=ehci0",
  149. "-device", "piix3-usb-uhci,id=uhci0",
  150. "-device", "usb-storage,bus=ehci0.0,drive=src",
  151. "-device", "usb-mouse,bus=uhci0.0",
  152. "-device", "usb-kbd,bus=uhci0.0"]
  153. qemu = ["qemu-system-i386", "-M", "pc", "-m", "512M", "-boot", "c"] + usb + [
  154. "-drive", "if=none,id=src,file=%s,format=raw,readonly=on" % src,
  155. "-drive", "file=%s,format=raw,if=ide" % image,
  156. "-drive", "file=%s,format=raw,if=ide" % target,
  157. "-display", "none", "-qmp", "unix:%s,server,nowait" % sock,
  158. "-serial", "file:%s" % live_log, "-no-reboot"]
  159. proc = subprocess.Popen(qemu, stdout=subprocess.DEVNULL,
  160. stderr=subprocess.DEVNULL)
  161. ok = False
  162. try:
  163. for _ in range(100):
  164. if os.path.exists(sock):
  165. break
  166. time.sleep(0.1)
  167. s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  168. s.connect(sock)
  169. qmp_recv(s) # greeting
  170. qmp_cmd(s, "qmp_capabilities")
  171. time.sleep(10.0) # boot the live system
  172. send_text(s, "install ide1\n")
  173. time.sleep(1.5)
  174. for _ in range(4): # the prompt may not be in raw mode yet
  175. send_key(s, "y")
  176. time.sleep(0.7)
  177. result = wait_serial(live_log, ["install: done", "install: failed"], 120)
  178. qmp_cmd(s, "quit")
  179. try:
  180. proc.wait(timeout=10)
  181. except subprocess.TimeoutExpired:
  182. pass
  183. msc = serial_has(live_log, "usbmsc:")
  184. used_line = serial_has(live_log, "install: usb0 -> ide1")
  185. print("usbmsc source:", "ok" if msc else "MISSING")
  186. print("installer used usb0:", "ok" if used_line else "NO")
  187. print("install result:", result or "TIMEOUT")
  188. src_data = open(src, "rb").read()
  189. used = image_used(src_data)
  190. tgt_data = open(target, "rb").read()
  191. clone = used and tgt_data[:used * 512] == src_data[:used * 512]
  192. print("image used: %d sectors, clone: %s" %
  193. (used, "ok" if clone else "MISMATCH"))
  194. bios = run_boot(target, bios_log, uefi=False)
  195. print("target BIOS boot:", "ok" if bios else "FAILED")
  196. uefi = None
  197. if image_has_esp(src_data):
  198. uefi = run_boot(target, uefi_log, uefi=True)
  199. if uefi is None:
  200. print("target UEFI boot: skipped (no OVMF)")
  201. else:
  202. print("target UEFI boot:", "ok" if uefi else "FAILED")
  203. else:
  204. print("target UEFI boot: skipped (source has no ESP)")
  205. ok = bool(msc and used_line and result == "install: done" and clone
  206. and bios and (uefi is None or uefi))
  207. finally:
  208. if proc.poll() is None:
  209. proc.kill()
  210. proc.wait()
  211. if keep:
  212. print("kept:", work)
  213. else:
  214. shutil.rmtree(work, ignore_errors=True)
  215. return 0 if ok else 1
  216. if __name__ == "__main__":
  217. sys.exit(main())