debian, linux, networking, qemu, raspberrypi

Raspbian on QEMU with network access

This is a little guide to emulate the Raspbian operating system for ARM on QEMU with network connectivity.

I found most resources on this outdated, relying on broken links, and lacking the steps for network access, so I reviewed them and streamlined the process.

Long story short

If you haven’t yet, install QEMU on your system. On my Arch machine this means

on Debian based

Then, clone my qemu-raspbian-network repository, download a raspbian image and launch qemu-pi.sh

If you want network access, edit qemu-pi.sh  line 30 and set

If you do this, the script will setup a bridge called br0  on your enp3s0  interface, and restore your routes after QEMU exits. If you want to modify this behaviour, change the relevant lines in the script.

Now your Raspbian image will have network connectivity, so you can SSH to it, and apt-get  from it.

For QEMU to have access to the network bridge configuration, this needs to be in /etc/sudoers . Access with sudo visudo.

If you ever need more disk space, you can

When you are done modifying the image, you can dd  it to an SD card and run it directly on a Raspberry Pi.

Remember to use <CTRL><ALT>g in order to regain mouse control on QEMU.

Long story long

The Raspbian operating system is a Debian based GNU/Linux distribution that targets the Raspberry Pi board.

It can be convenient to play around with the OS without the need for a Rasberry Pi or an SD card. For this, we want to run it on the QEMU virtual machine.

The original Raspberry Pi has an ARM11 (ARMv6) processor, RPi2 has an ARM Cortex-A7, and RPi3 has an ARM Cortex-A53. I recommend going easy for the arm1136  or arm1176 . To see what your qemu-system-arm can emulate run

The problem is that the kernel that ships with Raspbian is taylored for the Raspberry Pi board, which is not supported by QEMU.

For this reason, the kernel needs to be patched and cross-compiled in order to be run on the ARM Versatile development board, which is supported by QEMU. I included a modified kernel 4.4.34 is in my repo.

After this, it is a matter of invoking qemu with the -kernel  option to replace the one that comes with Raspbian, and the  -M versatilepb  option to specify the emulated hardware.

Then, we are only left with the task of interfacing the block devices that raspbian expects, which are of the form mmcblk0 to what it will receive from the QEMU virtual block device, which come on the form sda . That is what the following lines of the script do

In order to achieve network connectivity, it is common to use TUNTAP with QEMU.

TUNTAP creates a virtual network interface that can be accesed by a hypervisor. All packets sent to this virtual interface will appear on the hypervisor’s network stack and viceversa, allowing communication with the virtual machine through the regular network interface.

Therefore, we will include both the real interface and the TUNTAP interface on a network bridge, which will cause any packet sent to the bridge from outside to appear on both interfaces, and any packet sent from inside will appear as if it originates on the bridge.

QEMU requires hooks on /etc/qemu-ifup  and /etc/qemu-ifdown  to manage the TUNTAP interface, which is dealt with by the following lines taken from the Arch wiki.

Cross-compile or get a newer kernel

The first ones to share this work as far as I know were Xecdesign, for the 3.10.25 kernel. At some point, their website went down, so Dhruv Vyas rescued the kernel patches and has been providing newer kernels as well as cross-compiling instructions on his github.

Code

github

This was tested on Arch Linux (qemu-arm version 2.8.0), and only some parts on a Debian container.

In the next post we will do something useful with this.

Author: nachoparker

Humbly sharing things that I find useful
[ github dockerhub ]

