‹ back home

Quick and simple VMs with qemu

2022-07-01 #how-to #notes #open-source

I don’t use VMs very often, so there’s no chance I can remember all the dozens of command line flags for qemu.

I end up using virt-manager most of the time. It’s a GUI for managing QEMU (and other VM backends) and has dozens of checkboxes and buttons, which come in handy for really complex virtual hardware configurations where I’d need to know a dozen obscure command line flags to replicate the same results. virt-manager is also very handy for VMs with a graphical interface (e.g.: to test something on Fedora or Ubuntu), but not so much when the only need is a simple console.

After taking less than half an hour of reading, it turns out qemu is far easier than I suspected. Should have done this years ago.

Basic VM

The most basic VM simply starts a console with the alpine ISO:

qemu-system-x86_64 -nographic -drive file=alpine-virt-3.15.4-x86_64.iso,format=raw

By default qemu shows output in a separate window (this is the VM’s “monitor). This is rather annoying because it hijacks mouse, and doesn’t have the terminal’s regular copy-paste support. -nographic prevents qemu from showing a separate window for graphic output and uses the current terminal console instead (it simply connects the current terminal to the VM’s serial port).

CPU and Memory

By default, the VM has a single CPU core and 128MB of RAM. This can be changed very easily:

qemu-system-x86_64 -m 1G -smp cores=2 -drive file=alpine-virt-3.15.4-x86_64.iso,format=raw

Network

The internet is a kinda useful thing, and it’s nice to be able to use it inside the VM. There’s the -net and -netdev flags, but those are really complicated – mostly useful for more advanced configurations. For the most basic setup (userspace NAT), -nic is the “new” flag which makes things dead simple1:

qemu-system-x86_64 -nic user -nographic -drive file=alpine-virt-3.15.4-x86_64.iso,format=raw

After booting, network itself needs to be configured in the guest. For alpine, use:

ip link set eth0 up
udhcpc eth0

Putting it all together

The following example defines CPU, memory and network:

qemu-system-x86_64 \
    -nic user \
    -nographic \
    -m 1G \
    -smp cores=2 \
    -drive file=alpine-virt-3.15.4-x86_64.iso,format=raw

Architecture

QEMU supports A LOT of architectures. For ARM64, use qemu-system-aarch64. Make sure the ISO image’s architecture matches the VM’s architecture.


  1. QEMU Networking Documentation ↩︎

— § —