If you need to test ARM-specific code but don't have (or need) real ARM hardware for testing, you can set up a virtual ARM environment running Ubuntu Linux in the QEMU emulator. This is the same emulator system used by the Firefox OS Simulator.

Note: In this article, we'll be making use of Linux distributions and other software provided by Linaro, which provides distributions customized for various ARM hardware.

Prerequisites

This article assumes that the computer on which you'll be running the virtual ARM environment is Ubuntu Linux. This makes setup easier, since it's much easier to install the required software from external repositories. It's possible to set up this simulation on other platforms but that is not (yet) covered here.

Select a target board and CPU

QEMU is capable of emulating a variety of boards and ARM CPUs. At the time of writing, the author of this article was only able to get the Versatile Express board to work with more than 256 MB of RAM, so for the purposes of this article we'll use that one, and we'll use the Cortex A9 (ARMv7) CPU.

Install the required software

First, we need to tell the package manager to allow us to use Linaro's repository, since it contains the tools we'll need as well as recent versions of the QEMU simulator package. We need to be using at least QEMU 0.15*.

sudo add-apt-repository ppa:linaro-maintainers/tools

Next, we install the Linaro tools and the QEMU packages:

sudo apt-get install linaro-image-tools qemu-user-static qemu-system

If you want to be able to cross-compile using your host computer to build code to run on the simulator, you'll need to install the GCC packages for doing so:

sudo apt-get install gcc-arm-linux-gnueabi g++-arm-linux-gnueabi

Download a Linaro release and hardware pack

You can find suitable release and hardware pack tarballs on the Linaro release page. Some of them might work, others might not; you may have to experiment in order to come up with a combination that adequately simulates the environment you want to simulate. Those used in the instructions below worked well at the time this article was written.

Note: Update! If you use Natty today, you will need to update /etc/sources.list to reflect that the repositories for natty have been moved to http://old-releases.ubuntu.com/ubuntu before you will be able to install additional software

wget https://releases.linaro.org/platform/linaro-n/nano/alpha-3/linaro-natty-nano-tar-20110302-0.tar.gz
wget https://releases.linaro.org/platform/linaro-n/hwpacks/alpha-3/hwpack_linaro-vexpress_20110302-0_armel_supported.tar.gz

Create the VM disk image

Using the Linaro tools, you can easily create an SD card image from the downloaded packs. This will take a while, so be patient.

linaro-media-create --rootfs ext2 --image_file vexpress.img --dev vexpress \
  --binary linaro-natty-nano-tar-20110302-0.tar.gz \
  --hwpack hwpack_linaro-vexpress_20110302-0_armel_supported.tar.gz

Note: These instructions were derived from Versatile Express support in QEMU, with some modification.

Extracting the kernel and initrd

The VM image we just created contains the kernel and initrd for our machine. We need to extract them from the VM so QEMU can use them.

First, we mount the image at /mnt/tmp. The snippet below finds the correct partition in the image and mounts it.

sudo mount -o loop,offset="$(file vexpress.img | awk 'BEGIN { RS=";"; } /partition 2/ { print $7*512; }')" \
  -t auto vexpress.img /mnt/tmp

Note: In version 16.10 of Ubuntu $7 is no longer correct , $10 works instead.

Next, copy the kernel and initrd files from the VM image.

Note: The actual names of the files might vary slightly in your build.

cp /mnt/tmp/vmlinuz-2.6.38-1000-linaro-vexpress .
cp /mnt/tmp/initrd.img-2.6.38-1000-linaro-vexpress .

To make the kernel and initrd files easier to refer to, we can create symlinks:

ln -s vmlinuz-2.6.38-1000-linaro-vexpress vmlinuz
ln -s initrd.img-2.6.38-1000-linaro-vexpress initrd.img

Starting QEMU

You can now start QEMU with the following command:

qemu-system-arm -M vexpress-a9 -cpu cortex-a9 -kernel ./vmlinuz \
  -initrd ./initrd.img -redir tcp:2200::22 -m 512 \
  -append "root=/dev/mmcblk0p2 vga=normal mem=512M devtmpfs.mount=0 rw" \
  -drive file=vexpress.img,if=sd,cache=writeback

Some explanation of the non-obvious options:

After starting the machine, you should end up with a Linux shell in the QEMU window after some startup time.

Using SSH

To use SSH to connect to the VM, you first need to bring up its network and install the SSH server package.

Bring up network temporarily using the following commands in the simulator:

ifconfig eth0 up
dhclient eth0

Now install SSH:

apt-get install openssh-server

To make the network changes permanent, edit the file /etc/network/interfaces (you can use the vi editor, for instance) and add the following lines:

auto eth0
iface eth0 inet dhcp

Finally, set a password for root using the passwd command, reboot the virtual machine and see if you are able to SSH into it by typing the following command into a terminal window on the host:

ssh -p2200 root@localhost