#!/bin/sh NUMBER_OF_VM=1 USERNAME=root if [ $2 ] ; then NVM=$2 let NUMBER_OF_VM=NVM-1 fi if [ $3 ] ; then USERNAME=$3 fi case "$1" in start) # Detect the gateway echo -n "Searching for configured gateway... " CURRENT_GW=`route -n | grep UG | sed s/' *'/' '/g | cut -d' ' -f2` if [ "$CURRENT_GW" ]; then echo "$CURRENT_GW" else echo "cannot find gateway: stop." exit 1; fi # Interface associated with the gateway CURRENT_IF=`route -n | grep UG | sed s/' *'/' '/g | cut -d' ' -f8` echo "Associated interface: $CURRENT_IF" # Retrieve interface configuration echo -n "Searching for interface address... " CURRENT_IP=`ifconfig $CURRENT_IF | grep 'inet addr' | cut -d: -f2 | cut -d' ' -f1` if [ "$CURRENT_IP" ]; then echo "$CURRENT_IP" else echo "cannot find address: stop." exit 1; fi echo "Creating the bridge: br0" brctl addbr br0 echo -n "Adding interfaces:" NB=0 while [ $NB -le $NUMBER_OF_VM ] do echo -n " tap$NB" /usr/lib/virtualbox/VBoxTunctl -t tap$NB -u $USERNAME > /dev/null ifconfig tap$NB up brctl addif br0 tap$NB let NB=NB+1 done echo "" echo "Setting $CURRENT_IF in promisc mode" ifconfig $CURRENT_IF 0.0.0.0 promisc echo "Adding $CURRENT_IF to the bridge" brctl addif br0 $CURRENT_IF echo "Configuring bridge with address $CURRENT_IP" ifconfig br0 $CURRENT_IP echo "Adding route to gateway $CURRENT_GW" route add default gw $CURRENT_GW echo "Done!" ;; stop) # Detect the gateway echo -n "Searching for configured gateway... " CURRENT_GW=`route -n | grep UG | sed s/' *'/' '/g | cut -d' ' -f2` if [ "$CURRENT_GW" ]; then echo "$CURRENT_GW" else echo "cannot find gateway: stop." exit 1; fi # Interface associated with the gateway CURRENT_IF=`route -n | grep UG | sed s/' *'/' '/g | cut -d' ' -f8` echo "Associated interface: $CURRENT_IF" if [ "$CURRENT_IF" != "br0" ]; then echo "Associated interface is not the bridge br0: stop." exit 1; fi # Retrieve interface configuration echo -n "Searching for bridge address... " CURRENT_IP=`ifconfig $CURRENT_IF | grep 'inet addr' | cut -d: -f2 | cut -d' ' -f1` if [ "$CURRENT_IP" ]; then echo "$CURRENT_IP" else echo "cannot find bridge address: stop." exit 1; fi IFACES=`brctl show | tail -n +2 | sed 's/\t\+/\n/g' | grep -v '^$' | tail -n +4` echo "Shutting down bridge br0" ifconfig br0 down echo "Deleting bridge br0" brctl delbr br0 for IFACE in $IFACES ; do PREFIX=`echo $IFACE | cut -b1-3` case "$PREFIX" in tap) echo "Deleting interface: $IFACE" /usr/lib/virtualbox/VBoxTunctl -d $IFACE &> /dev/null ;; eth) #brctl delif $IFACE echo "Configuring interface $IFACE with address $CURRENT_IP" ifconfig $IFACE $CURRENT_IP -promisc echo "Adding route to gateway $CURRENT_GW" route add default gw $CURRENT_GW ;; esac done echo "Done!" ;; *) echo "Usage: vbox {start [number of virtual interfaces] [user] | stop}" ;; esac