Virtualising Raspbian on Mac OS X

26 November 2012, 08:03

I’ve had a fun day trying to virtualise the Raspberry Pi operating system, Raspbian, on my Mac. I’m writing a book about the “RasPi” and need to do things like get screenshots.

A whole day spent compiling this, installing that, configuring the other… I’m not a programmer. This isn’t fun. I even tried running Qemu on Ubuntu, which was being virtualised in VMWare Fusion. It worked, but made the CPU work so hard that the fans were blowing louder than a hairdryer.

Lots of guides I read were from early this year (2012), from when the Raspbian images were first released and there was a nasty bug with Qemu that caused it to glitch on a Mac when starting-up with an ARM image.

In the end I found the following solution, which might save you some time. It will still take several hours to setup but works very well afterwards.

I hope it works for you. Let me know if not (keir d.o.t. thomas at gmail d.o.t. com).

1. Install Xcode from the Mac App Store (it’s free but a huge download).

2. Run Xcode and agree to its initial installation of stuff.

3. Once Xcode’s up and running, open its Preferences, click the Downloads tab/button at the top, then select to install the Command Line Tools.

4. Install XQuartz (X11) (not sure if this is actually needed for Qemu but it’s a good idea anyway).

5. Install Java (open a terminal window and type javac — this will either show you it’s already installed, or prompt you to install it).

6. Follow these instructions to download and install Fink, including the instructions to update your path and update the package list.

7. Create a directory called raspbian, then download this kernel image file into it, then download the latest Raspbian image AND EXTRACT IT FROM ITS ZIP into the folder (so you end-up with the Raspbian .img file).

8. At a terminal window type fink install qemu. You might get asked about databases (I was surprised to see that Qemu needs a database, and even more surprised to see it apparently need one with AES encryption). I chose option No.2. You might also get asked about which mirrors to use. I chose to use the Sourceforge ones.

9. Switch into the raspbian directory you created, and type the following (which is all one line, although I’ve split it across several to be readable):

qemu-system-arm -kernel kernel-qemu
-cpu arm1176 -m 256 -M versatilepb -no-reboot
-serial stdio -append “root=/dev/sda2 panic=1”
-hda 2012-10-28-wheezy-raspbian.img

EDIT 18 Dec 2012: For the latest kernel image (2012-12-16-wheezy-raspbian.img), you’ll need to use the following line:

qemu-system-arm -kernel kernel-qemu
-cpu arm1176 -m 512 -M versatilepb
-no-reboot -serial stdio -append “root=/dev/sda2
elevator=deadline rootwait panic=1” -hda

Note: If you cut and paste the above lines into Terminal, the quotation marks around “root=/dev/sda2 panic=1” will be incorrectly formatted and will cause an error — just use the cursor to delete them, then retype them.

Edit: A fix for the Qemu window being small is to add the following two lines to /etc/rc.local within your virtualised Raspbian setup:

fbset -fb /dev/fb0 1024×768-60
export FRAMEBUFFER=/dev/fb0

Remember that, in order to edit /etc/rc.local, you’ll need to use sudo (i.e. sudo nano /etc/rc.local). Place the lines directly above the exit 0 lines (see screenshot below). Bear in mind that rc.local runs at the end of each run level, so the window WON’T be resized until just before the login prompt appears.

Edit 2: There’s an issue with colour depth, which is noticeable when the desktop is started (i.e. startx). To cut a long story short, it appears I can’t set a colour depth of 16-bits using fbset. I’m not sure why this is but it might be the very limited virtualised default graphics adapter that Qemu is using. I think it’s possible to choose another “graphics card”, which will have to be specified in the Qemu command-line. As an interim measure, add -depth 8 to fbset above in rc.local:

fbset -depth 8 -fb /dev/fb0 1024×768-60

Edit 3: From a quick browse of Qemu’s documentation it appears that ARM virtualisation isn’t like PC virtualisation; just like PC emulation aims to recreate a desktop PC, ARM emulation aims to recreate various ARM prototyping boards. So it appears there’s only one choice of graphics adapter and it might be limited to 8-bit colour depth, although I’m not sure, and don’t want to wade through lots of embedded Linux documentation/forums. (I wonder aloud if the Qemu guys are working on emulating a RasPi?) However, I’ve got what I need, which is to be able to take screenshots of things happening at the command-line. For the X session screenshots I can use a real-life RasPi.




Leave a comment...

Hi Keir…

Thanks for the instructions. I’m trying to follow along, but using the homebrew mac package management system instead of fink.

Unfortunately the site seems to be down. Poking around on the RPi forums, it looks as though some other precompiled kernels are available from this thread:

— Tim Gilbert · Dec 19, 10:35 AM · #

Tim, as another person following these instructions, thanks for that alternative link: Trying this now!


— Neil · Dec 19, 12:47 PM · #

Sorry to hear the site’s offline.

Here’s the kernel file I use if you want to download it:

— Keir Thomas · Dec 20, 03:43 AM · #

Fun bit about this: turns out both 1.3.0 and current git versions of qemu fail reliably for me; they boot, and just after mounting /, throw SCSI errors and eventually hang or crash.

1.2.2 works fine.
is the bug I opened after git bisecting my way to the problem.

— Derrick Brashear · Dec 30, 06:59 AM · #

Commenting is closed for this article.