QEMU Configuration & Usage
Here I collect some QEMU options I have found useful beyond the basics.
- Machine Type (x86)
 - 
-M q35configures a more modern chipset to be emulated. The Q35 chipset supports PCI-e and includes an AHCI controller [1]. - UEFI
 - 
Get UEFI support by replacing the bios using
-bios ./OVMF-pure-efi.fdorOVMF-with-csm.fdif legacy boot is desired.OVMF can be downloaded from https://www.kraxel.org/repos/jenkins/edk2/ (pick the one that says ovmf-x64). bsdtar can extract the rpms.Many distributions also offer a matchingovmfpackage in their repos.If you need UEFI settings (such as display resolution) to be persisted, a copy of
OVMF_VARS(one per VM) needs to be provided too: - Attaching disk images using VirtIO
 - 
Use
-drive file=disk.img,if=virtiofor improved disk performance. Windows guests require additional drivers [2] to use this. - Disabling disk cache flush
 - 
If you are installing a VM to quickly test something, disabling flushing of write cache to disk can speed up the process immensely:
-drive file=foobar.qcow2,if=virtio,cache=unsafe(orcache.no-flush=on)Caution: Never use this in a production environment or with any VMs or data you care about. The guest OS will be tricked into believing it has safely written data to disk, when in reality it could be lost at any moment.
 - Attaching raw disks
 - 
-drive file=/dev/sdb,if=virtio,format=raw,cache=noneWhen attaching entire disks, partitions or logical volumes
cache=noneis a good idea. - Share host directory to guest
 - 
-drive file=fat:/path/to/dir,snapshot=oncreates a read-only virtual FAT-formatted disk image from the given directory. - Multiple CD-ROM images
 - 
-drive file=X.iso,index=0,media=cdrom -drive file=Y.iso,index=1,media=cdromThe
index=Nparameter is optional but can be used to explicitly order drives. - Bridged Network Adapter
 - 
-netdev bridge,br=br0,id=mynet -device virtio-net-pci,netdev=mynetshort syntax:-nic bridge,br=br0,model=virtioFor virtio, Windows needs additional drivers [2].Aside fromvirtio-net-pciQEMU also supports emulating real cards such as:e1000e(Intel 82574L GbE) which is the default,e1000(Intel 82540EM GbE) orrtl8139(Realtek RTL-8139C 10/100M) - CPU type
 - 
The default is
-cpu qemu64.To get the full CPU feature set in the guest use-cpu hostor the appropriate family, e.g.-cpu Haswell.Alternatively, flags can also be enabled individually:-cpu qemu64,+ssse3,+sse4.2,+avx,+avx2-cpu kvm64is legacy and should not be used [3]. - VNC
 - 
-display vnc=localhost:1,lossy=onstarts VNC on port 5901 (no authentication, but localhost only) with JPEG compression enabled to save bandwidth. - io_uring
 - 
io_uring is supported by Linux 5.1 and newer (on the host) and can further raise disk performance:
-drive file=disk.qcow2,aio=io_uring - USB Input Devices
 - 
-usb -device usb-tablet -device usb-kbdattaches attaches keyboard and tablet (as mouse) via USB instead of PS/2.This improves mouse support especially when using VNC and will stop the GUI from grabbing the input devices.
 - Port forwarding with User networking
 - 
When using
-nic user(default) thehostfwd=PROTO::HPORT-:PORToption can be used to forward connections to the guest.e.g.
-nic user,model=virtio,hostfwd=tcp::2222-:22 - VGA driver
 - 
3D acceleration for Linux guests is possible with
-vga virtio[4]. - Serial console
 - 
-serial ptyconnects the serial port to a PTY, which can then be interacted with usingscreen.Alternatively when-nographicis used, the QEMU monitor and serial get multiplexed to stdio.Ctrl-A ccan then be used to switch between the monitor/serial [5]. - Monitor console
 - 
With either
-nographicor-monitor stdio, QEMU will make its monitor console available in the terminal.It can also be accessed when using the GUI via "View" > "compatmonitor0".The monitor console provides access to lots of functionality including snapshots, migration, device hotplug and debugging.
stoppauses the VM,contresumes execution againinfo registersshows the current values of CPU registersx/x ADDRESSprints the hexadecimal value at the given address in guest memoryx/20i ADDRESSdisassembles 20 instructions at the given address
Detailed explanations can be found in the relevant documentation.
 - Emulated SCSI controller
 - 
(because it's possible, not because it's useful)
-device lsi,id=lsi -drive file=somewhere.img,if=none,id=disk0 -device scsi-hd,drive=disk0,bus=lsi.0 - Emulated USB drive
 - 
This may not seem useful at first look but will help if some software refuses to operate on "system" disks and requires a removable drive to be presented to the VM.
-drive file=whatever.qcow2,if=none,id=disk1 -device usb-storage,drive=disk1As a bonus here's how that looks from the monitor console (hotplug):drive_add 0 file=whatever.qcow2,if=none,id=disk1device_add usb-storage,drive=disk1