Sunday, December 14, 2014

Share internet with Virtual RaspberryPI using QEMU

In this article I'm going to show you, how to share host OS internet with emulated raspberry Pi.

Note: I'm on Ubuntu 12.04

Pre-requisites:
 Follow this article and brought your virtual RaspberryPI,  it will have IP but you can't access Internet from RaspberryPI. Ok no worry, that's what this article about. Now shutdown the RaspberryPI, and  do the following in your host OS:

Install brctl, tunctl & libvirt
    brctl — ethernet bridge administration
      sudo apt-get install bridge-utils

    tunctl — create and manage persistent TUN/TAP interfaces
      sudo apt-get install uml-utilities
   

    libvirt — visualization management system. This will automatically setup a DHCP server and a bridge(virbr0).
      sudo apt-get install qemu-kvm libvirt-bin

make sure, dns server & bridge automattically configured:

root@unknown:~# ps -ef | grep dns
121       1377     1  0 Dec13 ?        00:00:00 /usr/sbin/dnsmasq -u libvirt-dnsmasq --strict-order --bind-interfaces --pid-file=/var/run/libvirt/network/default.pid --conf-file= --except-interface lo --listen-address 192.168.122.1 --dhcp-range 192.168.122.2,192.168.122.254 --dhcp-leasefile=/var/lib/libvirt/dnsmasq/default.leases --dhcp-lease-max=253 --dhcp-no-override

root@unknown:~# ifconfig virbr0
virbr0    Link encap:Ethernet  HWaddr 62:80:41:0f:0b:56  
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:619 errors:0 dropped:0 overruns:0 frame:0
          TX packets:685 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:44276 (44.2 KB)  TX bytes:51576 (51.5 KB)  

root@unknown:~# brctl show
bridge name bridge id  STP enabled interfaces 
virbr0  8000.000000000000 yes  

How to:  
  1. Bind eth0 to virbr0
  2. create a tap interface and make it up
  3. Bind tap0 to virbr0
  4. make sure it binded succesfully
  5. Brought up the Raspberry Pi with tap0
root@unknown:~# brctl addif virbr0 eth0
root@unknown:~# tunctl -t tap0 && ifconfig tap0 up
Set 'tap0' persistent and owned by uid 0
root@unknown:~# brctl addif virbr0 tap0
root@unknown:~# ifconfig
eth0      Link encap:Ethernet  HWaddr 48:84:3c:c6:33:34  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:20070 errors:0 dropped:0 overruns:0 frame:0
          TX packets:20070 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1697375 (1.6 MB)  TX bytes:1697375 (1.6 MB)

tap0      Link encap:Ethernet  HWaddr 1a:98:19:c5:4b:a8  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:500 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

virbr0    Link encap:Ethernet  HWaddr 78:84:3c:e6:38:98  
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:619 errors:0 dropped:0 overruns:0 frame:0
          TX packets:685 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:44276 (44.2 KB)  TX bytes:51576 (51.5 KB)

wlan0     Link encap:Ethernet  HWaddr cd:af:76:c4:6d:99  
          inet addr:192.168.1.5  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: ff70::ceff:78dd:fdc1:6d95/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1338984 errors:0 dropped:0 overruns:0 frame:0
          TX packets:997145 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1521507128 (1.5 GB)  TX bytes:142971835 (142.9 MB)

root@unknown:~# brctl show
bridge name bridge id  STP enabled interfaces
virbr0  8000.1a9819c54ba8 yes  eth0
       tap0
root@unknown:~# qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda 2014-09-09-wheezy-raspbian.img -net nic,macaddr=00:16:3e:00:00:01 -net tap,ifname=tap0,script=no,downscript=no

RaspberryPI interface set to DHCP:
pi@vrpi:~$ cat /etc/network/interfaces 
auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
pi@vrpi:~$ 
Screenshot of Virtual-Raspberry-Pi: