Raspberry Pi under QEMU

What: Run raspberry pi system under QEMU
Why: I wanted to have builder environment for Raspberry PI. Emulating it it much faster that running something on it
Disclaimer: I am not a qemu/raspberry-pi expert – Some things may be wrong here

Introduction

Running qemu-arm is slightly different than running qemu for x86. There is no BIOS there and it needs to boot with a kernel image directly. The tricky part is that this image is not the standard Raspberry Pi (from now on mentioned as “raspi”). You can either download one from online or create your own.

I ended up creating my own qemu arm image

Disk image

Get the official raspberry pi disk image from here. Place it somewhere and uncompress it.

By default the image does not have much free space. A trick to change that is to extend it with something like the following:

# dd if=/dev/zero of=2013-02-09-wheezy-raspbian.img 
 bs=1024000 conv=notrunc 
 seek=4000 count=1

This will write 1MB after the first 4000 MBytes on the image. This will cause the image to become approximately 4GB but it will leave a hole in the file if the underlying filesystem supports it.

Kernel Image (optional)

If you want to compile your own kernel image for qemu then follow first the instructions  here. Follow just the instructions on cloning git and patching the kernel.

Instead of configuring it yourself get this configuration (will speed your life) which has some additional stuff like built-in sound, vfat support, IPv6, nice fonts, etc. Put this under the cloned kernel directory with name .config and then run:

# make ARCH=arm oldconfig
# nice make ARCH=arm -j 4   # Adjust for your number of cores
# cp arch/arm/boot/zImage /path/to/raspi/directory

You will need the arm compilation toolchain. If you’re running Debian then follow the instructions in http://www.emdebian.org/

The package you are looking for is gcc-4.4-arm-linux-gnueabi

Note: Raspberry Pi supports hardfloat while the above compile is not using hardfloat. There should be no problems with that since the kernel is not using floating point math itself and it won’t make a difference. The attached kernel configuration however has hardfloat support and so does qemu.

Booting

I am using vde networking with qemu so your setup may be slightly different. In any case, this is a nice setup to get you started:

# qemu-system-arm 
        -cpu arm1176 
        -kernel kernel-v13-qemu-130323 
        -name 'builder-raspi' 
        -hda 2013-02-09-wheezy-raspbian.img 
        -m 256 
        -M versatilepb 
        -no-reboot 
        -serial stdio 
        -net vde,sock=/var/run/vde2/tap0.ctl 
        -net nic 
        -append "root=/dev/sda2 panic=0 ro

Use the proper path for the disk image and the kernel.

Now, here’s the catch: The latest (as of 2013-03-23) raspberry pi image will have some issues booting with the above setup.

Fix latest filesystem image

Change the qemu -append parameter to: “root=/dev/sda2 panic=0 ro single” and boot the image.

This should leave you with a nice prompt. Then:

# mount / -o remount,rw

Next, edit fstab and change mmcblk0p1 and mmcblk0p2 to sda1 and sda2 respectively

Then create the file /etc/udev/rules.d/90-qemu.rules with the following contents:

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

This should ensure that /dev/mmcblk0p1/2 exist and will make other stuff work.

Also edit /etc/ld.so.preload (if it exists) and remove or comment everything in it.

Now shutdown the qemu image, restore the -append parameter and run it again. If raspi-config does not start by itself, run it yourself after logging in as user “pi”. You can now select “expand_rootfs” and do another reboot to get more space on the root partition.

7 comments

    1. My bad: By “sound” I meant that it has compiled in the sound modules. I’m changing that in the post as it seems that qemu-arm doesn’t support sound. Everything else should be valid though 🙂

      Like

      1. Ah, fair enough. I’ve got another correction though. You say you link to my article on how to compile a kernel, but the link actually goes to the article on how to compile qemu, which may confuse some people. Other than that, everything seems to be alright. I especially like the udev rules trick.

        Like

  1. Excellent article. The most valuable part was how to ensure mmcblk exists but I ran into the issue of having: “/dev/root -> sda2” (/dev/root was still pointing to the second partition).
    So, “$ raspi-config expand_rootfs” didn’t worked for me.

    I had to do this:
    $ sudo rm /dev/root (Remove the link)
    $ sudo ln -s mmcblk0p2 /dev/root (Create a new link pointing to mmcblk0p2)
    $ sudo raspi-config expand_rootfs (Now it worked)
    $ sudo reboot

    $ sudo rm /dev/root (Remove the link)
    $ sudo ln -s sda2 /dev/root (restore the previous link)
    $ sudo reboot (yes, again)

    now I have almost 4G partition in /dev/rootfs (which in fact, sda2)

    Like

  2. Thank you so much for your excellent walk through, precise instructions!
    I’ve managed to get rasbian up and running on osx yosemite and expand the file system, thanks to Alek’s comment – worth adding.

    Yet, I have some issues,
    – memory: would want to set it to 512 for Pi B+ but I see a white screen freeze upon launch

    – resolution: any option I’ve tried in -vga param doesn’t work (cirrus, std, vmware …) . My goal is a higher resolution screen, currently looks like 640×480 which is hard to work with X.

    – ERROR: ../libkmod/libkmod.c:554 kmod_search_moddep: could not open file /lib/modules/3.10.26+/modules.dep.bin
    This error appears while booting rasbian, would be nice to know what it means.
    The directory ../3.10.26+ does not exist but the file is inside 3.12.28+ (a new version maybe?).
    If I new where to set the correct path for this file, if this makes sense.

    Thanks

    Like

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.