Sitemap

Qemu Tuning

3 min readMay 7, 2024

--

side questing

The beauty of Qemu is how fast it can be and the ability to use it via terminal (which means scripting can be done easier and, as long as qemu is installed, I can take use it anywhere). Naturally, there is much more great things that Qemu has going for it but in this blog, I am going to be focusing more on how to make it really fast when it comes to the doing

The major performance boosts can come from using 4 options: -enable-kvm, -m <MEM>G , -cpu host , -smp $(nproc). My recommendation is to take a look at https://wiki.gentoo.org/wiki/QEMU/Options for some inspiration.

-enable-kvm

This option enables KVM (Kernel-based Virtual Machine) full virtualization support. In essence, this allows Qemu to take advantage of KVM acceleration, making it run like it was on bare-metal and utilizing hardware virtualization features of various processors.

-m <MEM>G

This option specifies the memory that Qemu will use.

-cpu host

This will emulate the host processor, meaning it will do a host passthrough. This can be used to squeeze the last bit of major performance out of Qemu. If possible, it’s more worth to increase -smp and/or -m <MEM>G , first.

-smp $(nproc)

This specifies the number of cores that Qemu is allowed to use.

My QEMU Configurations

My Qemu Configurations on based on using Linux, so once the base of the configuration is built out I can script some quality of life improvement. Making it so that I can, within a couple of seconds, log into my Virtual Machine running whatever variant of Linux I want to play with, on whatever architecture* (Mileage will vary when it comes to more niche architectures).

# Example of the static values used in the config

LOC="$(realpath $0 | rev | cut -d'/' --complement -f1 | rev)"
MEM="4G"
CORES="2" # on linux, you can replace this line with CORES="$(nproc)"
IMAGE="${LOC}/debian.qcow2"
DISPLAY_OPTS="gtk,gl=on,show-cursor=on"

The $LOC variable is responsible for normalizing the path of the qcow2 file, the other stuff are examples of what one might put in that place based on the templates below.

Get Andres O’s stories in your inbox

Join Medium for free to get updates from this writer.

Performance

# Performance
qemu-system-x86_64 -m ${MEM} -enable-kvm -cpu host \
-smp ${CORES} -machine pc,accel=kvm \
-drive file=${IMAGE} \
-vga virtio \
-display ${DISPLAY_OPTS} \
-boot c

Generally, my config looks like this unless I am doing some extra special that will require isolation or a more custom networking setup.

Isolation

# Isolation 
qemu-system-x86_64 -m ${MEM} \
-smp ${CORES} -machine pc,accel=kvm \
-drive file=${IMAGE} \
-vga virtio \
-display ${DISPLAY_OPTS} \
-boot c \
-nic none

This will make Qemu not emulate a NIC (Network Interface Card), effectively disabling your Virtual Machine from making any contact to the greater network. Networking in Qemu looks something like this: https://wiki.qemu.org/Documentation/Networking .

One note, I vaguely remembering reading that using -enable-kvm & -machine pc,accel=kvm together is redundant. So just using -enable-kvm should have the intended effect, however, I can not find anything that can prove or disprove that stance.

Resources

--

--