boot-qemu-image.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. # This script expect to run from the Buildroot top directory.
  3. import os
  4. import pexpect
  5. import sys
  6. import time
  7. def main():
  8. if not (len(sys.argv) == 2):
  9. print("Error: incorrect number of arguments")
  10. print("""Usage: boot-qemu-image.py <qemu_arch_defconfig>""")
  11. sys.exit(1)
  12. # Ignore non Qemu defconfig
  13. if not sys.argv[1].startswith('qemu_'):
  14. sys.exit(0)
  15. if not os.path.exists('output/images/start-qemu.sh'):
  16. print('qemu-start.sh is missing, cannot test.')
  17. sys.exit(0)
  18. qemu_start = os.path.join(os.getcwd(), 'output/images/start-qemu.sh')
  19. child = pexpect.spawn(qemu_start, ['serial-only'],
  20. timeout=5, encoding='utf-8',
  21. env={"QEMU_AUDIO_DRV": "none"})
  22. # We want only stdout into the log to avoid double echo
  23. child.logfile = sys.stdout
  24. # Let the spawn actually try to fork+exec to the wrapper, and then
  25. # let the wrapper exec the qemu process.
  26. time.sleep(1)
  27. try:
  28. child.expect(["buildroot login:", pexpect.TIMEOUT], timeout=60)
  29. except pexpect.EOF as e:
  30. # Some emulations require a fork of qemu-system, which may be
  31. # missing on the system, and is not provided by Buildroot.
  32. # In this case, spawn above will succeed at starting the wrapper
  33. # start-qemu.sh, but that one will fail (exit with 127) in such
  34. # a situation.
  35. exit = [int(l.split(' ')[1])
  36. for l in e.value.splitlines()
  37. if l.startswith('exitstatus: ')]
  38. if len(exit) and exit[0] == 127:
  39. print('qemu-start.sh could not find the qemu binary')
  40. sys.exit(0)
  41. print("Connection problem, exiting.")
  42. sys.exit(1)
  43. except pexpect.TIMEOUT:
  44. print("System did not boot in time, exiting.")
  45. sys.exit(1)
  46. child.sendline("root\r")
  47. try:
  48. child.expect(["# ", pexpect.TIMEOUT], timeout=60)
  49. except pexpect.EOF:
  50. print("Cannot connect to shell")
  51. sys.exit(1)
  52. except pexpect.TIMEOUT:
  53. print("Timeout while waiting for shell")
  54. sys.exit(1)
  55. child.sendline("poweroff\r")
  56. try:
  57. child.expect(["System halted", pexpect.TIMEOUT], timeout=60)
  58. child.expect(pexpect.EOF)
  59. except pexpect.EOF:
  60. pass
  61. except pexpect.TIMEOUT:
  62. # Qemu may not exit properly after "System halted", ignore.
  63. print("Cannot halt machine")
  64. sys.exit(0)
  65. if __name__ == "__main__":
  66. main()