Raspberry Pi has gained a lot of fame over the past years since it’s introduction. Being dirt cheap while powerful enough to run your home multimedia center is the key selling point of Raspberry Pi. But when it comes to embedded systems, one might want to test his software on an emulated Raspberry Pi (due to the debugging and serial console maybe!). For sure there are some cases that running your code on an emulated R-Pi would make sense. If that is the case, today’s article is going to show you how to emulate R-Pi on Linux!
So, there are essential 3 things you need to do. Getting the R-Pi Image, installing QEMU emulator (if you do not have it already) and compile or download a QEMU-Ready kernel of R-Pi.
Raspberry Pi Image
You can simply download the latest Raspbian Wheezy distribution from RaspberryPi.org. Please note that the procedure in this article is only tested on Wheezy, and I am not sure if it is going to work with the new Jessie release or the NOOBS flavour (I do not see why it shouldn’t, you can try on your own and let me know :P).
UPDATE JULY 2016: Since the procedure described in this tutorial is only tested and guaranteed to work with Raspbian Wheezy, and since the Wheezy is not anymore the de-facto distribution, you can find the official archived distribution only HERE.
QEMU is an opensource machine emulator that supports wide variety of processors (including R-Pi’s ARM processor). You need to install QEMU on your Linux. Installation is pretty easy…either you can build from source or install it directly from you distribution’s source. On Ubuntu terminal do:
$sudo apt-get update && sudo apt-get install qemu-system-x86
Since you are there…you might want to install KVM as well (it will be usefull for a later article about running R-Pi emulator on higher speeds!) To do so, please follow this page (it is not essential for current article, you might as well skip it).
To check the correct installation, do as follow:
$qemu-system-arm --version //your output should be like QEMU emulator version 2.0.0 (Debian 2.0.0+dfsg-2ubuntu1.19), Copyright (c) 2003-2008 Fabrice Bellard
Get things going…
So first of all create a working directory anywhere you like in your system. I did it on “~/Work/raspberry-pi” (I hope you know that ~ refers to your home directory!!!!). Copy your newly downloaded Raspberry Pi image to current directory and unzip it. Then rename it to rpi.img (if you want to have an easy life copy/pasting the rest of tutorial). You might keep the original zip file intact, in case you screw up the image you would not be forced to re download that 1GB file…
QEMU-Ready Raspberry Pi Kernel
You can download the QEMU-Ready R-Pi’s kernel from here (make sure to get the one based on Wheezy). Or you can simply clone the git repository in the directory that you just created using the command below:
$git clone https://github.com/dhruvvyas90/qemu-rpi-kernel.git
This is not the latest kernel version but will get you going. (I will try to build the most recent kernel version later and edit this article!)
Preparing the setup phase
So If you have done everything correctly your working folder should contain these files and folders
Next, you need to make a simple script (to make it easier to start the emulator…believe me it is going to be a long command!). So open up your favorite text editor and save the content below as a new file and name it config
#!/bin/bash # Starts raspberry pi image in configuration mode qemu-system-arm -kernel ./qemu-rpi-kernel/kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash" -hda rpi.img
After saving the file, make sure you make it executable by the command below:
$chmod +x config
Now you are ready to start the raspberry pi in configuration mode. To do so, run the script you just created:
Again if you have done everything correctly…hopefully you will be greeted by terminal prompt of Raspberry Pi:
Now, you have to do some changes in the R-Pi. Please note that you are in a terminal and from here onwards…you need to do the required changes using nano text editor. While you are in the R-Pi’s emulation window do the following changes (please make sure you are in R-Pi’s QEMU window, not to accidentally perform these changes on your own machine!)
TIP: If QEMU window has captured your mouse, you can release it by pressing LEFT CTRL + LEFT ALT
This will open up nano and lets you edit the ld.so.preload file. There is only one line of text in that file. You need to comment it out by putting a # in front of that line. To save and exit nano, You must press CTRL+O then accept the changes, then CTRL+X.
Now you should define some rules to tell raspberry pi how to direct calls to SD-Card to the “sda” storage we have defined in the config script. To do so, you need to make a new rule in udev rules. Create a new rule by following command:
It makes a new file named 90-qemu.rules in /etc/udev/rules.d folder. Type in the following directives in the new file:
KERNEL=="sda", SYMLINK+="mmcblk0" KERNEL=="sda?", SYMLINK+="mmcblk0p%n" KERNEL=="sda2", SYMLINK+="root"
Now save changes and exit nano. You have done the basic configuration now and it is time to actually boot in the fully functional Raspberry Pi. At this point just close the QEMU window.
Now you have to make another script and name it runme but this time put the folliwing content in the script, save and finally make it executable.
#!/bin/bash # Start the Raspberry Pi in fully functional mode! qemu-system-arm -kernel ./qemu-rpi-kernel/kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda rpi.img
Did you notice the change? we just removed the init=/bin/bash compared to the config script. We are almost done know…just run the runme script and you should get booted into a fully functional emulated Raspberry Pi. Here you can do usual config procedure using raspi-config command and all those stuff…
What are the next steps?
We will cover this topics in a future article:
- Increasing the free space of the raspberry pi image from 300 megabyte to whatever you like
- Increasing the resolution
- Booting in GUI mode
I have also made a video version of this tutorial on YouTube. If you faced any problem please see this video. You can also use the comment section to ask questions!