# QEMU (Quick Emulator) Cheat Sheet ## Basic Concepts QEMU is a generic machine emulator and virtualizer that can run operating systems and programs for one machine on a different machine. It can be used in two main modes: - Full system emulation: Emulates a complete computer system, including CPUs and peripherals - User mode emulation: Runs programs compiled for one CPU on another CPU ## Installation ```bash # Debian/Ubuntu sudo apt install qemu-system qemu-utils # RHEL/CentOS/Fedora sudo dnf install qemu-kvm qemu-img # Arch Linux sudo pacman -S qemu qemu-arch-extra # macOS (using Homebrew) brew install qemu ``` ## Image Management ### Creating Disk Images ```bash # Create a raw disk image (20GB) qemu-img create -f raw disk.img 20G # Create a qcow2 disk image (more efficient, supports snapshots) qemu-img create -f qcow2 disk.qcow2 20G # Create a qcow2 with pre-allocation for better performance qemu-img create -f qcow2 -o preallocation=metadata disk.qcow2 20G ``` ### Converting Between Formats ```bash # Convert from raw to qcow2 qemu-img convert -f raw -O qcow2 disk.img disk.qcow2 # Convert from qcow2 to raw qemu-img convert -f qcow2 -O raw disk.qcow2 disk.img # Convert from vmdk (VMware) to qcow2 qemu-img convert -f vmdk -O qcow2 disk.vmdk disk.qcow2 ``` ### Resizing Disk Images ```bash # Resize a qcow2 image (increase by 10GB) qemu-img resize disk.qcow2 +10G # Note: After resizing, you must also resize the filesystem inside the VM ``` ### Image Information ```bash # Get information about an image qemu-img info disk.qcow2 # Check disk image for errors qemu-img check disk.qcow2 ``` ### Snapshots ```bash # Create a snapshot qemu-img snapshot -c snapshot_name disk.qcow2 # List snapshots qemu-img snapshot -l disk.qcow2 # Restore a snapshot qemu-img snapshot -a snapshot_name disk.qcow2 # Delete a snapshot qemu-img snapshot -d snapshot_name disk.qcow2 ``` ## Basic VM Operation ### Starting a VM ```bash # Basic VM launch (x86_64 architecture) qemu-system-x86_64 -hda disk.qcow2 -m 2G # Launch with KVM acceleration (much faster) qemu-system-x86_64 -enable-kvm -hda disk.qcow2 -m 2G ``` ### Installing an OS ```bash # Boot from ISO and install to disk qemu-system-x86_64 -enable-kvm \ -m 2G \ -cdrom os-installer.iso \ -hda disk.qcow2 \ -boot d ``` ### Common Options ```bash -m 2G # Allocate 2GB of memory -smp 4 # Use 4 CPU cores -enable-kvm # Use KVM hardware acceleration -boot c|d|n # Boot from disk (c), CD (d), or network (n) -cdrom file.iso # Connect CD-ROM to ISO file -hda disk.qcow2 # Primary hard disk -hdb disk2.qcow2 # Secondary hard disk -cpu host # Use host CPU model (best performance with KVM) -display vnc=:0 # Use VNC display (connect to localhost:5900) -display gtk # Use GTK display (local window) -device virtio-net-pci # Use virtio network device -nic user,hostfwd=tcp::2222-:22 # User-mode networking with port forwarding ``` ## Networking Options ### User-mode Networking (simplest) ```bash # Basic user-mode networking qemu-system-x86_64 -enable-kvm -hda disk.qcow2 -m 2G -nic user # With port forwarding (host port 2222 -> guest port 22) qemu-system-x86_64 -enable-kvm -hda disk.qcow2 -m 2G \ -nic user,hostfwd=tcp::2222-:22 ``` ### Bridged Networking ```bash # Create a TAP interface (requires root) sudo ip tuntap add dev tap0 mode tap user $USER sudo ip link set tap0 up sudo ip link set tap0 master br0 # Use TAP interface qemu-system-x86_64 -enable-kvm -hda disk.qcow2 -m 2G \ -nic tap,ifname=tap0,script=no,downscript=no ``` ### Complex Network Configuration ```bash # Multiple network interfaces qemu-system-x86_64 -enable-kvm -hda disk.qcow2 -m 2G \ -nic user,hostfwd=tcp::2222-:22 \ -nic tap,ifname=tap0,script=no,downscript=no ``` ## Storage Options ### Drive Options ```bash # Connect disk to IDE bus -hda disk.qcow2 # Connect disk to VirtIO (faster) -drive file=disk.qcow2,if=virtio # Read-only disk -drive file=disk.img,if=virtio,readonly=on # CD-ROM -drive file=os.iso,media=cdrom # Multiple disks with explicit index -drive file=disk1.qcow2,if=virtio,index=0 \ -drive file=disk2.qcow2,if=virtio,index=1 ``` ### Using Physical Devices ```bash # Use a physical disk (requires root) sudo qemu-system-x86_64 -enable-kvm -m 2G -hda /dev/sdb # Use a physical CD/DVD drive qemu-system-x86_64 -enable-kvm -m 2G -cdrom /dev/cdrom -hda disk.qcow2 ``` ## Display and Input Options ### Display Options ```bash # Local GUI window -display gtk # VNC server -display vnc=:0 # Connect to localhost:5900 # Headless (no display) -display none # SPICE remote desktop protocol -spice port=5930,addr=127.0.0.1,disable-ticketing ``` ### Remote Access ```bash # Connect to a QEMU VNC server vncviewer localhost:5900 # Connect to a QEMU SPICE server remote-viewer spice://localhost:5930 ``` ## Advanced Options ### CPU and Memory ```bash # Pin vCPUs to physical cores -smp 4,cores=2,threads=2,sockets=1 \ -cpu host,topoext \ -numa node,nodeid=0,cpus=0-1,mem=2G \ -numa node,nodeid=1,cpus=2-3,mem=2G # Huge pages for better performance -mem-path /dev/hugepages # Memory hotplug -m 4G,slots=3,maxmem=16G ``` ### Device Passthrough (VFIO) ```bash # Pass through a PCI device to VM qemu-system-x86_64 -enable-kvm -m 4G \ -hda disk.qcow2 \ -device vfio-pci,host=01:00.0 ``` ### QEMU Monitor ```bash # Access QEMU monitor (runtime control) -monitor stdio # Common monitor commands: # info status - Show VM status # info cpus - Show CPU info # device_add - Hot-add a device # device_del - Hot-remove a device # system_powerdown - Send ACPI shutdown # system_reset - Reset the VM # quit - Quit QEMU ``` ### QEMU + Libvirt ```bash # Get QEMU command line from libvirt XML virsh domxml-to-native qemu-argv --domain vm_name # Dump QEMU monitor commands for VM virsh qemu-monitor-command vm_name --hmp 'info status' ``` ## Script Examples ### Basic VM Launch Script ```bash #!/bin/bash qemu-system-x86_64 \ -name "My VM" \ -enable-kvm \ -cpu host \ -smp 4 \ -m 4G \ -drive file=disk.qcow2,if=virtio \ -drive file=os.iso,media=cdrom \ -nic user,hostfwd=tcp::2222-:22 \ -display gtk ``` ### Headless Server VM ```bash #!/bin/bash qemu-system-x86_64 \ -name "Server VM" \ -enable-kvm \ -cpu host \ -smp 2 \ -m 2G \ -drive file=server.qcow2,if=virtio \ -nic user,hostfwd=tcp::2222-:22,hostfwd=tcp::8080-:80 \ -display none \ -daemonize ``` ## Debugging and Troubleshooting ```bash # Trace all QEMU operations (very verbose) qemu-system-x86_64 -d in_asm -D qemu.log [other options] # Enable QEMU debug console qemu-system-x86_64 -monitor stdio [other options] # Get QEMU version and supported features qemu-system-x86_64 --version # List available emulated machines qemu-system-x86_64 -machine help # List available CPU models qemu-system-x86_64 -cpu help ``` ## QEMU Guest Agent ```bash # Enable the QEMU Guest Agent qemu-system-x86_64 -enable-kvm \ -hda disk.qcow2 \ -m 2G \ -device virtio-serial \ -chardev socket,path=/tmp/qga.sock,server,nowait,id=qga0 \ -device virtserialport,chardev=qga0,name=org.qemu.guest_agent.0 # Install Guest Agent inside VM (Linux guest) # sudo apt install qemu-guest-agent # sudo systemctl start qemu-guest-agent ```