Raspberry Pi emulation

Running ARM Raspian in a QEMU virtual machine.

I am building a Raspberry Pi specific snap package and learned you don't simply build software on an x86 Intel machine and run it on a Raspberry Pi. The Pi has an ARM processor and the software needs to be purposely built for this. Fine, but I don't always have a free Raspberry Pi at hand.

Why not virtualize the Raspberry in a VM on my laptop? No need for a physical Raspberry, no extra hardware to carry around, and I can boot up as many Pi's as I desire (or my laptop can handle).

I used the free and open-source QEMU hypervisor to virtualise the Raspberry hardware. Let’s download and install QEMU:

sudo apt install qemu

QEMU does not (yet) know the Raspberry kernel. I downloaded the most recent kernel from Dhruv VyasQEMU RPi Kernel repository and the versatile-pb.dtb device tree blob from the same repo (not the slightest idea what this does but it won’t run without it).

Only the Linux image itself left. I don’t need a GUI on the Pi, it’s a build machine, so I took the Raspbian Stretch Lite version. Unzipping the image gives a .img file.

With QEMU installed, the patched kernel and dtb file in place, and the Raspbian image it’s time to get the VM spun up:

sudo qemu-system-arm \
  -kernel kernel-qemu-4.9.59-stretch \
  -cpu arm1176 -m 256 \
  -M versatilepb \
  -dtb versatile-pb.dtb \
  -no-reboot \
  -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" \
  -net nic -net user \
  -hda 2018-06-27-raspbian-stretch-lite.img
Screenshot of Raspberry Pi emulation running in QEMU

The user-mode network used in the previous command does not support the ping command out-of-the-box, only TCP and UDP are. I've found better network configurations online, but they look too complex for my little test setup.

This post is heavily based on Alistair Chapman’s blog post with slight modifications to make it work for me.