Find me on Codeberg

How to configure Qemu

Target Pi on Mac or Linux

So even the idea is to run software on the Pi, not everyone has a Pi (yet :-)

Others, like me, prefer to develop on a laptop and not carry the Pi around.

For all those, this here explains how to emulate the Pi on a unix.

Even if you have a Pi, this explains a nice way to develop with it.

We'll more or less follow a blog post i found, with small additions.

Qemu

Get the Qemu. There may be other emulators out there, and i have read of armulator, but this is what i found described and it works and is “easy enough”.

On Mac:

brew install qemu
On Linux with debian/ubuntu:
sudo apt-get qemu-system-arm

Pi images

Create a directory for the stuff on your computer, ie pi.

mkdir pi
pi is in the gitignore, and the scripts in bin, will assume pi.

Get the latest Raspian image. Currently (2019) the buster lite version without Gui is fine, and smaller.

Change the file format with something like:

qemu-img convert -f raw -O qcow2 2019-07-10-raspbian-buster-lite.img raspbian-buster-lite.qcow
So we can then resize the file, without it taking extra space (optional)
qemu-img resize raspbian-buster-lite.qcow +2G
Put the resulting file into the pi directory. After booting you will need to change the partition to take the newly created space. This can be done with
sudo fdisk /dev/sda
but for the buster image there is an extra little problem. A small free space is before the partition, but the new partion must be at the exact sector as the old. So copy the start sector before deleting the root partion and give fdisk the start sector when creatin the new.

Also the file system has to be enlarged after another reboot:

sudo resize2fs /dev/sda2

Kernel

One still needs a kernel (even there is one on the image?), and Druv kindly keeps a repository of them. Since we downloaded the buster image, grab the buster kernel and also the mysterious dtb file. Both go into the pi directory.

Boot

There is quite a bit to the command line to boot the pi. There is a script in the bin directory, but here it is:

qemu-system-arm -kernel pi/kernel-qemu-4.19.50-buster -dtb pi/versatile-pb.dtb -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append 'root=/dev/sda2 panic=1 rootfstype=ext4 rw' -hda pi/raspbian-buster-lite.qcow -net nic -net user,hostfwd=tcp::2222-:22

So (after the configuring the ssh, as shown below)

ssh -p 2222 -l pi localhost

will get you “in”. Ie username pi (password raspberry is the default) and port 2222

Qemu bridges the network (that it emulates), and so your pi is now as connected as your mac.

Working on the pi

You'll probably want to log in by ssh at some point. If for no other reason than Ctl-C will stop the emulator when you are logged in, and not the program you are running on the emulator. This gets old _really_ quickly.

To configure the ssh server on the pi, log in and run

sudo raspi-config
and enable ssh in the interface options. There is another text on how to work on the pi. Off course google is good , and raspian site can help too.