6. Connecting Qemu to network

Previously, we have Built and booted U-Boot in Qemu as in this post.  Now, We are going to connect the Qemu to the virtual network for further usage. This is based on this post. We are going to connect Qemu to Network bridge and TUN/TAP For this, we need the following installed in out ubuntu machine

$ sudo apt-get install bridge-utils uml-utilities openvpn firestarter

Insert the TUN device module to the kernel

$ sudo modprobe tun

The node ?tun? must have been automatically created in /dev/net folder. If not try to create it manually by typing the following commands

$ sudo mkdir /dev/net
$ sudo mknod /dev/net/tun c 10 200
$ sudo chmod 660 /dev/net/tun

Our machine has the ethernet or Wireless Lan connection. In case of Ethernet connection, it should be named like eth0. We need to tap network from this to our virtual network, tap0. We can do it using following scripts.

Contents of /etc/qemu-ifup

#!/bin/sh

#
# script to bring up the tun device in QEMU in bridged mode
# first parameter is name of tap device (e.g. tap0)
#
# some constants specific to the local host ? change to suit your host
#

ETH0IP=10.0.2.15
GATEWAY=10.0.2.2
BROADCAST=10.0.2.255

#
# First take eth0 down, then bring it up with IP 0.0.0.0
#
ifconfig eth0 down
ifconfig eth0 0.0.0.0 promisc up

#
# Bring up the tap device (name specified as first argument, by QEMU)
#
openvpn --mktun --dev $1 -user 'id -un'
ifconfig $1 0.0.0.0 promisc up

#
# create the bridge between eth0 and the tap device
#
brctl addbr br0
brctl addif br0 eth0
brctl addif br0 $1

#
# only a single bridge so loops are not possible, turn off spanning
# tree protocol
#
brctl stp br0 off

#
# Bring up the bridge with ETH0IP and add the default route
#
ifconfig br0 $ETH0IP netmask 255.255.0.0 broadcast $BROADCAST
route add default gw $GATEWAY

#
# stop firewall ? comment this out if you don?t use Firestarter
#
service firestarter stop

Here, you may need to edit the following according to your network configurations.

ETH0IP=10.0.2.15 		
GATEWAY=10.0.2.2		
BROADCAST=10.0.2.255

Save the file as executables. Current ifconfig shoud return something like this.

$ ifconfig
eth0	  Link encap:Ethernet  HWaddr 08:00:27:d6:16:37  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fed6:1637/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:78 errors:0 dropped:0 overruns:0 frame:0
          TX packets:156 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:12215 (12.2 KB)  TX bytes:22482 (22.4 KB)

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:42 errors:0 dropped:0 overruns:0 frame:0
          TX packets:42 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:3354 (3.3 KB)  TX bytes:3354 (3.3 KB)

Now, we can run the script to connect bridge the network.

raj@raj-VirtualBox:~$ sudo /etc/qemu-ifup tap0
[sudo] password for raj: 
Tue May 21 10:29:29 2013 TUN/TAP device tap0 opened
Tue May 21 10:29:29 2013 Persist state set to: ON
raj@raj-VirtualBox:~$

If you check the ifconfig now, it should look like below with br0 and tap0 added..

raj@raj-VirtualBox:~$ ifconfig 
br0       Link encap:Ethernet  HWaddr 08:00:27:d6:16:37  
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.0.0
          inet6 addr: fe80::a00:27ff:fed6:1637/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
          TX packets:33 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:328 (328.0 B)  TX bytes:7126 (7.1 KB)

eth0      Link encap:Ethernet  HWaddr 08:00:27:d6:16:37  
          inet6 addr: fe80::a00:27ff:fed6:1637/64 Scope:Link
          UP BROADCAST RUNNING PROMISC MULTICAST  MTU:1500  Metric:1
          RX packets:118 errors:0 dropped:0 overruns:0 frame:0
          TX packets:267 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:17802 (17.8 KB)  TX bytes:40277 (40.2 KB)

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:150 errors:0 dropped:0 overruns:0 frame:0
          TX packets:150 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:10986 (10.9 KB)  TX bytes:10986 (10.9 KB)

tap0      Link encap:Ethernet  HWaddr 9e:43:58:5d:bb:3f  
          UP BROADCAST PROMISC 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:100 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

raj@raj-VirtualBox:~$

Now, let us test the setup with qemu. For this, we need the u-boot.bin that we generated using this.

sudo qemu-system-arm -M versatilepb -nographic \
-net nic -net tap,ifname=tap0 -kernel u-boot.bin

This should give Uboot prompt as below.

U-Boot 2010.03-dirty (May 02 2013 - 10:00:51)

DRAM:   0 kB
## Unknown FLASH on Bank 1 - Size = 0x00000000 = 0 MB
Flash:  0 kB
*** Warning - bad CRC, using default environment

In:    serial
Out:   serial
Err:   serial
Net:   SMC91111-0
VersatilePB #

Now, we have to set an IP address for the qemu system. This can be done by running this command in uboot prompt

VersatilePB # setenv ipaddr 10.0.2.3

Note down your machines IP and now, We can test the connection by doing a ping to our machine.

VersatilePB # ping 10.0.2.15
SMC91111: PHY auto-negotiate timed out
SMC91111: MAC 52:54:00:12:34:56
Using SMC91111-0 device
host 10.0.2.15 is alive
VersatilePB #

Voila!! your system is connected if its telling the host is alive..