Today a colleague and I wanted to install gnuradio on a Raspberry Pi. This can than be combined with the amazing RTL-SDR dongle. This dongle this is a DVB-T USB stick, but can be turned into full software defined radio.

More information on that can be found here: http://www.rtl-sdr.com/about-rtl-sdr/.

Compiling gnuradio

When trying to compile gnuradio on the RPi (Raspberry Pi) we followed this description. But we quickly ran into a problem, compiling would take 20+ hours!

After running ‘make’ and grabbing a cup of coffee we set ourself a new goal, is it possible to emulate the RPi on our fast Macbook instead?

Spoiler: Building in the emulator is just as slow. There is a slightly faster method involving chroot. But if you really want speed you’ll have to set up a cross compiler environment, which I haven’t been able to get up and running yet.

Qemu

After following a couple of guides that didn’t work we finally managed to get Qemu up and running, this is what we did:

  • Install and upgrade Xcode to 4.3 or above
  • Install the latest version of Homebrew

Now we need to modify the Homebrew formula (which downloads and install qemu) to the correct version:

osx$ vi /usr/local/Library/Formula/qemu.rb

I’m using the osx$ prefix for commands that are executed on your OS X machine, pi$ for commands on the virtual Raspberry Pi.

Use the following file to get the working version 1.7.1 (other versions had SCSI problems): qemu.rb

require 'formula'

class Qemu < Formula
  homepage 'http://www.qemu.org/'
  url 'http://wiki.qemu.org/download/qemu-1.7.1.tar.bz2'

  depends_on 'jpeg'
  depends_on 'gnutls'
  depends_on 'glib'

  fails_with :clang do
    build 318
  end

  def install
    system "./configure", "--prefix=#{prefix}",
                          "--cc=#{ENV.cc}",
                          "--host-cc=#{ENV.cc}",
                          "--enable-cocoa",
                          "--disable-bsd-user",
                          "--disable-guest-agent"
    system "make install"
  end
end

After setting qemu.rb to the correct version you can install qemu:

osx$ brew install qemu --env=std --cc=gcc-4.2

Now check if qemu is installed correctly:

osx$ qemu-system-arm -version
QEMU emulator version 1.7.1, Copyright (c) 2003-2008 Fabrice Bellard

Now we need to download two thing:

  1. Linux kernel
  2. Raspbian image

To download the linux kernel:

osx$ curl http://xecdesign.com/downloads/linux-qemu/kernel-qemu > kernel-qemu

Now we’ve downloaded the latest version of the raspbian image.
In our case: 2014-01-07-wheezy-raspbian.img

First boot

Now it is time to start the image in the emulator:

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

This first boot is a bit special because we only initialize /bin/bash. This is because we need to make two changes to the system:

We need to add a comment to this file:

pi$ vi /etc/ld.so.preload

Comment this line by placing a # in front of the line:

#/usr/lib/arm-linux-gnueabihf/libcofi_rpi.so

Now create the following file:

pi$ vi /etc/udev/rules.d/90-qemu.rules

And put in the following content: 90-qemu.rules

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

Now we can stop the emulator and make one final change, the image file is a bit small and we need to increase the size before we continue:

osx$ qemu-img resize 2014-01-07-wheezy-raspbian.img +8G

From now on we can do a normal boot (save this command) by removing the “init=/bin/bash” part:

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

The last thing we need to do to get our virtual Raspberry Pi up and running is:

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

In this menu, you can “Expand filesystem” to make use of the increased image size (need to reboot afterwards).

Now you are ready to explore the raspberry pi without actually needing one.

(Broken) sources:
https://github.com/psema4/pine/wiki/Installing-QEMU-on-OS-X
http://xecdesign.com/qemu-emulating-raspberry-pi-the-easy-way/

Some problems we’ve encountered:

  • qemu raspberry pi boot getting stuck in ‘scsi’ loop (fixed by using version 1.7.1)
  • Disk size problems, resize didn’t work, expand filesystem didn’t work (fixed expanding and using ln -snf)
 
  • http://about.me/ester misato

    What you probably want is cross compile for the raspberry pi in your OSX machine. That can be done compiling the toolchain. You can have a look a this post (i’m not the original author, but this was the first link I found in google) http://www.jaredwolff.com/blog/cross-compiling-on-mac-osx-for-raspberry-pi/

  • http://blog.iavin.com Avi

    what if I want install it on a already installed SD card?

    • http://www.royvanrijn.com royvanrijn

      I havent tried this yet, but I think in theory you should be able to open an existing image from SD and also put the image back on the SD; I’ll try this once my own personal Rasp Pi arrives!

      • http://oncleben31.cc/ Oncleben31

        Interstead in this feature. Have you test it ?

  • tristank

    I followed you instructions but end up getting errors:

    “`
    Warning: Cannot verify integrity of qemu-1.7.1.tar.bz2
    A checksum was not provided for this resource
    For your reference the SHA1 is: 6606a3091bdf4ee028df3d16fe20d495150ff2c5
    ==>
    ./configure –prefix=/usr/local/Cellar/qemu/1.7.1
    –cc=/usr/local/bin/gcc-4.2 –host-cc=/usr/local/bin/gcc-4.2
    –enable-cocoa –disable-bsd-user –disable-guest-agent
    ==> make install
    /private/tmp/qemu-5xAU/qemu-1.7.1/include/qemu-common.h: In function ‘can_use_buffer_find_nonzero_offset':
    /private/tmp/qemu-5xAU/qemu-1.7.1/include/qemu-common.h:471: error: ‘__m128i’ undeclared (first use in this function)
    /private/tmp/qemu-5xAU/qemu-1.7.1/include/qemu-common.h:471: error: (Each undeclared identifier is reported only once
    /private/tmp/qemu-5xAU/qemu-1.7.1/include/qemu-common.h:471: error: for each function it appears in.)
    make: *** [thread-pool.o] Error 1

    READ THIS: https://github.com/Homebrew/homebrew/wiki/troubleshooting
    “`

    I’m on 10.9.4.

  • Baihaqi Aviq

    hi.. how run openelec on qemu??

  • Walter B DuquedeEstrada

    Using the latest qemu from homebrew (2.2.0 )on Yosemite, I was successful following the “FIRST BOOT STEPS”. I also used the same steps to load KANO OS. Thanks for the help.

  • rasika90

    Hi, I tried your article with latest image “2015-02-16-raspbian-wheezy.img”. When running the Kernel, it startups and giving the error: “vfs cannot open root device sda2 or unknown-block”. and it shows list of all partisions as sr0, sda, mtdblock0. Any idea?

    • rasika90

      wow, after hours of searching finally at last tried “2014-01-07-wheezy-raspbian.img”. it is working fine. There is something I don’t understand.

  • RichieNess

    Hi, I hope you can shed some light on a problem I’m having. I’m a noob and would like to know where do you locate the raspbian image when you start the image in the emulator?

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

    I get the error below when the image file is located on my desktop:
    qemu-system-arm: -hda 2015-05-05-raspbian-wheezy.img: could not open disk image 2015-05-05-raspbian-wheezy.img: Could not open ‘2015-05-05-raspbian-wheezy.img': No such file or directory

    • Bryan Gibson

      remove the 2015-05-05…img from the command and drag the file from finder into terminal and try it. Worked for me.

      • RichieNess

        thanks for the help. worked a treat.

  • Bryan Gibson

    Where can I find the proper kernel now that http://xecdesign.com/downloads/linux-qemu/kernel-qemu is no longer valid???

  • dhosenfeld

    Hi, thanks for this post.

    The first boot doesn’t work on my machine. I ge the following error message:

    os$ qemu-system-arm -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append “root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash” -kernel kernel-qemu -hda 2015-05-05-raspbian-wheezy.img

    qemu: fatal: Trying to execute code outside RAM or ROM at 0x10000000

    R00=00000000 R01=00000183 R02=00000100 R03=00000000
    R04=00000000 R05=00000000 R06=00000000 R07=00000000
    R08=00000000 R09=00000000 R10=00000000 R11=00000000
    R12=00000000 R13=00000000 R14=00000000 R15=10000000
    PSR=400001d3 -Z– A svc32
    s00=00000000 s01=00000000 d00=0000000000000000
    s02=00000000 s03=00000000 d01=0000000000000000
    s04=00000000 s05=00000000 d02=0000000000000000
    s06=00000000 s07=00000000 d03=0000000000000000
    s08=00000000 s09=00000000 d04=0000000000000000
    s10=00000000 s11=00000000 d05=0000000000000000
    s12=00000000 s13=00000000 d06=0000000000000000
    s14=00000000 s15=00000000 d07=0000000000000000
    s16=00000000 s17=00000000 d08=0000000000000000
    s18=00000000 s19=00000000 d09=0000000000000000
    s20=00000000 s21=00000000 d10=0000000000000000
    s22=00000000 s23=00000000 d11=0000000000000000
    s24=00000000 s25=00000000 d12=0000000000000000
    s26=00000000 s27=00000000 d13=0000000000000000
    s28=00000000 s29=00000000 d14=0000000000000000
    s30=00000000 s31=00000000 d15=0000000000000000
    FPSCR: 00000000

    Some ideas?

    Thanks, Dirk