emulator.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import pexpect
  2. import infra
  3. import os
  4. class Emulator(object):
  5. def __init__(self, builddir, downloaddir, logtofile, timeout_multiplier):
  6. self.qemu = None
  7. self.downloaddir = downloaddir
  8. self.logfile = infra.open_log_file(builddir, "run", logtofile)
  9. # We use elastic runners on the cloud to runs our tests. Those runners
  10. # can take a long time to run the emulator. Use a timeout multiplier
  11. # when running the tests to avoid sporadic failures.
  12. self.timeout_multiplier = timeout_multiplier
  13. # Start Qemu to boot the system
  14. #
  15. # arch: Qemu architecture to use
  16. #
  17. # kernel: path to the kernel image, or the special string
  18. # 'builtin'. 'builtin' means a pre-built kernel image will be
  19. # downloaded from ARTIFACTS_URL and suitable options are
  20. # automatically passed to qemu and added to the kernel cmdline. So
  21. # far only armv5, armv7 and i386 builtin kernels are available.
  22. # If None, then no kernel is used, and we assume a bootable device
  23. # will be specified.
  24. #
  25. # kernel_cmdline: array of kernel arguments to pass to Qemu -append option
  26. #
  27. # options: array of command line options to pass to Qemu
  28. #
  29. def boot(self, arch, kernel=None, kernel_cmdline=None, options=None):
  30. if arch in ["armv7", "armv5"]:
  31. qemu_arch = "arm"
  32. else:
  33. qemu_arch = arch
  34. qemu_cmd = ["qemu-system-{}".format(qemu_arch),
  35. "-serial", "stdio",
  36. "-display", "none",
  37. "-m", "256"]
  38. if options:
  39. qemu_cmd += options
  40. if kernel_cmdline is None:
  41. kernel_cmdline = []
  42. if kernel:
  43. if kernel == "builtin":
  44. if arch in ["armv7", "armv5"]:
  45. kernel_cmdline.append("console=ttyAMA0")
  46. if arch == "armv7":
  47. kernel = infra.download(self.downloaddir,
  48. "kernel-vexpress-5.10.202")
  49. dtb = infra.download(self.downloaddir,
  50. "vexpress-v2p-ca9-5.10.202.dtb")
  51. qemu_cmd += ["-dtb", dtb]
  52. qemu_cmd += ["-M", "vexpress-a9"]
  53. elif arch == "armv5":
  54. kernel = infra.download(self.downloaddir,
  55. "kernel-versatile-5.10.202")
  56. dtb = infra.download(self.downloaddir,
  57. "versatile-pb-5.10.202.dtb")
  58. qemu_cmd += ["-dtb", dtb]
  59. qemu_cmd += ["-M", "versatilepb"]
  60. qemu_cmd += ["-device", "virtio-rng-pci"]
  61. qemu_cmd += ["-kernel", kernel]
  62. if kernel_cmdline:
  63. qemu_cmd += ["-append", " ".join(kernel_cmdline)]
  64. self.logfile.write(f"> host cpu count: {os.cpu_count()}\n")
  65. ldavg = os.getloadavg()
  66. ldavg_str = f"{ldavg[0]:.2f}, {ldavg[1]:.2f}, {ldavg[2]:.2f}"
  67. self.logfile.write(f"> host loadavg: {ldavg_str}\n")
  68. self.logfile.write(f"> timeout multiplier: {self.timeout_multiplier}\n")
  69. self.logfile.write("> starting qemu with '%s'\n" % " ".join(qemu_cmd))
  70. self.qemu = pexpect.spawn(qemu_cmd[0], qemu_cmd[1:],
  71. timeout=5 * self.timeout_multiplier,
  72. encoding='utf-8',
  73. codec_errors='replace',
  74. env={"QEMU_AUDIO_DRV": "none"})
  75. # We want only stdout into the log to avoid double echo
  76. self.qemu.logfile_read = self.logfile
  77. # Wait for the login prompt to appear, and then login as root with
  78. # the provided password, or no password if not specified.
  79. def login(self, password=None, timeout=60):
  80. # The login prompt can take some time to appear when running multiple
  81. # instances in parallel, so set the timeout to a large value
  82. index = self.qemu.expect(["buildroot login:", pexpect.TIMEOUT],
  83. timeout=timeout * self.timeout_multiplier)
  84. if index != 0:
  85. self.logfile.write("==> System does not boot")
  86. raise SystemError("System does not boot")
  87. self.qemu.sendline("root")
  88. if password:
  89. self.qemu.expect("Password:")
  90. self.qemu.sendline(password)
  91. index = self.qemu.expect(["# ", pexpect.TIMEOUT])
  92. if index != 0:
  93. raise SystemError("Cannot login")
  94. self.run("dmesg -n 1")
  95. # Prevent the shell from wrapping the commands at 80 columns.
  96. self.run("stty columns 29999")
  97. # Run the given 'cmd' with a 'timeout' on the target
  98. # return a tuple (output, exit_code)
  99. def run(self, cmd, timeout=-1):
  100. self.qemu.sendline(cmd)
  101. if timeout != -1:
  102. timeout *= self.timeout_multiplier
  103. self.qemu.expect("# ", timeout=timeout)
  104. # Remove double carriage return from qemu stdout so str.splitlines()
  105. # works as expected.
  106. output = self.qemu.before.replace("\r\r", "\r").splitlines()[1:]
  107. self.qemu.sendline("echo $?")
  108. self.qemu.expect("# ")
  109. exit_code = self.qemu.before.splitlines()[2]
  110. exit_code = int(exit_code)
  111. return output, exit_code
  112. def stop(self):
  113. if self.qemu is None:
  114. return
  115. self.qemu.terminate(force=True)