boot-qemu-image.py 2.3 KB

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