x86 Raspberry Pi Emulator using QEMU: Run Raspbian


Recently, I had to run the ARM version of Raspbian on my mac in order to do some development. However, I couldn't find a tutorial that was up to date - all tutorials seemed to use the -net option for QEMU, which is depreciated.

So in this blog post, I will explain how to install the ARM version of Raspbian on an x86 machine using QEMU 3.1.0. I will also show you how to set up networking so you can access the emulated machine using your terminal.

First, donwload the latest image of Raspbian - I would use Raspbian lite, as the desktop environment is rather slow. You can get the raspbian image here.

Next, download a patched linux kernel from this github repository - we need this, as the default kernel does not load inside QEMU.

With that done, make sure that the package qemu-system-arm is installed on your machine. Create a new script and put the following content inside:

sudo qemu-system-arm \
-kernel ./kernel-qemu-xxx-jessie \
-append "root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash" \
-hda raspbian-stretch-lite.img \
-cpu arm1176 -m 256 \
-M versatilepb \
-no-reboot \
-nic user,id=n0 \
-netdev user,id=n0,hostfwd=tcp::5022-:22 \
-serial stdio

Replace the argument to -kernel with the correct path to your patched kernel, and the argument to -hda with your raspbian image. This script will start up the guest OS and run /bin/bash so we can finish setting it up.

Once it has booted, run the following commands inside the guest operating system:

sed -i -e 's/^/#/' /etc/
sed -i -e 's/^/#/' /etc/fstab

This modifies how the hard drives are loaded, so that we can make some changes to the file system. In particular, we want to be able to expand the size of the raspbian .img file, so that there is space for your own files inside the guest operating system.

So shut QEMU down. Then, from the command line, run

qemu-img resize raspbian-stretch-lite.img +2G

This will increase the size of the disk image by 2GB. You can also increase it by more if you want.

Before you start QEMU back up, you need to modify the start script: Remove the init=/bin/bash from the third line, so that we boot properly this time. Once it has booted, you should be able to ssh from your host operating system using the command ssh -p 5022 pi@localhost with the password raspberry. If that does not work, you may have to enable ssh first, see this stackoverflow answer.

Once you are inside once again, we need to resize the partitions so that we can use the full space available on the .img file. To do this, run sudo fdisk sda. This will open the fdisk utility.

Inside fdisk, type p to view the current partitions. It should show /dev/sda1 and /dev/sda2. Remember the start position of sda2. Then type d, followed by 2, to delete the second partition.

Then type n to start the creation of a new partition. Type p to select "primary", then enter the start sector I told you to remember a few seconds ago. After that, just hit enter to make it fill the rest of the .img file. Finally, type w to write the changes.

Next, reboot the virtual machine. Once that's done, type sudo resize2fs /dev/sda2, and your partition will be reisized!

That's pretty much all you need to know to get raspbian running on you x86 machine. Note that it is extremely slow, but in a pinch, it will do!

By Hannes Hertach