Introduction
QEMU (Quick Emulator) has become an essential tool for developers, system administrators and enthusiasts who need to run different architectures or operating systems within a Linux environment. Its ability to emulate complete hardware and, at the same time, to take advantage of acceleration by KVM, places it as a versatile solution for both light tests and productive workloads.
What is QEMU?
QEMU is an open source emulator and virtualizer that can reproduce the behavior of various processors (ARM, x86, PowerPC, MIPS, etc.) and peripheral devices. Unlike traditional hypervisors, QEMU does not depend on a specific hardware abstraction layer; instead, it translates the guest code instructions to the host in real time. When combined with KVM (Kernel-based Virtual Machine), QEMU delegates the execution of the CPU to the kernel module, achieving a performance close to the native one.
Installation in popular distributions
In most Linux distributions, QEMU is in the official repositories. In Ubuntu or Debian, just run:
sudo apt update && sudo apt install qemu qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils
In Fedora, the equivalent command is:
sudo dnf install @virtualization
For Arch Linux, it is used:
sudo pacman -S qemu virt-manager dnsmasq vde2 ebtables iptables
After installation, it is recommended to add your user to the groupkvmandlibvirtto avoid the use ofsudoin each command:
sudo usermod -aG kvm libvirt $USER
newgrp kvm
newgrp libvirt
Emulation mode vs. KVM acceleration
QEMU can operate in two main ways:
- Pure emulation mode (TCG): Translate each instruction of the guest code using the Tiny Code Generator. It is useful when you need to emulate a different architecture than the host (for example, run ARM on a x86 _ 64 computer). The performance is lower, but the flexibility is maximum.
- Accelerated mode with KVM: When the guest architecture matches that of the host and the processor supports virtualization extensions (Intel VT-x or AMD-V), QEMU delegates the execution of the CPU to the KVM kernel module. This significantly reduces overload and allows to achieve almost native performance.
To check if KVM is available, run:
kvm-okIf the result indicates that KVM acceleration can be used, you are ready to take advantage of the acceleration.
Create and launch a basic virtual machine
The typical process involves creating a disk image, downloading an installation ISO and running QEMU with the right parameters. Below is an example to install Ubuntu 22.04 in a VM x86 _ 64 with KVM acceleration:
# Crear un disco virtual de 20 GB en formato qcow2
qemu-img create -f qcow2 ubuntu22.04.qcow2 20G
# Iniciar la instalación
qemu-system-x86_64 \
-enable-kvm \
-m 4096 \
-smp 4 \
-cpu host \
-drive file=ubuntu22.04.qcow2,format=qcow2,if=virtio \
-cdrom ubuntu-22.04-live-server-amd64.iso \
-boot d \
-netdev user,id=net0,hostfwd=tcp::2222-:22 \
-device virtio-net-pci,netdev=net0 \
-vga virtio
The most important flags are:
-enable-kvm: activates acceleration.-m: amount of RAM in MB.-smp: number of virtual CPU nuclei.-drive ... if=virtio: uses the virtio controller for better disk performance.-netdev user,...: sets up a NAT network with port forwarding (e.g. SSH in port 2222 of the host).
Once the installation is completed, you can start the VM without the ISO:
qemu-system-x86_64 -enable-kvm -m 4096 -smp 4 -cpu host -drive file=ubuntu22.04.qcow2,format=qcow2,if=virtio -netdev user,id=net0,hostfwd=tcp::2222-:22 -device virtio-net-pci,netdev=net0 -vga virtio
Advanced Network and Storage Options
For more complex production or testing environments, QEMU offers multiple types of storage networks and backends:
- Bridge-type networks: They allow VM to appear as another device in the same physical network, ideal for services that must be accessible from other machines.
- Vhost-user-type networks: They offer high performance by transferring packages directly between QEMU and a user space process (often used with DPDK).
- Storage in row or qCow2 format: While raw provides maximum speed, qCow2 adds features such as snapshots, compression and encryption.
- Cloned disks and snapshots: With
qemu-img create -b base.qcow2 -f qcow2 delta.qcow2Different images can be created that facilitate rapid testing and rollbacks.
An example of VM connected to a bridge calledbr0:
qemu-system-x86_64 -enable-kvm -m 4096 -smp 2 -drive file=win10.qcow2,if=virtio -netdev bridge,id=net0,br=br0 -device virtio-net-pci,netdev=net0
Debugging and monitoring
QEMU includes an integrated monitor accessible byCtrl+Alt+2(in the graphic console) or by a TCP / UNIX socket. From the monitor you can inspect the status of the CPU, record device information and send commands in real time. Some useful commands are:
info cpus: shows the status of each vCPU.info snapshots: list existing snapshots.system_powerdown: Turn off the VM in an orderly manner.quit: exits the monitor and stops the VM.
For more detailed monitoring, you can enable the registration of events with-d int,guest_errors,unimpor use external tools such asvirsh(if libvirt is involved) orperfto analyze the use of host CPU.
Conclusions
QEMU remains a key part of the Linux virtualization ecosystem thanks to its flexibility, its ability to emulate multiple architectures and its close integration with KVM to achieve a performance close to the native one. Whether you need to test an experimental kernel, run a legacy application in a different architecture or mount a complex network laboratory, QEMU offers the tools needed to do it safely and efficiently. With the proper practice of your network, storage and purification options, you can adapt the solution to virtually any development or production scenario.


