Full Raspberry Pi (Raspbian) emulation with qemu

I wanted to do some experimental hacking on my Raspberry Pi, specifically to try a bit of fun with talking to Arduino and Spark Cores. My ultimate aim was to have a go at doing something fun with the meArm robotic arm (https://www.wevolver.com/#/project/196/parts/629/document)

I started off compiling OpenCV and OpenNI on the physical pi, but quickly realised I didn’t have a big enough SD card lying around. I momentarily thought about stealing one of my wife’s pro camera SD cards, but then thought about the consequences… I then decided to emulate the whole thing and then buy an SD card when the project was done.

First off, you need a qemu environment. I’ll assume you have a basic qemu installation going, but if not, get started with

sudo apt-get install qemu-system qemu-user-static binfmt-support

Next, you will need to download the latest raspbian release image. Make a directory to use, and then grab it

mkdir ~/qemu_vms
cd ~/qemu_vms
wget http://downloads.raspberrypi.org/raspbmc_latest

You also need a kernel:

wget http://xecdesign.com/downloads/linux-qemu/kernel-qemu

XEC Design maintains a qemu kernel with the ARMhf patches already, but if you would like to build your own one, feel free to grab it at https://github.com/raspberrypi/linux

You will need to extract the zip archive that you just downloaded, and you should be left with something like:

~/qemu_vms$ ls
2014-06-20-wheezy-raspbian.img kernel-qemu

which means you are ready to start doing cool stuff! (Remember that if you are reading this, the .img file has probably changed, so keep a note of that!)

Lets boot this thing up!

qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash" -hda 2014-06-20-wheezy-raspbian.img

which should start up qemu with a command prompt. Login with the default credentials (user: pi, pass: raspberry) and have a cookie for getting this far.

Now, you will notice that not everything can be emulated by qemu, so change /etc/ld.so.preload like this

nano /etc/ld.so.preload
#Comment out the libcofi_rpi object like this
#/usr/lib/arm-linux-gnueabihf/libcofi_rpi.so

Now you need to edit

/etc/udev/rules.d/90-qemu.rules

(This is a new file!)
Add the following to your new file:

KERNEL=="sda", SYMLINK+="mmcblk0"
KERNEL=="sda?", SYMLINK+="mmcblk0p%n"
KERNEL=="sda2", SYMLINK+="root"

Now you should halt/shutdown the system, and prepare for your first real boot!

Boot up again with

qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda 2014-06-20-wheezy-raspbian.img

Do a df -h and notice with horror that you have almost no space to work with!

Resizing the image “disk” is pretty easy though.

First close down the emulator again, then

qemu-img resize 2014-06-20-wheezy-raspbian.img +4G

This will make your partition 6GB long (do more if you like…) which should be plenty of space and will fit onto a relatively cheap 8GB SD Card.

Now boot up your emulator again and do:

sudo ln -snf mmcblk0p2 /dev/root
sudo raspi-config

Choose the first option to resize your disk, and it will tell you to reboot. Great, once everything is halted, manually restart your emulator, and do another df -h. SURPRISE! It now looks like this:

Filesystem      Size  Used Avail Use% Mounted on
rootfs          6.6G  2.1G  4.2G  33% /
/dev/root       6.6G  2.1G  4.2G  33% /
devtmpfs        125M     0  125M   0% /dev
tmpfs            25M  204K   25M   1% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs            50M     0   50M   0% /run/shm
/dev/sda1        56M  9.5M   47M  17% /boot

You are done! Great job!

Have fun!

Liked this post? Follow this blog to get more.