Wednesday, July 11, 2012

Creating and Binding bridge interface with tap and ethernet interface

Here i have written a script which firstly set the interface setting then load a virtual OS for host Ubuntu 12.04
i don't know it will work for other Ubuntu version or not....


start.sh ...

#!/bin/bash

username=$(whoami)
echo "username is $username"
tdevice=$(sudo tunctl -u $username)
bridgename='br0'
ethname='eth0'
tapname='tap0'

echo "bridge= $bridgename"
echo "ethernet = $ethname"
echo "tapname= $tapname"



sudo brctl addbr $bridgename
echo "A bridge has been added"

sudo brctl addif $bridgename $ethname
echo "eth0 is added to bridge"

sudo brctl addif $bridgename $tapname
echo "tap0 is added to bridge"

sudo ifconfig $bridgename up
echo "up bridge interface"

sudo ifconfig $ethname up
echo "up ethernet interface"

sudo ifconfig $tapname up
echo "up tap interface"

sudo ifconfig $bridgename 192.168.68.75/24
echo "Giving IP address manually to bridge interface"

sudo ifconfig $ethname 0.0.0.0 promisc
echo "setting eth at promisc mode"

sudo ifconfig $tapname 0.0.0.0 promisc
echo "setting tap interface at promisc mode"

sudo ip route add default via 192.168.68.254
echo "setting the gateway for bridge"

echo "Starting Ubuntu Server"
kvm -hda ubuntu.vmdk -net nic,model=i82551 -net tap,ifname=tap0,script=no,downscript=no -net nic,model=i82551 -boot c

echo "Turning off Server Oh Yeah!!!"
. off.sh
------------------------------------------------------------------------------------------------------------






off.sh .....
#!/bin/bash

bridgename='br0'
ethname='eth0'
tapname='tap0'

output=$(sudo tunctl -d $tapname)
echo $output
echo "Deleting tap interface"

sudo ifconfig $ethname 192.168.68.75/24 up
echo "Setting eth interface ip address"

sudo ifconfig $bridgename down
echo "Down bridge interface"

sudo brctl delbr $bridgename
echo "Deleting bridge interface"

sudo ip route add default via 192.168.68.254
echo "Adding gateway for eth interface"