Raspberry Pi, qemu and network access

Recently, I got in my hands a great book by Igor Viarheichyk called “Embedded Programming with modern C++ cookbook”. It is a fantastic book that captured my interest since the first page.

At one point, guide you on how to set up a qemu environment to run a Raspberry Pi emulator. At this stage, you might experience a problem since the proposed approach does not seem that work. As a workaround, you could apply the following approach.

First, you need to download a raspbian image. It seems that Jessie is the most popular and straightforward to set up. The repository with the lite version can be found here: http://downloads.raspberrypi.org/raspbian_lite/images/

From the list, we select 2017-08-16-raspbian-stretch-lite.zip. Getting the image, we unzip like:

unzip 2017-08-16-raspbian-stretch-lite.zip

Next step is to download the compatible kernel. The appropriate for our case is kernel-qemu-4.4.34-jessie, which can be found in dhruvvyas90/qemu-rpi-kernel GitHub repository, where we can find ready-made kernels that can be used to emulate a Raspberry Pi using QEMU.

It is preferable to convert the img file qcow and resize it. How? Like that:

qemu-img convert -f raw -O qcow2 2017-08-16-raspbian-stretch-lite.img raspbian-stretch-lite.qcow
qemu-img resize raspbian-stretch-lite.qcow +6G

Ready to rock now? I guess so. So let’s start the emulation of the raspberry pi with the following command:

sudo qemu-system-arm \
-kernel ./kernel-qemu-4.4.34-jessie \
-append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" \
-hda raspbian-stretch-lite.qcow \
-cpu arm1176 -m 256 \
-M versatilepb \
-no-reboot \
-serial stdio \
-net nic \
-net user,hostfwd=tcp::5022-:22

At this point, as you can see we configured QEMU to forward connections from our host port 5022 to our raspberry Pi port 22, which is SSH.

Then, we need to enable SSH services by executing:

$ sudo systemctl start ssh

So now, you can easily copy files between your system and the emulated Pi like:

scp -P5022 /mnt/hello pi@192.168.1.68:~

Assuming that, 192.168.1.68 is the ip of your host system and pi the default user of you raspbian image (password: raspberry).

Reference:

Using QEMU to emulate a Raspberry Pi