boot-qemu-image.py 2.2 KB

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