Skip to content

QEMU command-line arguments — the layer under libvirt

A practical tour of qemu-system-x86_64 flags — machine, CPU, memory, disks, networking, devices — for operators who occasionally need to drop below libvirt.

Most of the time libvirt is the right interface — it stores the config, manages the lifecycle, and integrates with virsh and virt-manager. But every operator who works with KVM long enough hits a problem where the answer is “look at the qemu command line libvirt is actually running, and run it directly.” This article is the field guide for that moment: the flag families, the ones you change, and the ones you leave alone.

How to verify

Confirm QEMU is present, check the version, and inspect what machine types and CPUs your build supports. The build determines which devices are even available.

qemu-system-x86_64 --version
qemu-system-x86_64 -machine help | head
qemu-system-x86_64 -cpu help | head
qemu-system-x86_64 -device help 2>&1 | head -30
ps -ef | grep '[q]emu-system' | head -3

The ps line shows what libvirt is invoking for live domains — copy one of those into a file and you have a starting point you know works on your host.

What’s happening

qemu-system-x86_64 is the userspace emulator for x86_64 guests. With KVM, it asks the kernel to execute guest instructions at near-native speed; without it, it falls back to TCG software emulation. Every aspect of a guest — virtual hardware, memory layout, boot order, I/O paths — is set on the command line. There is no config file. libvirt’s job is to take XML and translate it into one of these enormous command lines.

Flags fall into rough families. The “what kind of machine” set (-M, -cpu, -smp, -m) defines the platform. The “what’s plugged in” set (-drive, -netdev + -device, -chardev, -display) defines the peripherals. The “how it boots” set (-bios, -kernel, -initrd, -boot) controls firmware and boot media. The “how it talks to us” set (-monitor, -qmp, -vnc, -serial) sets up control channels.

The procedure

  1. Define the machine. -machine q35,accel=kvm is the modern PCIe-based chipset for new guests; -machine pc,accel=kvm is the legacy i440FX. Always pin a machine version (q35-7.2) for production so guest hardware doesn’t shift under you when QEMU is upgraded:

    qemu-system-x86_64 \
      -machine type=q35,accel=kvm,vmport=off \
      -cpu host -smp cores=2,threads=1,sockets=1 \
      -m 2048
  2. CPU model — host vs named. -cpu host exposes every feature the physical CPU has. Fine for a non-clustered host; on a cluster where guests live-migrate, pin a named CPU model that all hosts share (-cpu Haswell-noTSX) so guest features don’t disappear after a migration.

  3. Disks — the slow way and the fast way. Two flag styles exist: the legacy -drive and the modern -blockdev + -device. -drive is concise but conflates the backend (the file) with the frontend (the virtual device). For production, use the split form:

    -blockdev driver=qcow2,node-name=disk0,file.driver=file,file.filename=/var/lib/libvirt/images/web01.qcow2 \
    -device virtio-blk-pci,drive=disk0,id=virtio-disk0
  4. Network — netdev + device. Same backend/frontend split. The backend (-netdev) is the host side (tap into a bridge, user-mode, vhost); the device (-device) is what the guest sees:

    -netdev tap,id=net0,ifname=tap0,script=no,downscript=no,vhost=on \
    -device virtio-net-pci,netdev=net0,mac=52:54:00:11:22:33,id=net0dev

    script=no means QEMU does not bring up or down the tap interface — your host script (or libvirt) does. vhost=on offloads the data path to a kernel module for a meaningful throughput boost.

  5. Console and monitor — how you control the guest. QMP (the JSON monitor) is what tools speak; the HMP text monitor is what humans use to poke at a running guest. Always set both:

    -qmp unix:/tmp/web01.qmp,server,nowait \
    -monitor unix:/tmp/web01.mon,server,nowait \
    -serial mon:stdio

    mon:stdio muxes the serial console and monitor onto stdin/stdout — useful for one-shot tests; production wants separate Unix sockets.

  6. Display — VNC or none. Headless cloud images don’t need a display; Windows installers do. SPICE has nicer remote-desktop semantics but VNC is universally available:

    -display none -vga none                      # truly headless
    -vnc unix:/tmp/web01.vnc -vga virtio         # VNC over Unix socket
  7. Inspect what libvirt actually runs. When a libvirt XML produces a guest that misbehaves, dump the QEMU args:

    sudo cat /var/log/libvirt/qemu/web01.log | head -50
    # Or: ps -ef | grep '[q]emu-system' | tr ' ' '\n' | less

    Copy the line, simplify it (drop -name guest=..., drop unused chardev sockets), and run it by hand. You can now twiddle one flag at a time.

Operational notes

  • -cpu host is great for one-host setups and a footgun on clusters — pick a baseline CPU model that survives live migration across the whole fleet.
  • Mixing -drive and -blockdev styles in the same invocation works but is confusing; pick one and stay there.
  • vhost=on on tap netdevs requires the vhost_net kernel module loaded on the host; without it you get poor network performance and no error.
  • The QEMU monitor (-monitor) over TCP is dangerous unless bound to localhost — anyone who reaches the port can quit the guest or migrate it elsewhere.
  • Machine type aliases (q35 without a version) follow the QEMU upgrade — a live-migrated guest can land on a different QEMU and end up on a different machine version. Pin versions for stable VM identities.

Stack Harbor keeps the raw QEMU command line in mind even when libvirt is the daily driver — when a guest behaves weirdly, the answer is often in the underlying -machine or -cpu flag, not in the libvirt XML. The clients who run KVM at scale get that diagnostic literacy as part of how we operate; the lifecycle is covered in Managed Operations.