### Another Dimension

Xiangyu's personal blog.

### Emulating Raspberry Pi with QEMU

2013-11-16

The USB-TTL cable I’m provided with doesn’t support Windows 8.x, which is super inconvenient. So I decided to make a virtual machine running Occidentalis so that I don’t have to work on the real Pi.

“Occidentalis” is Adafruit’s Raspberry Pi educational distro. More details can be found here.

Before I made everything from scratch, I did some research and found one article that already discussed emulating RPi on Linux x86-64, and it works. And I even found one packed emulator for Raspbian on Microsoft Windows (Many thanks!)

Update: I have packed my customization to the Raspbian emulator and uploaded the whole stuff to SourceForge. Copyright belongs to whoever creates the original tools and software.

## Build from scratch

A brief guide if you want to build everything from scratch.

### Installing QEMU for your OS.

For Windows, I found this: http://qemu.weilnetz.de/ (owner: Stefan Weil)

For Linux (e.g., Debian or Ubuntu), the following packages are needed, as the article in soslug.org discussed:

• qemu
• qemu-user
• qemu-kvm-extras
• qemu-utils
• kvm-ipxe

### Get “kernel-qemu” file

Just follow the article in soslug.org.

### Get the system image file

Ether Raspbian or Occidentalis.

My customizations to the sourceforge project “Raspberry Pi emulation for Windows”:

• I used Occidentalis to replace Raspbian since the former better suits the need of a learner.
• I added some port forwarding in the command to run QEMU:
qemu-system-arm.exe -M versatilepb -cpu arm1176 -m 192 -hda Occidentalis_v02.img -kernel kernel-qemu -append "root=/dev/sda2" -net nic -net user,hostfwd=tcp::2222-:22,hostfwd=tcp::5800-:5800,hostfwd=tcp::5900-:5900


I forwarded VM port 22 to host port 2222 so that I can ssh into the VM by connecting to localhost:2222 and exchange files easily.

I forwarded VM ports 5800 and 5900 to host ports 5800 and 5900, so after installing TightVNCServer on the VM, I can use TightVNC Viewer to get a decent desktop environment. Simply connect to localhost using TightVNC’s default port 5800 or 5900.

Thankfully again, how to install VNC Server on RPi is discussed in this article: http://elinux.org/RPi_VNC_Server

The emulator already runs faster than the real Pi, but things are still slow, and it seems the Internet connection is not quite stable inside QEMU VM. I don’t know how it goes on other machines.

There have been many updates since the Raspbian or Occidentalis image was released. So one might want to update the system:

sudo apt-get update


And because my course project was to write a simple C compiler on Raspberry Pi using yacc and lex, I also installed the related packages:

sudo apt-get -y install byacc flex bison m4


I am quite happy with the customizations because the GUI is decent and the system runs faster than the real Pi. But unfortunately the GPIO of RPi cannot be emulated.

#### Alternative Approach – SSH

An alternative solution to using USB-TTL cable: having your Pi send its IP address to your email when it starts up, and then you ssh into it. Here is an article discussing this approach.