Why does CLI QEMU use the full terminal width at boot time, but not when using nano or gdb?

I currently am launching a RISC-V qemu session as follows:

/usr/bin/qemu-system-riscv64 -machine virt -m 4G -smp cpus=2 -nographic 
    -bios /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.bin 
    -kernel /usr/lib/u-boot/qemu-riscv64_smode/u-boot.bin 
    -netdev user,id=net0 
    -device virtio-net-device,netdev=net0 
    -drive file=qemu_disk.bin,format=raw,if=virtio 
    -device virtio-rng-pci 
    -vga virtio 

The -vga virtio in particular was necessary to get it to use the full terminal width at boot time, as shown below:
enter image description here

However, if I open an application like nano or gdb from within the qemu VM, it stops using the full width of the enclosing terminal window, as shown below:
enter image description here

How can I tell QEMU to behave as if it has more space, so that applications like nano and gdb recognize this when launched?

(Note: I subsequently found I can install the "resize" tool, and manually run it, but I’m wondering if there’s a way to just have qemu handle this automatically.)

Asked By: JRR


Answering "why":

nano and gdb use the same information that stty shows, since that usually is good enough.

The first screenshot shows only output from programs that don’t attempt to use the terminal’s characteristics, while the second shows an attempt — and lack of underlying configuration information — so you get the default (looks like 25×80).

If you look closer, qemu doesn’t own the console, and can’t get good information. For instance, it might actually be connected via a pseudo terminal. That’s easy to see in other programs. Here’s an example where I’m using screen. The stty program gives the right size, but lets me pretend that it’s a different size. That (in reverse) is what resize does: calculate the correct size using the terminal’s escape sequences and update the settings used by stty.

example of stty ignored

If qemu doesn’t have a workaround built-in, using resize (or something like it) is the solution.

Answered By: Thomas Dickey
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.