Psellos
Life So Short, the Craft So Long to Learn

Run Debian ARMEL Linux on OS X with QEMU

August 15, 2012

While getting OCaml 4.00.0 working on iOS I’ve often wanted an ARM-based Linux system for testing out the stock OCaml compiler. Now I have one, running inside QEMU on my Mac!

QEMU is an impressive system that emulates ten or twelve different hardware systems at an amazing level of detail yet with good performance. A couple of the emulated systems are based on ARM and let you boot a Linux system (such as Debian Squeeze, which is what I used). In essence, QEMU does what I thought the iOS Simulator did, before I found out how it really works. I learned about QEMU from Benedikt Meurer on the OCaml mailing list—he suggested it for running the OCaml 4.00.0/ARM compiler.

The only downside is that it’s extremely tricky to get the ARM subsystem of QEMU working under OS X right now. Many things are in flux: new versions of QEMU, OS X, and Xcode have already come out. So things might be working well by the time you read this. Unfortunately, right now (August 15, 2012) recent versions of QEMU, OS X, and Xcode don’t work together. And realistically it could be a while until they do.

After trying many things, I found a page by Jeremy Kahn that explains how to build QEMU for OS X with Homebrew. Homebrew is a package manager for OS X based on Ruby and Git. It seems to be getting a lot of play these days, and I use it myself.

I made a couple of corrections to Jeremy’s steps. For posterity, here are the steps I followed to get Debian Squeeze ARMEL working under QEMU on my Mac. I have Xcode 4.3.3 and OS X 10.7.4 (Lion).

(Edit: Jeremy has updated his page to include my corrections.)

Preliminaries

  • Install Xcode. You can download it for free from the Mac App Store.

  • In Xcode -> Preferences go to Downloads -> Components. Install the Command Line Tools.

  • Install Homebrew, as directed on the Homebrew Home Page. Make sure your Homebrew is up to date:

$ brew update

Install QEMU

  • Here begins the tricky part. The ARM subpart of latest QEMU built by Homebrew doesn’t work. It hangs indefinitely at startup, as I can attest from experience. You need to trick Homebrew into using version 1.1.0 of QEMU. You don’t want version 1.1.0-1 (the latest version as of this writing). We’re going to check out an old version of the Homebrew recipe and use that to build QEMU:

$ cd /usr/local   # (or wherever you installed Homebrew)
$ git checkout 2b7b4b3027 Library/Formula/qemu.rb

If you look at the replacement recipe you should see that it uses QEMU 1.1.0 exactly.

$ grep url Library/Formula/qemu.rb
  url 'http://wiki.qemu.org/download/qemu-1.1.0.tar.bz2'

  • Even more trickery is required. The gcc that comes with the latest Xcode can’t be used to build QEMU. Download an older version as follows:

$ brew install https://raw.github.com/Homebrew/homebrew-dupes/master/apple-gcc42.rb

(Warning: this is a long line. If you cut and paste, be sure to get it all.)

After this, you should have gcc 4.2:

$ which gcc-4.2
/usr/local/bin/gcc-4.2

  • Build and install QEMU using the alternate compiler:

$ brew install qemu --use-gcc

If it works, you should have qemu-system-arm, the QEMU executable for the full-system ARM emulator.

$ which qemu-system-arm
/usr/local/bin/qemu-system-arm

Install Debian Squeeze ARMEL

You can download the necessary disk image and support files for booting Debian on QEMU from Aurélien Jarno’s ARM QEMU Files at Debian HQ. You need a disk image (debian_*.qcow2), an initial ramdisk (initrd.img-*) and a kernel file (vmlinuz-*). Here are the commands to download the ones I used for Debian Squeeze:

$ curl -O http://people.debian.org/~aurel32/qemu/armel/debian_squeeze_armel_standard.qcow2

$ curl -O http://people.debian.org/~aurel32/qemu/armel/initrd.img-2.6.32-5-versatile

$ curl -O http://people.debian.org/~aurel32/qemu/armel/vmlinuz-2.6.32-5-versatile

(You might find it more convenient to download from the website. Otherwise, be careful of these long command lines.)

You can now boot Debian Squeeze inside the QEMU emulation of ARM:

$ qemu-system-arm -nographic -M versatilepb \
-kernel vmlinuz-2.6.32-5-versatile \
-initrd initrd.img-2.6.32-5-versatile \
-hda debian_squeeze_armel_standard.qcow2 \
-append "root=/dev/sda1 console=ttyAMA0"

You should see many lines of boot output, then a login prompt:

debian-armel login:

The root password is root. There is a nonprivileged user account named user, whose password is also user.

To exit the emulator, type “^A x” (Control-a, then x).

The emulator works exceptionally well—well enough that I successfully built the OCaml 4.00.0 compiler from sources and then ran tests with it. There is one smallish problem that the (simulated) SCSI disk seems to encounter hardware errors occasionally. I’ve never heard of hardware errors with a simulated piece of hardware! (I guess you could say this makes the simulation even more accurate.) My web searches suggest that this is a known problem that will be fixed in a future release of QEMU.

I find it strangely thrilling to run a simulated ARM system on my Mac. (Pehaps I’m too easily thrilled.)

If you have any corrections, improvements, or other comments, leave them below or email me at jeffsco@psellos.com.

Posted by: Jeffrey

Comments

blog comments powered by Disqus