๐ง What Is QEMU?#
QEMU (Quick EMUlator) is a generic and open-source emulator and virtualizer.
๐งฐ QEMU Has Two Main Modes:#
Emulation Mode:
- Emulates one hardware architecture on another (e.g., ARM on x86).
- No hardware acceleration โ slower but portable.
Virtualization Mode (with KVM):
- Runs guest OSes using your CPU’s virtualization features (Intel VT-x, AMD-V).
- Requires
KVMand a Linux host. - Much faster, near-native performance.
๐ฅ๏ธ Real-World Use Cases#
| Use Case | Description |
|---|---|
| OS Development | Test kernels or bootloaders without rebooting your PC. |
| Embedded Development | Emulate ARM, RISC-V, MIPS boards on x86. |
| CI Testing | Run headless QEMU in GitHub Actions, Jenkins, etc. |
| Server Virtualization | Lightweight VMs for dev/staging. |
| Try New OS | Test Linux/BSD distros or even Windows without touching your host. |
โ Installing QEMU in Detail#
๐ง On Ubuntu/Debian:#
sudo apt update
sudo apt install qemu qemu-kvm libvirt-daemon-system virtinst bridge-utils virt-managerqemu: Core QEMU binariesqemu-kvm: KVM support for accelerationlibvirt-*: Tools for managing VMsvirt-manager: GUI tool to manage virtual machines
๐ Verify Installation:#
qemu-system-x86_64 --version๐๏ธ Step-by-Step VM Creation (x86 Example)#
๐น Step 1: Create a virtual disk#
qemu-img create -f qcow2 myvm.qcow2 20Gqcow2: QEMU’s optimized disk format (supports snapshots, compression)20G: Disk size
๐น Step 2: Download an ISO (e.g., Ubuntu, Debian, TinyCore, etc.)#
Example:
wget https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-*.iso๐น Step 3: Start VM with ISO#
qemu-system-x86_64 \
-hda myvm.qcow2 \
-cdrom debian.iso \
-boot d \
-m 2048 \
-smp 2 \
-enable-kvm \
-cpu host| Option | Meaning |
|---|---|
-boot d | Boot from CD-ROM |
-m 2048 | 2GB RAM |
-smp 2 | 2 CPU cores |
-enable-kvm | Enables hardware acceleration |
-cpu host | Uses your host’s CPU features in the VM |
๐ก Running QEMU Without GUI (Headless)#
๐ฆ -nographic#
qemu-system-x86_64 -hda myvm.qcow2 -m 1024 -nographic- Redirects serial output to your terminal.
- Useful for server OS installs.
Combine with serial monitor:#
-serial mon:stdioExample:
qemu-system-x86_64 -hda myvm.qcow2 -m 1024 -nographic -serial mon:stdio๐ Networking Setup#
๐งพ Basic NAT Networking#
-net nic -net userAdds a virtual NIC and uses user-mode NAT networking.
๐ Port Forwarding (e.g., SSH into VM):#
-net nic -net user,hostfwd=tcp::2222-:22Then SSH into guest:
ssh -p 2222 user@localhost๐ Snapshot Support#
qemu-system-x86_64 -hda myvm.qcow2 -snapshot- All changes are temporary.
- Use for testing risky software/configs.
๐ Sharing Files Between Host and Guest#
Using 9p VirtIO (Linux guest):#
On host:#
qemu-system-x86_64 \
-hda myvm.qcow2 \
-net nic -net user \
-virtfs local,path=/home/user/shared,security_model=passthrough,mount_tag=hostshareInside guest:#
sudo mount -t 9p -o trans=virtio hostshare /mntโ๏ธ Emulating Other Architectures#
๐งฌ ARM Example:#
qemu-system-arm -M versatilepb -m 256 \
-kernel kernel-qemu -hda raspbian.img \
-append "root=/dev/sda2 rootfstype=ext4 rw" \
-serial stdio -display none-M versatilepb: Emulated ARM board-kernel: Bootloader kernel image-append: Boot parameters
๐ RISC-V Example:#
qemu-system-riscv64 \
-machine virt \
-nographic \
-m 1G \
-kernel bbl \
-drive file=rootfs.ext2,format=raw,id=hd0 \
-device virtio-blk-device,drive=hd0๐ Disk Image Management#
Convert a raw disk to qcow2:#
qemu-img convert -f raw -O qcow2 disk.img disk.qcow2Resize a disk:#
qemu-img resize myvm.qcow2 +10G๐ Secure and Isolated Testing#
You can run potentially dangerous software inside a QEMU VM with -snapshot and no network:
qemu-system-x86_64 -hda malware.qcow2 -snapshot -net none๐งช Bonus: Run QEMU in Daemon Mode#
Run VM in the background:
qemu-system-x86_64 -hda myvm.qcow2 -m 1024 -daemonizeControl VM via QEMU monitor socket or SSH.
๐ Stop and Kill QEMU VMs#
List QEMU instances:
ps aux | grep qemuKill them:
kill <pid>๐ QEMU vs VirtualBox vs KVM#
| Feature | QEMU | VirtualBox | KVM |
|---|---|---|---|
| GUI | โ (CLI) | โ | โ (with virt-manager) |
| Headless | โ | โ | โ |
| Cross-Arch Emulation | โ | โ | โ |
| Speed | Medium (fast with KVM) | Good | Very fast |
| Snapshot Support | โ | โ | โ |
๐งญ Summary: What You Can Do with QEMU#
โ Run Linux or BSD headlessly โ Emulate ARM/RISC-V boards โ Build custom test environments โ Automate CI pipelines โ Use with Docker for full-stack testing โ Perfect for OS/driver/kernel development
Great! Let’s now dive into advanced QEMU usage, focusing on topics like:
- Custom CPU models
- BIOS/UEFI boot
- TPM passthrough
- Multiple networks/interfaces
- Advanced QMP (QEMU Machine Protocol) and monitor control
- Running QEMU as a systemd service
- Creating templates for reproducible virtual labs
- Automation with cloud-init and preseed
- QEMU performance tuning
๐ง 1. Custom CPU Models and Feature Masking#
Use a specific CPU model:#
qemu-system-x86_64 -cpu Skylake-ClientUse your host CPU and enable all features:#
-cpu host,+vmx,+aes,+sse4.2See available CPU models:#
qemu-system-x86_64 -cpu helpThis is helpful when the guest OS depends on specific features like virtualization, AES instructions, or AVX.
๐งฐ 2. BIOS vs UEFI Boot#
BIOS Boot (Default):#
QEMU uses SeaBIOS by default:
qemu-system-x86_64 -hda disk.qcow2UEFI Boot:#
Use OVMF firmware:
๐ฆ Install OVMF (on Ubuntu):#
sudo apt install ovmfโ Boot with UEFI:#
qemu-system-x86_64 \
-drive if=pflash,format=raw,readonly=on,file=/usr/share/OVMF/OVMF_CODE.fd \
-drive if=pflash,format=raw,file=OVMF_VARS.fd \
-hda disk.qcow2๐ 3. TPM Passthrough (for Secure Boot / Windows 11)#
Step 1: Install swtpm#
sudo apt install swtpmStep 2: Create a TPM socket and launch QEMU:#
swtpm socket --tpm2 --ctrl type=unixio,path=/tmp/swtpm-sock &
qemu-system-x86_64 \
-chardev socket,id=chrtpm,path=/tmp/swtpm-sock \
-tpmdev emulator,id=tpm0,chardev=chrtpm \
-device tpm-tis,tpmdev=tpm0 \
-hda win11.qcow2This makes Windows 11 or secure-boot Linux guests work.
๐ 4. Advanced Networking#
โ Multiple Interfaces:#
-netdev user,id=net0 -device e1000,netdev=net0 \
-netdev user,id=net1 -device rtl8139,netdev=net1๐ Tap + Bridge Networking (host integration):#
- Create a tap interface:
sudo ip tuntap add dev tap0 mode tap
sudo ip link set tap0 up- Attach to QEMU:
-netdev tap,id=net0,ifname=tap0,script=no,downscript=no -device virtio-net,netdev=net0For persistent setup, use bridge-utils or systemd-networkd.
๐งช 5. Monitor, QMP, and Live Control#
โจ๏ธ Human-readable Monitor:#
-monitor stdio๐ง QMP JSON API:#
Enable the machine protocol interface:
-qmp unix:/tmp/qmp-sock,server,nowaitSend QMP commands:#
Use socat:
socat - UNIX-CONNECT:/tmp/qmp-sockThen send:
{ "execute": "query-status" }QMP lets you:
- Hot-plug CPUs, memory, disks
- Query VM info in JSON
- Integrate with libvirt, Ansible
๐งฐ 6. Running QEMU as a Systemd Service#
Create a service file:
[Unit]
Description=QEMU VM
After=network.target
[Service]
ExecStart=/usr/bin/qemu-system-x86_64 -hda /var/lib/vms/ubuntu.qcow2 -m 2048 -enable-kvm -nographic
Restart=always
[Install]
WantedBy=multi-user.targetSave as /etc/systemd/system/qemu-vm.service, then:
sudo systemctl enable --now qemu-vmPerfect for headless servers.
๐ฆ 7. Use Cloud-Init for VM Provisioning#
- Download cloud image:
wget https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img- Create cloud-init ISO:
cloud-localds user-data.img user-data- Boot with cloud-init:
qemu-system-x86_64 \
-drive file=jammy-server-cloudimg-amd64.img,format=qcow2 \
-drive file=user-data.img,format=raw \
-net nic -net user,hostfwd=tcp::2222-:22 \
-enable-kvm -m 2048Cloud-init automates:
- User creation
- SSH key injection
- Package install
- Custom scripts
โก 8. Performance Tuning#
Use virtio devices:#
-device virtio-blk-pci,drive=hd -drive file=vm.qcow2,if=none,id=hdUse cache=none for best disk performance:#
-drive file=vm.qcow2,if=virtio,cache=noneUse hugepages:#
-m 4096 -mem-path /dev/hugepages -mem-preallocEnable hugepages on host:
echo 256 > /proc/sys/vm/nr_hugepages๐ 9. Templates for Lab Automation#
Create a reusable VM template:#
- Install Linux into a qcow2 image.
- Don’t autologin or add user config.
- Use
cloud-initorkickstart/preseedto auto-configure cloned VMs. - Clone with:
qemu-img create -f qcow2 -b base.qcow2 newlab.qcow2Linked clones make dev labs fast and space-efficient.
๐ง 10. Other Tips#
Combine with virt-manager or libvirt for hybrid GUI/CLI use.
Use SPICE instead of VNC for better graphics:
-spice port=5930,disable-ticketingAdd USB devices:
-usb -device usb-host,hostbus=1,hostaddr=2
