pmb.qemu.run: add option to set serial to mon:stdio (MR 1980)

This makes QEMU trap signals like Ctrl-C and send it to the guest
instead of terminating QEMU.

To quit QEMU with this option you can use [Ctrl-A] [x]
  or
[Ctrl-A] [c] and type 'quit' at the prompt.

This behavior is disabled by default, and can be enabled by setting a
new option in the pmbootstrap.cfg (using "pmbootstrap config
qemu_redir_stdio true")

Co-authored-by: Luca Weiss <luca@z3ntu.xyz>
This commit is contained in:
Clayton Craft 2020-10-07 13:56:17 +02:00 committed by Oliver Smith
parent dfe8129640
commit 100fd332df
No known key found for this signature in database
GPG Key ID: 5AE7F5513E0885CB
2 changed files with 21 additions and 5 deletions

View File

@ -78,7 +78,8 @@ config_keys = ["aports",
"work",
"boot_size",
"extra_space",
"sudo_timer"]
"sudo_timer",
"qemu_redir_stdio"]
# Config file/commandline default values
# $WORK gets replaced with the actual value for args.work (which may be
@ -120,7 +121,8 @@ defaults = {
"work": os.path.expanduser("~") + "/.local/var/pmbootstrap",
"boot_size": "256",
"extra_space": "0",
"sudo_timer": False
"sudo_timer": False,
"qemu_redir_stdio": False
}

View File

@ -169,7 +169,11 @@ def command_qemu(args, arch, img_path, img_path_2nd=None):
command += ["-m", str(args.memory)]
command += ["-serial", "stdio"]
command += ["-serial"]
if args.qemu_redir_stdio:
command += ["mon:stdio"]
else:
command += ["stdio"]
command += ["-drive", "file=" + img_path + ",format=raw,if=virtio"]
if img_path_2nd:
@ -339,14 +343,24 @@ def run(args):
logging.info("* (ssh) ssh -p {port} {user}@localhost".format(**vars(args)))
logging.info("* (serial) in this console (stdout/stdin)")
if args.qemu_redir_stdio:
logging.info("NOTE: Ctrl+C is redirected to the VM! To disable this, "
"run: pmbootstrap config qemu_redir_stdio False")
logging.info("NOTE: To quit QEMU with this option you can use "
"Ctrl-A, X.")
# Run QEMU and kill it together with pmbootstrap
process = None
try:
signal.signal(signal.SIGTERM, sigterm_handler)
process = pmb.helpers.run.user(args, qemu, output="tui", env=env)
except KeyboardInterrupt:
# Don't show a trace when pressing ^C
pass
# In addition to not showing a trace when pressing ^C, let user know
# they can override this behavior:
logging.info("Quitting because Ctrl+C detected.")
logging.info("To override this behavior and have pmbootstrap "
"send Ctrl+C to the VM, run:")
logging.info("$ pmbootstrap config qemu_redir_stdio True")
finally:
if process:
process.terminate()