Merge branch 'master' of ssh://git.onelab.eu/git/tests
[tests.git] / system / template-qemu / qemu-bridge-init
1 #!/bin/bash
2
3 # Establishment of a runtime environment for a
4 # virtual  machine  under QEMU, This script allows the host box
5 # to share its network connection with qemu-based guests
6 #
7 # Author: Amine chaoui
8 #
9
10 COMMAND=$(basename $0)
11 cd $(dirname $0)
12
13 # turn on verbosity
14 set -x
15
16 # constant
17 INTERFACE_BRIDGE=br0
18 # Default Value
19 INTERFACE_LAN=eth0
20
21 # Fonction de mise en place du pont
22 start () {
23
24     echo "========== $COMMAND: entering start - beg"
25     hostname
26     uname -a
27     ifconfig
28     netstat -rn
29     echo "========== $COMMAND: entering start - end"
30
31     # take extra arg for ifname, if provided
32     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
33
34     ### Checking
35     type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
36
37     #if we have already configured the same host_box no need to do it again
38     /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
39         echo "Bridge interface $INTERFACE_BRIDGE already set up - $COMMAND start exiting"
40         exit 0
41     }
42     /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
43         echo "Cannot use interface $INTERFACE_LAN - exiting"
44         exit 1
45     }
46
47     #Getting host IP/masklen
48     address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
49     [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
50     
51     broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $4;}')
52     [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
53
54     gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
55     [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
56
57     ### do it
58     #Restarting udev
59     echo "Starting udev ..."
60     /sbin/udevd restart
61     if modprobe kqemu &> /dev/null ; then
62         echo "(bridge-init) kqemu loaded"
63     else
64         echo "(bridge-init) WARNING : Could not modprobe kqemu"
65     fi
66     #Loading the tun/tap model
67     if modprobe tun ; then
68         echo "tun loaded"
69         # Giving read/write access
70         echo "Granting read/write acces to the tun device"
71         chmod 666 /dev/net/tun
72     else
73         echo "Could not modprobe tun - exiting"
74         exit 1
75     fi
76
77     # creating the bridge
78     echo "Creating bridge INTERFACE_BRIDGE=$INTERFACE_BRIDGE"
79     brctl addbr $INTERFACE_BRIDGE
80     #brctl stp $INTERFACE_BRIDGE yes
81     brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
82     echo "Activating promiscuous mode INTERFACE_LAN=$INTERFACE_LAN"
83     /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
84     sleep 2
85     echo "Setting bridge address=$address broadcast=$broadcast"
86     # static
87     /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
88     sleep 1
89         
90     #Reconfigure the routing table
91     echo "Configuring gateway=$gateway"
92     route add default gw $gateway
93
94     echo "========== $COMMAND: exiting start - beg"
95     ifconfig
96     netstat -rn
97     echo "========== $COMMAND: exiting start - end"
98 }
99
100 #Adding a new interface to the bridge: this is used by qemu-ifup 
101 add () {
102
103     [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
104     INTERFACE_LAN=$1; shift
105
106     echo "========== $COMMAND: entering add - beg"
107     ifconfig
108     netstat -rn
109     echo "========== $COMMAND: entering add - end"
110
111     echo "Activating link for $INTERFACE_LAN..."
112     /sbin/ip link set $INTERFACE_LAN up
113     sleep 1
114     echo "Adding $INTERFACE_LAN to $INTERFACE_BRIDGE"
115     brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
116
117     # turn off filtering on this interface
118     ########## from the test environment
119     # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH
120     CONFIG=qemu.conf
121     [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; }
122     . $CONFIG
123
124     echo "Tweaking iptables"
125     iptables-save > iptables.pre
126     # rewrite a new config - quick and dirty
127     ./iptables.py iptables.pre iptables.post $IP
128     iptables-restore < iptables.post
129
130     echo "========== $COMMAND: exiting add - beg"
131
132     ifconfig
133     netstat -rn
134
135     echo "Installed iptables"
136     iptables-save
137     
138     echo "========== $COMMAND: exiting add - end"
139 }
140
141 #Stop the bridge and restore the original setting
142 stop () {
143     # take extra arg for ifname, if provided
144     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
145
146     ### Checking
147     type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
148
149     /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null || {
150         echo "Bridge interface $INTERFACE_BRIDGE does not exist - $COMMAND stop exiting"
151         exit 0
152     }
153     brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
154     /sbin/ifconfig $INTERFACE_BRIDGE down
155     brctl delbr $INTERFACE_BRIDGE
156     /sbin/service network restart
157     /sbin/service iptables restart
158 }
159
160 function main () {
161
162     case "$1" in
163         start)
164             shift; start "$@" ;;
165         stop)
166             shift; stop "$@" ;;
167         add)
168             shift; add "$@" ;;
169         *)
170             echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;
171     esac
172     exit 0
173 }
174
175 # redirect stderr as well
176 main "$@" 2>&1