Testing Raspberry Pi Images with Qemu

Although changing the OS on a Raspberry Pi is quick and easy (especially if you have a spare SD card), there are times when you might want to test a system first, or simply tinker without needing a spare Pi.

This documentation details how to use Qemu to run a RaspberryPi image.

 

Pre-Requisites

Make sure the qemu-arm support is installed (On Ubuntu the package will be called either qemu-system or qemu-kvm-extras). To find out, run the following

qemu-system-arm -cpu | grep 1176
qemu-system-arm -cpu | grep 1136

You should see the arm1176 CPU, if it's not available you can also use arm1136-r2. If neither are available, check that you have the relevant packages installed

Creating a Disk Image

Assuming you've already loaded the image onto an SD card, you'll want to make a copy (you can run direct from the SD Card but it tends to be slow). If you've simply got an image file, skip this step.

You'll need to know the character device node that the SD card has been assigned to (to find out, run dmesg just after plugging it in). In the examples we'll assume it's /dev/sdb

dd if=/dev/sdb of=disk.img

 

Grabbing the Kernel

To run well in Qemu, we need to use a slightly amended kernel

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

 

Initial (config) boot

For our first boot, we need to make a basic config change to our Pi image, so we're going to jump straight to a shell (replace the cpu with arm1136-r2 if needed)

qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 512 -M versatilepb \
-no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash" \
-hda disk.img

Once the system has finished booting, run

nano /etc/ld.so.preload
Put a # in front of the first line so that it looks like this:
#/usr/lib/arm-linux-gnueabihf/libcofi_rpi.so

# Save and exit
halt

 

Boot the System

Now we can boot the system for real

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 disk.img

 

Dumping back to an SD Card

If you want to dump the image back to an SD card, make sure QEmu isn't running and then run (make sure you use the correct character device, dd can kill your system!)

dd if=disk.img of=/dev/sdb

 

Things to Note

The CPU's we're using don't appear to support more than 256mb of RAM, so setting the memory (-m) to 512 won't work.

 
 Share