Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run qemu with -nographic and -monitor but still be able to send Ctrl+C to the guest and quit with Ctrl+A X?

Tags:

qemu

I have just found out that if you run QEMU with -monitor telnet::45454,server,nowait -nographic, then Ctrl-C kills the QEMU VM instead of generating SIGINT on the guest: How to pass Ctrl-C to the guest when running qemu with -nographic? | Unix & Linux Stack Exchange

However, I don't want to remove -monitor because it is convenient to automate monitor commands, e.g. it allows me to create a helper script that does:

echo 'savevm my_snap_id' |  telnet localhost 45454

Is there a way to both keep my Ctrl-C and -monitor working with -nographic?

Full QEMU command:

qemu-system-x86_64 -append 'root=/dev/vda console=ttyS0' -kernel 'bzImage' -drive file='rootfs.ext2.qcow2,if=virtio,format=qcow2' -nographic -monitor telnet::45454,server,nowait

On QEMU 2.10.1, Ubuntu 17.10, full QEMU command:

./x86_64-softmmu/qemu-system-x86_64 \
-append "root=/dev/sda console=ttyS0 nokaslr printk.time=y" \
-drive file="${dir}/out/x86_64/buildroot/images/rootfs.ext2.qcow2,format=qcow2" \
-kernel "${dir}/out/x86_64/buildroot/images/bzImage" \
-nographic \

If I add:

-chardev stdio,id=s1,signal=off \
-serial none -device isa-serial,chardev=s1

then Ctrl+C starts working as desired, but Ctrl+A X does not work to quit QEMU, which is annoying sometimes: I can use telnet to quit, but it requires more typing / automation.

http://lists.nongnu.org/archive/html/qemu-discuss/2018-04/msg00006.html


1 Answers

Add -serial mon:stdio and remove other -serial options

The following satisfies all my requirements:

./x86_64-softmmu/qemu-system-x86_64 \
  -append 'root=/dev/vda nopat nokaslr norandmaps printk.devkmsg=on printk.time=y console=ttyS0' \
  -drive file="${dir}/out/x86_64/buildroot/images/rootfs.ext2.qcow2,if=virtio,format=qcow2" \
  -kernel "${dir}/out/x86_64/buildroot/images/bzImage" \
  -nographic \
  -monitor telnet::45454,server,nowait \
  -serial mon:stdio

Or for aarch64:

./aarch64-softmmu/qemu-system-aarch64 \
  -M virt \
  -append 'root=/dev/vda nokaslr norandmaps printk.devkmsg=on printk.time=y' \
  -cpu cortex-a57 \
  -drive file="${dir}/out/aarch64/buildroot/images/rootfs.ext2.qcow2,if=virtio,format=qcow2" \
  -kernel "${dir}/out/aarch64/buildroot/images/Image" \
  -monitor telnet::45454,server,nowait \
  -nographic \
  -serial mon:stdio \

Tested on QEMU 9d2a09063922757ec3640d93f6b35921ab95b1c2 (post v2.12.0-rc2).