Emulating Raspberry Pi on Linux

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

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:

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:

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:

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

So far, your folder structure should like this

So far, your folder structure should like this

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

After saving the file, make sure you make it executable by the command below:

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:

Raspberry Pi terminal prompt waiting for your next action!

Raspberry Pi terminal prompt waiting for your next action!

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:

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.

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

Bonus

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!

Saeid Yazdani

An Electronics Engineer with passion in Embedded Systems, Wireless Sensor Networks, Assembly, Desktop and Mobile Development, Web Development and generally anything that has to do with hardware and software of any kind!

70 Comments:

  1. Nice! Now on to making an emulated Gertboard! 😀

  2. Hi, could I get some help?
    Terminal won’t tell me what version I have or
    $git clone https://github.com/dhruvvyas90/qemu-rpi-kernel.git
    doesn’t work either!

    Thanks

    • Can you please tell how it does not work? you don’t get the kernel downloaded? reply me with the exact error message if any.

      • Qemu-system-arm –version
        The program ‘qemu-system-arm ‘ is currently not installed. You can install it by typing:
        sudo apt-get install qemu-system-arm

        • Ok, the system is kind enough to tell us qemu-system-arm is not installed.

          You can install it by the following commands:

          $sudo apt-get update
          $sudo apt-get install qemu-system

  3. Hi, could I get some help?
    $chmod +x config
    doesn’t work either!

    Thanks

    • I don’t see why wouldn’t chmod command work? please do ‘ls -l’ to see if you are the owner of the file! if not use with ‘sudo’.

      • I saved the text in my text editor as config. Put it in raspberry-pi folder, chmod and sudo chmod not working, sir!

        • I really cant tell what is wrong unless I see a kind of an error message! “chmod +x filename” simply makes the file executable! if it fails I guess it should return an error message into the terminal. Did you see my video, or you are following only the text here in the website? after the chmod you can run the file by typing “./config” assuming you are still in the same folder that contains the “config” file. Please upload some screenshots and put links here so I can see whats going on! SIR!

          • Your Sir, I’m pupil. I’m so chuffed I got this far. I’m 45 years young and still learning. If you can tell me how to post or where to post some images , that would be great. The video moves to fast and is a pain to keep pausing etc. I’m really grateful that you are taking time to help. I put the config file in the raspberry-pi folder! Don’t know why chmod don’t work. I’m using ubuntu 15.10 kind Regards Mike 🙂

        • Ok no worries! please use this website to upload your images and paste the link as comment here.

          http://postimage.org/

          What I am interested to see, is the result of following commands (assuming you are in raspberry directory that you are trying to setup). You can either paste the text here or upload image…whichever works better for you!


          $ls -l
          $cat config

          • mike@mike:~$ ~/work
            bash: /home/mike/work: No such file or directory
            mike@mike:~$ ~/Work
            bash: /home/mike/Work: Is a directory
            mike@mike:~$ ls -l
            total 52
            drwxr-xr-x 2 mike mike 4096 Mar 5 00:08 Desktop
            drwxr-xr-x 2 mike mike 4096 Mar 4 20:50 Documents
            drwxr-xr-x 2 mike mike 4096 Mar 4 23:12 Downloads
            drwxr-xr-x 2 mike mike 4096 Mar 5 19:02 dwhelper
            -rw-r–r– 1 mike mike 8980 Mar 4 18:16 examples.desktop
            drwxr-xr-x 2 mike mike 4096 Mar 4 20:50 Music
            drwxr-xr-x 2 mike mike 4096 Mar 5 18:10 Pictures
            drwxr-xr-x 2 mike mike 4096 Mar 4 20:50 Public
            drwxr-xr-x 2 mike mike 4096 Mar 4 20:50 Templates
            drwxr-xr-x 2 mike mike 4096 Mar 4 20:50 Videos
            drwxrwxr-x 3 mike mike 4096 Mar 5 16:29 Work
            mike@mike:~$

        • One more thing. I hope you are not typing in the “$” sign at beginning of your commands…$ just means you have to perform the given command on terminal!

        • Chika okorie

          Hello Michael…..I also had the same problem. this is how I resolved it. when you click on save, just save it on the directory it takes you to. Don’t save it elsewhere. that was what I did, and it finally worked. I hope yours will too

    • You pasted for me the content of your home directory! I want to see what is in /home/mike/Work/raspberry-pi directory

      I think you got one thing wrong. When I say “$” in my article it means you have to be on command prompt. You are trying to chmod the file which is in anothr directory, but you are running the chmod command in your home directory. Do like this (I think your config is in /home/mike/Work/raspberry-pi


      cd /home/mike/Work/raspberry-pi
      chmod +x config

  4. Hello Saeid, I can now boot rpi.img, and after a moment it reboots in 1 second and disappears??

    • That does not sound good. I think you havent run it with config file. It could be that in the config file you have given the wrong path for kernel file.

  5. https://www.youtube.com/watch?v=K8R8JGfP8sQ

    Hello Saeid, I’ve made a small video, if you can watch and tell me what you think.

    Mike

    • Ok, make another one and show the content of the config file as well.

      Also, which version of raspbian did you download? mind you that only Raspbian Wheezy works as far as I tried.

  6. Hello Saeid, I downloaded Wheezy from a torrent site and have booted successfully!

    • Thanks god 😀 enjoy!

      • Hi Saeid qemu emulator can be opened successfully but I get some erorrs on my terminal which says –
        pulseaudio: set_sink_input_volume() failed
        pulseaudio: Reason: Invalid argument
        pulseaudio: set_sink_input_mute() failed
        pulseaudio: Reason: Invalid argument
        Uncompressing Linux… done, booting the kernel.

        Is that okay to ignore the erorrs or are they just warnings?

  7. Scott Rossell

    Saeid, thank you for all the detailed instructions. I was able to get Wheezy running on Debian 8.3 in Parallels on Mac. I’m very curious to see the next steps you mentioned. In similar configurations I’ve encountered on the web, GUI access is accomplished by employing a VNC connection. I’m curious to see if you take a similar route.

    I bashed my brains out trying to get Jessie working, but it just refuses. Any luck on your end? When can we expect Part 2?

    -Eagerly waiting…

    • Hi, Glad I could help. For Jessie, you would need a qemu compatible kernel…and I don’t know if anyone has bothered making that kind of kernel yet, but google it…

      There are parts II and III of the tutorial already available on my youtube channel. maybe they can help you!

      • Scott Rossell

        Hello again.

        Thanks for the heads up. I followed up on YouTube as suggested.

        The Git source you recommended shows qemu kernels for Jessie, but I have had no luck getting them to function properly. I’ll keep looking.

        Thanks!

  8. Scott Rossell

    Well, I was finally able to get Wheezy to boot up using the Jessie and Wheezy kernels found at https://github.com/dhruvvyas90/qemu-rpi-kernel. I was able to expand the filesystem using raspi-config and update the rpi firmware. I was also able to setup the port forwarding on 2222 to run X11 apps like galculator. I was even able to setup an xorg.conf file and boot to the desktop at 800×600 using startx, but it…freaks out. It laods the wallpaper and trash can an then repeatedly attempts to display a light colored flashing bar across the top of the screen that looks suspiciously like the silhouette of the task bar but has no distinguishing graphics – it’s just a blank, fat, white bar that keeps flashing at the top and moving the trash can down below it. It doesn’t crash though. It will keep doing that as long as you like. Also, once qemu starts up, I can release the cursor from the terminal window, but as soon as I try to click on anything, it immediately jumps back into captured mode. I have to SSH from outside and kill the qemu pid. Any suggestions?

    • Well! I would not trust startx to launch a desktop manager! If you really want to login and be able to do anything you like, I would suggest a remote viewer like team viewer (I am not sure if they got it compiled for ARM). About the flicker…it usually boils down to a crappy video driver.

  9. i downloaded the Raspbian jessie ….when i run the command ./config,,,the qemu terminal opens up than says booting in 1 sec,after that it disappears and nothing happens nxt

  10. Thanks for the information, Saeid. It looks like the Raspbian site only offers Jessie now, so I tried both Jessie-lite and the regular Jessie. Both crashed. The final messages disappear after one second, so I added the options “-no-shutdown” and “-no-reboot” to the qemu-system-run command to keep the screen up. I can send you a screenshot if you like, but the messages include:

    devtmpfs: mounted
    Freeing unused kernel memory: 140K (c049d000 – c04c0000)
    Kernel panic – not syncing: Attempted to kill init! exitcode=0x00000004
    CPU: 0 PID: 1 Comm: bash Not tainted 4.1.13+ #2
    Hardware name: ARM-Versatile PB

    The stack includes: panic do_exit do_group_exit get_signal do_signal do_work_pending

    I sniffed around and found an old version of Wheezy on a different site, and ran that one instead of Jessie. So far that one seems to be working, though I haven’t yet added the steps past the initial boot-up into config mode.

    If Raspbian has indeed dropped Wheezy, you’ll probably get more requests for Jessie support. If you don’t have the time to do that, you might want to at least let people know how to stop the screen from disappearing after a crash so that they can read the error message.

    Even if you don’t have the time to update your instructions, I’m grateful for the work you’ve done so far. It saved me the trouble of figuring it out myself. Thanks again!

    • Hi willdye, The problem you faced is already discussed in other comments. It all boils down to the kernel which is compiled bsaed on Wheezey. To make Jessie to work, you would need a qemu compatible compiled kernel. I have not tried it myself but in the github repo that I have linked the guy has also put kernel for Jessie, someone in the comment had it running with no problem. You are right I should find some time to update to link to a recent kernel and probably update any necessary changes in the tutorial.

  11. This is a good tutorial. for people having the issue where it reboots in 1 second and disappears…I recommend using this raspbian image:
    http://downloads.raspberrypi.org/raspbian/images/raspbian-2015-02-17/

  12. Hi! Thank you very much for your tutorial, great work! it helped me alot. You’ve mentioned usage of KVM for speeding up the emulation, have you managed to run it? I’ve tried -enable-kvm but that doesn’t work, it means that i don’t understand smth. Can you please be so kind as to point me in the right direction?

    • Glad it helped you. Honestly I do not remember if I made it work with KVM or not…but I am sure you can search some stuff regarding this issue on google! good luck!

  13. Well that’s quite helpful, seems like we could do the things with raspbian quite efficiently. keep working and keep posting.. good luck ..

  14. Why do we need a separate kernel image? Doesn’t Raspbian already contain one? (I can’t get it work without)

    • Simply put, it requires a kernel which is compatible with QEMU, as you may know the kernel initializes the hardware (which QEMU is different than actual RPi’s hardware) and has to configure the memory (user space and etc.), I/O and other stuff!

  15. Adil_idrissi

    “Az deedan’et shuma Khush bakht’am”, is this how I should say “Nice to meet you” to you? 🙂

  16. Adil_idrissi

    Is “kernel-qemu-3.10.25-wheezy” inside the qemu-rpi-kernel repository, the same as “/work/raspberry-pi/qemu-rpi-kernel /qemu-kernel”?

  17. Man, You are asome! Best article on Raspberry Pi emulation on Ubuntu(I use Debian) in all Internet! Can You please point to article on enabling GUI for Raspberry Pi?

  18. Deepak Premkumar

    Hi Saeid Yazdani,
    I’m trying to emulate ArchLinux rpi2.img in Ubuntu.
    But it is not working. I tried both wheezy and jessie from the github repo.
    How can I run it? I guess the problem is with the kernel.

    • Yep, without a qemu compatible kernel it will not work, I am not sure if there is a kernel for archlinux…but the wheezy should work what problems did you face with wheezy if you followed my guide?

      • Deepak Premkumar

        The problem was in creating the config file.
        when I give panic=1, it shows rebooting and exits with warning.
        WARNING: Image format was not specified for ‘rpi.img’ and probing guessed raw.
        Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
        Specify the ‘raw’ format explicitly to remove the restrictions.
        Then I gave,
        -drive format=raw,file=rpi.img
        Still it shows rebooting and exits.

  19. Hi
    I would like to test raspbian jessie on my laptop ” debian jessie version 8.6 and kernel 4.5.0 ” but i had some problem when i follow your instraction step by step .
    # when I lanched the command qemu-system-arm -cpu ?
    I get this error :
    No machine specified, and there is no default.
    Use -machine help to list supported machines!
    any seggetion , thx a lot

  20. Qemu 2.8 has a bugfix so you no longer need to comment /etc/ld.so.preload

  21. Yo Saeid, nice work, i got it booted. Do you know how i can somehow bridge my hosts network adapter to the pi so i can get networking up?

  22. I cannot change the locale to de, meither with raspi-config nor with editing /etc/default/keyboard.

  23. Pingback: Emulating raspberry pi using QEMU on Ubuntu – Study notes of XiyuFu

  24. Hi, I faced an error with ./config, it shows below,
    xxxxxxxx:~/work/raspberry-pi$ ./config
    lock: Input/output error
    pulseaudio: pa_context_connect() failed
    pulseaudio: Reason: Connection refused
    pulseaudio: Failed to initialize PA contextaudio: Could not init pa' audio driver
    ALSA lib confmisc.c:768:(parse_card) cannot find card '0'
    ALSA lib conf.c:4248:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
    ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
    ALSA lib conf.c:4248:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
    ALSA lib confmisc.c:1251:(snd_func_refer) error evaluating name
    ALSA lib conf.c:4248:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
    ALSA lib conf.c:4727:(snd_config_expand) Evaluate error: No such file or directory
    ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM default
    alsa: Could not initialize DAC
    alsa: Failed to open
    default’:
    alsa: Reason: No such file or directory
    ALSA lib confmisc.c:768:(parse_card) cannot find card ‘0’
    ALSA lib conf.c:4248:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
    ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
    ALSA lib conf.c:4248:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
    ALSA lib confmisc.c:1251:(snd_func_refer) error evaluating name
    ALSA lib conf.c:4248:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
    ALSA lib conf.c:4727:(snd_config_expand) Evaluate error: No such file or directory
    ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM default
    alsa: Could not initialize DAC
    alsa: Failed to open default':
    alsa: Reason: No such file or directory
    audio: Failed to create voice
    lm4549.out’
    Could not initialize SDL(No available video device) – exiting

  25. Pingback: Emulando a Raspberry Pi no Ubuntu. – Convergência Tecnológica

  26. Yo, I got Jessie to boot. I had to do the changes to ld.so.preload and add 90-qemu.rules *before* it would work. I did that by
    sudo kpartx -a 2017-03-02-raspbian-jessie.img
    sudo mount /dev/mapper/loop1p2 /mnt/temp/
    sudo cp 90-qemu.rules /mnt/temp/lib/udev/rules.d/
    sudo nano /mnt/temp/etc/ld.so.preload
    sudo umount /mnt/temp/
    sudo kpartx -d 2017-03-02-raspbian-jessie.img

  27. I got it to work with Jessie, by altering ld.so.preload and adding 90-qemu.rules *before* booting. I mounted the 2nd partition of the image file and made the alterations on my host system.

  28. hello can you please give me your email address Saeid…?

  29. How to transfer a file from my laptop to the emulated target?

    • I see two options here.
      A. Transfer using Google Drive :p
      B. Mount a file specified as HD or CD:


      $ mkdir -p ~/mnt
      $ sudo mount rpi.img ~/mnt

      Now you can access the files in the virtual machine as ~/mnt.

      At the end:


      $ sudo umount ~/mnt

      Make sure to stop the VM before mounting the image or unexpected things may happen! (Proceed If You Know What You Are Doing (TM))

      I hope I helped.

  30. Hi… got following error while running file ./runme :
    libkmod: Error ../libkmod/libkmod.c:554 kmod_search_moddep: could not open moddep file ‘/lib/modules/3.10.25/modules.dep.bin’
    fsck dies with exit status 6
    failed (code 6)
    [….] File system check failed. A log is being saved in /var/log/fsck/checkfs. Please repair the file system manually. …failed!
    [….] A maintenance shell will now be started. CONTROL-D will terminate this shell…resume system boot. … (warning).
    login: root account is locked , starting shell
    root@raspberrypi:~#

  31. thank you for the great guide!
    i have an image file that comes up but i have an error “failed to mount API filesystem, freezing.”
    can you please help ?

  32. Chaitanya Sonagara

    This is a good article. This is a lockdown period. we can share this with our team members who need hardware to execute the application but not have hardware on hand.

  33. So, the principles of the procedure still apply today I imagine. However this is now something like FIVE years ago, right? Which RPI would you suggest today? NOOBS is still mentioned, but I see Buster, for instance. Thanks…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.