2
1

emulator.py 5.0 KB

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