29 Comments on “Raspbian on QEMU with network access

    1. Mmmm I didn’t encounter that error.

      What distro are you using? What version of qemu ? What CPU are you emulating?

      1. Thanks for the guide! I ran into this same issue. After “Attempting to kill init” the terminal output says it’s going down for reboot. I’m using Ubuntu 16.04 . When I ran your apt-get command it couldn’t find qemu-kvm-extras. From what I could find (http://packages.ubuntu.com/search?keywords=qemu-kvm-extras) it looks like the -extras package was only transitional for 12.04

        Otherwise:


        1. Thanks for the feedback, I updated that bit.

          As it says in the post, I have only tested it througout on Arch.

          my version of qemu-arm is 2.8.0, unfortunatedly I have not tested it with 2.5.0

          Maybe you can try

          There is many guides on compiling QEMU out there 😉

          Let us know if you manage to get it working

        2. Nacho, Thanks for sticking with me. You’re probably right that recompiling QEMU would have been a good fix. I really need to switch over to arch one of these days.

          I was able to get it working be recompiling the kernel following Dhruv Vyas’s instructions. I also made some of the file system changes that his guide recommends. I’m still using your qemu-pi.sh to start the QEMU session.

          Thanks again for the guide and for collecting the pieces necessary to get this working.

        3. Awesome!

          It is a bit weird because I just use Dhruv Vyas’s kernel as well.

          For the record… What version of the kernel did you compile? what changes did you have to do?

          It looks like maybe there is something with QEMU 2.5.0 and RPi kernel 4.34?

          I will investigate this when I have time

  1. Thanks for the great HOWTO, unfortunately my machine fails with:
    VFS: Cannot open root device “sda/2” or unknown-block(0,0): error -6
    What can I do?

    1. did you follow all the steps carefully? what’s the output of

      What distro are you using? What CPU are you emulating?

  2. Hi,

    Thank you for this script and tutorial. I have managed to boot into raspbian, but can’t get network to work.
    This is config from qemu-pi.sh:

    My interface name is enp1s0 and MAC is the one written.

    Error is no IP found for enp1s0 altough I have an IP from DHCP.

    Can you help me with this issue?

    Best regards

    1. sure, the line that is failing is

      , so let’s see… what is the output of

  3. Hi,
    Thank you for the script!
    I’m using Ubuntu to emulate raspbian in QEMU.
    But when I assign no_network=0, it should have an ip address similar to the host network. It doesn’t work.

    And the output for ip address is,

    1. Hello,

      Let me explain what should happen, to make sure we are in the same page.

      After you run qemu-pi.sh and while it is running, a new brigde interface br0 should appear in ip address with the same IP and routing that the host computer had at the moment of running qemu-pi.sh. Also a new interface tap0.

      Then, inside Raspbian, you have an eth0 interface. If you have DHCP in your network it will get the IP and gateway from there. Otherwise you have to configure a static IP, DNS and gateway yourself.

      Your emulated Raspbian will not and should not have the same IP address as the host. That would not work, for the similar reasons that you cannot have two machines with the same IP in a network.

      In any case, can you show me the output of

      before running qemu-pi.sh and while it is running?

      See my output

      before I run QEMU

      after

      My routing while running QEMU goes through the bridge interface br0

      When you close QEMU it should go back to normal.

      1. Before I run QEMU,



        After running the QEMU,


        The message I get while running the QEMU,

        1. Somehow I fixed the problem. Apparently the latest ubuntu kernel doesn’t have tun. Instead it has tunctl.
          There is a issue with the host’s network. Internet doesn’t work in Ubuntu(like web-browsers). Surprisingly, it works inside QEMU raspbian.
          Inside QEMU, I run into this error all the time.

          pi@rpi00:~ $ cat /etc
          bash: cannot create temp file for here-document: No space left on device

          I have already expanded the raspbian image to +4G. And my computer has at-least 100Gb of free space.

  4. Hi Nacho, thanks a lot for your scripts!
    See that “qemu-pi.sh” is from end of april. Is that meaning that it runs good on Ubuntu/Mint?
    Specific qemu-arm -version and qemu-arm -cpu-version?
    Thanks a lot.

      1. Hello! Nice job!
        I write only two things I modified to work on my Ubuntu 16.04.2 LTS…
        1) tun is not a module (at least in my distro) so I changed the script in the check:
        #modprobe tun &>/dev/null #original
        #grep -q tun /dev/null || { echo “need tun module” ; NO_NETWORK=1; } #modified
        [if you don’t have tun you have only to create it :
        sudo mkdir /dev/net (if it doesn’t exist already)
        sudo mknod /dev/net/tun c 10 200
        sudo. chmod 0666 /dev/net/tun
        ]
        2) I noticed wlan0 (wireless) is not bridgeable, eth0 (ethernet) yes , I failed so I added a fallback for network with
        “-redir tcp:2222::22” to access ssh from host machine ( ssh -p 2222 pi@localhost )
        Maybe this will help somebody else.

  5. Hi,

    Worked like a charm! 🙂 Many thanks! However, my main interest in the emulator is to use Mathematica and the image
    2017-04-10-raspbian-jessie.img (which works fine) comes with one. The problem is, I don’t have a product key. Do you have a key or a hint about activating Mathematica you might share? Thanks a lot!

    1. This website is all about do it yourself and open source.

      Propietary software discussions and very specifically pirating licensed work are not encouraged, nor welcome here.

      1. No piracy here I’m afraid. To the best of my knowledge, Raspbian comes with a bundled copy of Mathematica. I had another Raspbian version (wheezy) whose copy of Mathematica worked out of the box. No activation key needed. I downloaded jessie waiting, in good faith, that it would work similarly (and legally). As I said jessie comes with Mathematica but this time an activation key is required. Beats me. Why is it different from wheezy? I thought you might know.

        I may be misinformed about a (possibly) new policy with regard to Raspbian & Mathematica, but you don’t have to be anal about that. You don’t know me after all. Once again, thanks for the neat set of instructions. They worked beautifully.

        1. I apologize, I totally read that wrong.

          I don’t know anything about that license change, sorry.

          Thank you for your words and positive feedback

  6. Hi Nachoparker!! Thank you very much for your tutorial, it was very useful to start a Raspbian emulation easily (sorry about my English).
    I am new on this, and I want to know how I can use your script with Kernel 4.9.28-v7+. I think I have to cross-compile the Kernel sources (downloaded from kernel.org). Is this the right way?
    I have never cross-compiled anything so I will appreciate if you can guide me with the process.
    Regards from Argentina.

Leave a Reply

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