How to Emulate Raspberry Pi OS
I’d like to share my technique on emulating a Raspberry Pi OS image, which involves creating an initrd that loads virtio_gpu, allowing graphical emulation with QEMU.
This technique:
- Does not require access to a Raspberry Pi
- Does not modify the image[1]
- Does not emulate Raspberry Pi hardware
- Allows GPU emulation
- Works for a backup image or an image downloaded from the Raspberry Pi website
You will learn:
- How to install Debian (32- or 64-bit arm)
- How to modify the initrd to include virtio_gpu and other modules
- How to boot an unmodified Raspberry Pi image using the modified initrd
Setup
64-bit
If you are not using Debian, you can download the EFI firmware from packages.debian.org.
-M virt
uses generic virtual machine hardware-cpu cortex-a72
is a fast and well-supported CPU-smp $(nproc)
uses the maximum number of threads-m 2G
allocates 2 GB of virtual RAM-device usb-ehci -device usb-kbd
enables virtual keyboard-device virtio-gpu-pci
enables virtual GPU
Network emulation should work by default, but if that’s not the case for you, you can copy the -netdev
and -device
arguments from the following 32-bit section.
32-bit
-M virt
enables generic virtual machine hardware-cpu cortex-a15
is a fast and well-supported cpu-smp $(nproc)
uses the maximum number of threads-m 2G
allocates 2 GB of virtual RAM-netdev ... -device virtio-net-device...
enables network support
Downloads (optional)
If you’re not the DIY type, you can download my kernel and skip to Resizing the Raspberry Pi image:
Installing Debian
You cannot boot the Raspberry Pi’s kernel and initrd with QEMU, so you have to build your own that play nice with QEMU. (Wim, 2023a; 2023b) To do this, you will be installing Debian arm in a virtual machine.
Create the image to which you will install Debian:
64-bit
Download Debian arm64. EFI targets are the easiest to emulate; you simply need to provide a firmware file. (Wookey et al., 2023)
Install Debian and reboot into the new system. Be careful; if you power off the guest, you will not be able to boot back into the system without extra parameters (-kernel
and -initrd
).
32-bit
Download Debian armhf and mount the ISO to access the kernel and initrd.
Adapted from “Run ARM/MIPS Debian on QEMU” (Lazymio, 2021).
Install Debian and reboot. It will reboot back into the installer; to exit, press Ctrl+a x. To boot into the new system, you will have to change the kernel and initrd.
# Host
loop=
Modifying the initrd
Now you need to modify the initrd to load the necessary kernel modules during boot. The virtio_gpu module enables GPU virtualization, while the other modules allow mounting /boot/firmware.
# Debian guest
Mounting debian.img (64-bit)
# Host
loop=
Remounting debian.img (32-bit)
You will need to remount the boot partition to use the updated initrd.
# Host
Resizing the Raspberry Pi image (optional)
If you downloaded an image file from the Raspberry Pi website, you should expand the root file system, as it won’t be done automatically.
rpi_loop=
Booting the Raspberry Pi image
You will see “Display is not active” but within a minute you should have output. You will be prompted to configure the keyboard layout, create a user, etc.
64-bit
32-bit
Congratulations, you’re emulating Raspberry Pi OS with graphics support.
Cleanup
“Cannot open access to console, the root account is locked”
This message appears when the boot fails and tries to start an emergency session, but the root account is locked. To figure out what went wrong, you’ll have to enable the root account.
# Host
rpi_loop=
Reboot into the image, where you will now have access to the emergency shell and can troubleshoot what failed with systemctl --failed
. To enable network access in the emergency shell, run ip link set enp0s1 up && dhclient enp0s1
.
References
- Lazymio. (2021). “Run ARM/MIPS Debian on QEMU.” https://blog.lazym.io/2021/04/16/Run-ARM-MIPS-Debian-on-QEMU/.
- Wim. (2023a). “Raspberry Pi 4 Emulation with QEMU Virt.” https://blog.grandtrunk.net/2023/03/raspberry-pi-4-emulation-with-qemu/.
- Wim. (2023b). StackExchange. https://raspberrypi.stackexchange.com/a/142609.
- Wookey, Wise P., Walton J., Zhu S., Matsumoto R., Staudt J. C., & Thibault S. (2023). “Arm64Qemu.” Debian Wiki. https://wiki.debian.org/Arm64Qemu.
Any changes you make in the guest VM will reflect to the .img file. Copy it to preserve the original. ↩