Disclaimer: A Note on QEMU Version Coverage and Timeliness
This blog post was initiated based on observations of QEMU version v10.2.1. Please be aware that the information presented here may not align with updates in more recent or future releases in the official QEMU repository. Due to the fast-moving nature of technology and aggressive software development cycles, I find it necessary to provide this disclaimer to prevent any potential misinformation going forward.
Running a VM with QEMU can be done in several ways, depending on your needs and the type of virtualization you want to perform. In this blog post series, we will be running Ubuntu and Raspberry Pi OS with 64-bit ARM QEMU System Emulation.
Ubuntu
1. Download Ubuntu Server install image for ARM
wget https://cdimage.ubuntu.com/releases/22.04/release/ubuntu-22.04.5-live-server-arm64.iso
2. Prepare the Disk Image
We need to introduce the read-only UEFI variables file to QEMU as a virtual machine firmware. For that reason we will use the /opt/homebrew/share/qemu/edk2-aarch64-code.fd from the MacOS host machine (If you are using Linux, you can use the /usr/share/AAVMF/AAVMF_CODE.fd). On the other hand, we will need the copy of /opt/homebrew/share/qemu/edk2-arm-vars.fd as a writable UEFI variables file. We copy it to the current directory to avoid modifying the original file. We will use this copied file as writable UEFI variables file.
2.1. Copy the UEFI variables file (MacOS)
cp /opt/homebrew/share/qemu/edk2-arm-vars.fd ./edk2-arm-vars-copy.fd
2.2. Create the Disk Image
We create a disk image for the VM. The size of the disk image is 40G. We use the qcow2 format, which is a disk image format for QEMU. It is a copy-on-write format, which means that it is a copy of the original disk image. This is useful for saving space, as it only stores the changes made to the disk image.
qemu-img create -f qcow2 ubuntu_disk.qcow2 40G
3. Run the VM
qemu-system-aarch64 \
-machine virt,accel=hvf,highmem=on \
-cpu host -smp 4 -m 4G \
-drive if=pflash,format=raw,readonly=on,file=/opt/homebrew/share/qemu/edk2-aarch64-code.fd \
-drive if=pflash,format=raw,file=./edk2-arm-vars-copy.fd \
-drive file=./ubuntu_disk.qcow2,if=virtio,format=qcow2 \
-cdrom ./ubuntu-22.04.5-live-server-arm64.iso \
-boot menu=on -boot order=d \
-device virtio-gpu-pci -display cocoa \
-device qemu-xhci,id=xhci \
-device usb-kbd,bus=xhci.0 \
-device usb-tablet,bus=xhci.0 \
-serial stdio
4. Install Ubuntu Server
Shell> fs0:
FS0:\> ls
FS0:\> cd EFI\BOOT
FS0:\EFI\BOOT> ls
FS0:\EFI\BOOT> BOOTAA64.EFI
5. Install Ubuntu Desktop (Optional)
After the installation of Ubuntu Server, we can install Ubuntu Desktop by running the following command:
sudo apt update
# Option 1: Install Ubuntu Desktop (GNOME Desktop)
sudo apt install ubuntu-desktop -y
# Option 2: Install a minimal desktop environment
sudo apt install --no-install-recommends ubuntu-desktop -y
# Option 3: Install lightweight desktop environment (XFCE)
sudo apt install xubuntu-desktop -y
sudo reboot
Raspberry Pi OS
1. Download Raspberry Pi OS install image for ARM
wget https://downloads.raspberrypi.com/raspios_arm64/images/raspios_arm64-2024-11-19/2024-11-19-raspios-bookworm-arm64-lite.img.xz
2. Prepare the Disk Image
2.1. Extract the ISO
unxz 2024-11-19-raspios-bookworm-arm64-lite.img.xz
2.2. Resize the Disk Image
qemu-img resize 2024-11-19-raspios-bookworm-arm64-lite.img 8G
2.3. Prepare the Kernel and Device Tree
Unlike Ubuntu, Raspberry Pi OS does not use UEFI. Instead, it uses a custom bootloader called kernel8.img to boot the kernel and bcm2710-rpi-3-b.dtb to boot the device tree blob. We need to copy the kernel and device tree files from the disk image to the current directory.
# Write the image as read-only and get the disk number
DEVICE=$(hdiutil attach -imagekey diskimage-class=CRawDiskImage 2024-11-19-raspios-bookworm-arm64-lite.img | grep -Eo '^/dev/disk[0-9]+' | head -1)
# Mount the FAT32 boot partition (partition 1)
diskutil mount ${DEVICE}s1
# Mount the boot partition dynamically and copy the files
MOUNT_POINT=$(find /Volumes -maxdepth 1 -name "boot*" | head -1)
cp $MOUNT_POINT/kernel8.img .
cp $MOUNT_POINT/bcm2710-rpi-3-b.dtb .
# Unmount the partition and release the disk
diskutil unmount ${DEVICE}s1
hdiutil detach $DEVICE
3. Run the VM
qemu-system-aarch64 \
-M raspi3b \
-cpu cortex-a72 \
-smp 4 \
-m 1G \
-kernel kernel8.img \
-dtb bcm2710-rpi-3-b.dtb \
-sd 2024-11-19-raspios-bookworm-arm64-lite.img \
-append "rw earlyprintk loglevel=8 console=tty1 dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootdelay=5" \
-device usb-net,netdev=net0 \
-netdev user,id=net0,hostfwd=tcp::2222-:22 \
-display cocoa \
-device usb-mouse \
-device usb-kbd
Common Options
Here are some common options that can be used with QEMU:
| Option | Description |
|---|---|
-m <size> |
Sets the amount of RAM to allocate to the VM. The size can be specified in MB or GB (e.g., -m 2G). |
-hda <file> |
Specifies the hard disk image to use. The file can be a raw disk image or a QCOW2 image. |
-cdrom <file> |
Specifies the CD-ROM image to use. |
-boot <device> |
Specifies the boot device. The device can be d for CD-ROM, c for hard disk, or n for network. |
-netdev <type>,<options> |
Configures the network interface. Common types include user, bridge, and tap. |
-display <type> |
Configures the display. Common types include sdl, gtk, and vnc. |
-cpu <type> |
Specifies the CPU type. Common types include qemu64, qemu32, and host. |
-smp <cores> |
Specifies the number of CPU cores to allocate to the VM. |
-drive <options> |
Configures a storage drive. This is a more flexible option than -hda and can be used to specify different drive types and formats. |
-device <type>,<options> |
Adds a device to the VM. Common types include virtio-net-pci, virtio-blk-pci, and usb-tablet. |
Examples
Here are some examples of how to use QEMU to run VMs with different configurations:
Example 1: Running a VM with KVM Acceleration
qemu-system-aarch64 -enable-kvm -m 2G -hda disk.qcow2 -cdrom cd.iso -boot d
This command will start a VM with KVM acceleration, which can significantly improve performance.
Example 2: Running a VM with a Network Interface
qemu-system-aarch64 -enable-kvm -m 2G -hda disk.qcow2 -netdev user,id=net0 -device virtio-net-pci,netdev=net0
This command will start a VM with a network interface that is connected to the host's network.
Example 3: Running a VM with a Graphical Display
qemu-system-aarch64 -enable-kvm -m 2G -hda disk.qcow2 -display sdl
This command will start a VM with a graphical display that is connected to the host's display.
Example 4: Running a VM with Multiple CPU Cores
qemu-system-aarch64 -enable-kvm -m 2G -smp 2 -hda disk.qcow2 -cdrom cd.iso -boot d
This command will start a VM with 2 CPU cores.
Example 5: Running a VM with a USB Tablet
qemu-system-aarch64 -enable-kvm -m 2G -hda disk.qcow2 -display sdl -device usb-tablet
This command will start a VM with a USB tablet, which provides better mouse support.
Common Issues
- Performance issues: QEMU can be slow when running VMs, especially when running VMs on a different architecture than the host. To improve performance, use KVM acceleration when possible.
- Network issues: Network interfaces can be difficult to configure. To simplify network configuration, use the
usernetwork mode or a bridge network. - Display issues: The display can be difficult to configure. To simplify display configuration, use the
sdlorgtkdisplay modes. - USB issues: USB devices can be difficult to configure. To simplify USB configuration, use the
usb-tabletdevice. - Raspberry Pi OS issues: Raspberry Pi OS can be difficult to configure. To simplify Raspberry Pi OS configuration, use the
raspi3bmachine type and thekernel8.imgandbcm2710-rpi-3-b.dtbfiles. - Raspberry Pi Desktop issues: There are some bugs and missing features in the current version of the raspberry pi in qemu machines. For example, the graphical display is not working properly. That's why I recommend using the lite version of the OS and raspi3b machine type.
Conclusion
Running a VM with QEMU can be a powerful way to test and develop software on different hardware architectures. While the command-line interface can be complex, there are many tools available to simplify the process. By understanding the common options and issues, you can effectively use QEMU to run VMs for your specific needs.