How to use qemu to run a non-gui OS on the terminal?

You can compile qemu for youself and install it into your home directory. There will be no kernel-mode qemu accelerator, but the qemu will work and the speed will be rather high.

Qemu has two options for non-gui start: http://wiki.qemu.org/download/qemu-doc.html

2.3.4 Display options:

-nographic

  • Normally, QEMU uses SDL to display the VGA output. With this option, you can totally disable graphical output so that QEMU is a simple command line application. The emulated serial port is redirected on the console. Therefore, you can still use QEMU to debug a Linux kernel with a serial console.

-curses

  • Normally, QEMU uses SDL to display the VGA output. With this option, QEMU can display the VGA output when in text mode using a curses/ncurses interface. Nothing is displayed in graphical mode.

Also it can send graphic output to another machine via VNC protocol (-vnc option)


Linux: -append 'console=ttyS0'

That option was also needed for Linux kernel be besides -nographic mentioned by osgx as in a comment:

qemu-system-x86_64 -append 'console=ttyS0' \
                   -initrd rootfs.cpio.gz \
                   -kernel bzImage \
                   -nographic \
                   -serial mon:stdio \
                   

Now you can do the following:

  • Ctrl + A X: exit qemu, see: https://superuser.com/questions/1087859/how-to-quit-the-qemu-monitor-when-not-using-a-gui
  • Ctrl + C: gets passed to the guest

-append 'console=ttyS0' makes QEMU pass the console=ttyS0 kernel command line option to Linux, which tells the kernel to use a serial port instead of the display. The serial port sends characters between host and guest, instead of pixels on a display, and then QEMU can display those characters on the terminal.

-serial mon:stdio is optional in this minimal command, but it is generally a good idea to have around. E.g., it improves behaviour if you also want to add a handy -monitor telnet later on:

  • 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?
  • https://unix.stackexchange.com/questions/167165/how-to-pass-ctrl-c-to-the-guest-when-running-qemu-with-nographic

This can be easily tested with Buildroot qemu_x86_64_defconfig. I've created this is a highly self-contained and automated setup that allows you to try this out easily

Related but with less OS constraints:

  • redirect QEMU window output to terminal running qemu
  • Redirect Qemu console to a file or the host terminal?

Tested on Ubuntu 18.04, QEMU 2.11.1.

FreeBSD

Got it working there too: How to boot FreeBSD image under Qemu


I've struggled this for a while and finally figured out how to get it to work for me:

You need to have SGABIOS installed. Interestingly this BIOS is not included in the debian qemu package, so you need to install it (as the superuser):

apt install sgabios  

Then when you run qemu use the -device option to tell the virtural machine to use the sga output

qemu-system-i386 -nographic -device sga discimage.bin

Voila! works perfectly over ssh with both the monitor and text output sent through stdio. You can access the qemu monitor with C-a c.

cheers, ben