Tag Archives: QEMU

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!

QEMU Linu/xMipsel emulation

This tutorial will assume that you have a running QEMU environment and that it works. These are old notes that I am simply capturing here as a reference, so things may have changed regarding links etc. Please exercise some caution with copying and pasting!

First step is to create a disk image to use with our new OS:

qemu-img create -f qcow hda.img 10G 

This will hold all the files related to our MIPSel machine.

Now go and grab the kernel image:

wget http://ftp.de.debian.org/debian/dists/etch/main/installer-mipsel/current/images/qemu/netboot/vmlinux-2.6.18-6-qemu
wget http://ftp.de.debian.org/debian/dists/etch/main/installer-mipsel/current/images/qemu/netboot/initrd.gz

Now you should be able to start the installation process with

qemu-system-mipsel -M mips -kernel vmlinux-2.6.18-6-qemu -initrd initrd.gz -hda hda.img -append "root=/dev/ram console=ttyS0" -nographic 

Once you have gone through the debian installer, you can boot your new system with

qemu-system-mips -M mips -kernel vmlinux-2.6.18-6-qemu -hda hda.img -append "root=/dev/hda1 console=ttyS0" -nographic 

Using QEMU to emulate ARM devices

This post will show you how to set up a QEMU virtual device to play with your ARM code on an x86_64 host. It is quite simple, and you should be able to simply copy and paste into a terminal and get going relatively quickly.

As an example, we will be installing a debian build (wheezy) into your VM.

First off, we need to install the QEMU packages. I use Ubuntu/Mint, so this post will be somewhat biased towards that.

Let’s start off getting the packages we need:

sudo apt-get install qemu-kvm
sudo apt-get install qemu-system-arm
sudo apt-get install qemu-utils

Now we can check that everything is installed OK and ready to go with:

qemu -version

Make a directory to work with and then grab some files off your local debian mirror. Remember, we need the ARM based distro.

mkdir ~/arm-emul
cd ~/arm-emul
wget ftp://ftp.debian.org/debian/dists/wheezy/main/installer-armel/current/images/versatile/netboot/initrd.gz
wget ftp://ftp.debian.org/debian/dists/wheezy/main/installer-armel/current/images/versatile/netboot/vmlinuz-3.2.0-4-versatile

Remember now that depending on your board/device, you may want to check if it supports ARM EL or ARM HF. As you can probably guess from the above filenames, we are working with ARM EL. There are a number of differences between the way (and efficiency) of the two device types, but if you don’t know, then you are probably using an ARM EL device. Also, it is worth checking with your manufacturer if you haven’t built your device yourself, as ARM HF is a way better buy!

Let’s create a virtual HDD now to host the code/OS:

qemu-img create -f raw hda.img 8G

I like to create a drive as big as my devices flash ROM. In this case, it is 8GB. Yours may vary.

Now, lets get the system up and running:

qemu-system-arm -m 256 -M versatilepb -kernel ~/arm-emul/vmlinuz-3.2.0-4-versatile -initrd ~/arm-emul/initrd.gz -hda ~/arm-emul/hda.img -append “root=/dev/ram”

Should get you started with the Debian installer. Do the installation and then close your VM.

Once complete, mount your filesystem, and then copy the relevant files around. You need to do this step as debian will not be able to install the bootloader, so you kind of have to do it manually.

mkdir mount

sudo losetup /dev/loop0 hda.img
sudo kpartx -a /dev/loop0
sudo mount /dev/mapper/loop0p1 mount

cp ~/arm-emul/mount/boot/initrd.img-3.2.0-4-versatile ~/arm-emul/
sudo umount ~/arm-emul/mount

Now you can start up your brand new debian ARM VM with:

qemu-system-arm -M versatilepb -kernel ~/arm-emul/vmlinuz-3.2.0-4-versatile -initrd ~/arm-emul/initrd.img-3.2.0-4-versatile -hda ~/arm-emul/hda.img -append "root=/dev/sda1"

Great! Now off to make your custom OS and flash it to your board! Good luck!