Monday, July 20, 2015

Emulating a Raspberry Pi using Qemu on Windows

The Raspberry Pi is a fun little computer great for small projects or just tinkering.  Sometimes you might want to try something "virtually" before doing it on a real Raspberry Pi.  That's where Qemu comes in.  Qemu is a small, free, and open-source emulator that can run Raspberry Pi software such as Raspbian.  There are multiple guides on the Internet on how to do this (e.g. here and here), but unfortunately I had a lot of problems getting these to work.  I was getting a lot of errors following these guides.  But with a lot of trial and error I was able to figure out the exact steps necessary to successfully emulate a Raspberry Pi. using Qemu running on Windows.  This guide should work on pretty much any version of Windows.  Also, this guide assumes you have at least some Linux knowledge.  For example, I'm going to assume you know how to run the Linux utility 'nano.'

First step is to download the necessary files.
  • Download the 64-bit version of Qemu for Windows, or the 32-bit version of Qemu for Windows.  Please note, and this is very important, I'm using Qemu version 2.3.0 dated April 24th 2015.  There are newer versions of Qemu available; however, in my experience the newer versions don't work because they are missing necessary files.  The version I'm using is known to work, try a newer version at your own risk.
  • Download the latest version of the Raspbian image.  At the time of writing this the latest version is May 5th 2015.  Newer versions (once released) should work the same.
  • Download the Qemu Raspberry Pi kernel.

Next, install Qemu on your system (or use a tool like 7Zip to "extract" the EXE contents into a folder of your choosing).  Unzip the Raspbian image into the same folder as Qemu.  I renamed the file from "2015-05-05-raspbian-wheezy.img" to just "raspbian.img."  From the Raspberry Pi kernel unzip "kernel-qemu" and place it into the Qemu folder.

Now you're ready to run Qemu for the first time.  Unfortunately getting the Raspberry Pi emulation to work properly is a process that requires multiple initial reboots and configuring.  But once you complete this process, the emulation works correctly from then on.

Begin the emulation with the command:
qemu-system-arm.exe -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 raspbian.img
This will start in a maintenance mode.  We need to make some changes to the image before we can start it up normally.
nano /etc/ld.so.preload
There's a single line in the file, comment it out by placing a '#' at the beginning.  Save the file, then create a new file as follows:
nano /etc/udev/rules.d/90-qemu.rules
Type the following into this file:
KERNEL=="sda", SYMLINK+="mmcblk0"
KERNEL=="sda?", SYMLINK+="mmcblk0p%n"
KERNEL=="sda2", SYMLINK+="root"

Save the file and exit nano.  Then type "exit" to end the emulation.

Now is a good time to expand the Raspbian image.  By default the image is 3GB in size.  In this tutorial we'll expand it by adding 5GB for a total size of 8GB.
qemu-img.exe resize raspbian.img +5G

This command will generate some ominous warnings, but as long as the command ends with "Image resized" then it was successful.  Now it's time to start Raspbian normally.
qemu-system-arm.exe -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda raspbian.img

During the boot, fsck will generate an error and the boot process will stop at a terminal prompt.  Here's the key step which I found missing from all other online tutorials, you need to rerun the above 90-qemu.rules step.
nano /etc/udev/rules.d/90-qemu.rules
Type the following into this file:
KERNEL=="sda", SYMLINK+="mmcblk0"
KERNEL=="sda?", SYMLINK+="mmcblk0p%n"
KERNEL=="sda2", SYMLINK+="root"

Note that during this part the keyboard is in English UK configuration.  If you try and type doublequotes (") it won't work.  To type doublequotes press shift+2.

Save the file and exit nano.  Then type "exit" and the boot process will resume.  When the Raspbian configuration screen comes up, select Finish to exit.  Type "sudo reboot" to restart Raspbian.  Note that Qemu does not support rebooting a VM, instead it just powers down.  So after Qemu closes, use the above command to rerun Raspbian.


Login using the standard pi / raspberry as the username and password.  Then run:
sudo ln -snf mmcblk0p2 /dev/root

Now we can run the Raspbian configuration utility. by running "sudo raspi-config"  Select "Internationalization Options" and then "Change locale."  Place a check by "en_US.UTF-8 UTF-8."  Again, select "Internationalization Options" and then "Change time zone."  Linux handles time zones differently than Windows.  Instead of selecting your time zone by name or UTC offset, you instead select a region and a city near you.  So for me I select Americas and Los Angeles.  Finally select "Internationalization Options" one more time and then "Change keyboard layout."  For me I selected "Generic 104 key" and "English US."  This step is important as the default keyboard layout is English UK with things like the pound sterling symbol.  Lastly, select "Expand Filesystem."  Reboot to expand

After this Raspbian will be configured and ready to go.  It's a good idea to run "sudo apt-get update" and "sudo apt-get dist-upgrade" to update your image.  From my experience X (the graphical desktop) is very slow and hard to use.  But if all you want to do is terminal stuff this is a great way to emulate a Raspberry Pi.  Also, don't expect to be blown away by speed, even on a fast desktop computer.  The emulator runs about the same speed as a real Raspberry Pi, maybe even a little slower than.

As I said, I figured all this out through a lot of trial and error.  So hopefully someone else finds this useful.  Enjoy, and happy emulating!

No comments:

Post a Comment