skip bridge interface when guessing physical interface
[tests.git] / system / template-qemu / qemu-bridge-init
1 #!/bin/bash
2
3 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
4 # Copyright (C) 2010 INRIA 
5 #
6 # Establishment of a runtime environment for a
7 # virtual  machine  under QEMU, This script allows the host box
8 # to share its network connection with qemu-based guests
9 #
10
11 COMMAND=$(basename $0)
12 cd $(dirname $0)
13
14 # turn on verbosity
15 set -x
16
17 # constant
18 INTERFACE_BRIDGE=br0
19
20 #################### compute INTERFACE_LAN
21 # use /proc/net/dev instead of a hard-wired list
22 function gather_interfaces () {
23     python <<EOF
24 for line in file("/proc/net/dev"):
25     if ':' not in line: continue
26     ifname=line.replace(" ","").split(":")[0]
27     if ifname.find("lo")==0: continue
28     if ifname.find("br")==0: continue
29     if ifname.find("virbr")==0: continue
30     if ifname.find("tap")==0: continue
31     print ifname
32 EOF
33 }
34     
35 function discover_interface () {
36     for ifname in $(gather_interfaces); do
37         ip link show $ifname | grep -qi 'state UP' && { echo $ifname; return; }
38     done
39     # still not found ? that's bad
40     echo unknown
41 }
42 INTERFACE_LAN=$(discover_interface)
43 echo Using physical interface $INTERFACE_LAN
44
45 ####################
46 # Fonction de mise en place du pont
47 function start () {
48
49     echo "========== $COMMAND: entering start - beg"
50     hostname
51     uname -a
52     ifconfig
53     netstat -rn
54     echo "========== $COMMAND: entering start - end"
55
56     # disable netfilter calls for bridge interface (they cause panick on 2.6.35 anyway)
57     #
58     # another option would be to accept the all forward packages for
59     # bridged interface like: -A FORWARD -m physdev --physdev-is-bridged -j ACCEPT
60     sysctl net.bridge.bridge-nf-call-iptables=0
61     sysctl net.bridge.bridge-nf-call-ip6tables=0
62     sysctl net.bridge.bridge-nf-call-arptables=0
63
64     # take extra arg for ifname, if provided
65     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
66
67     ### Checking
68     type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
69
70     #if we have already configured the same host_box no need to do it again
71     /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
72         echo "Bridge interface $INTERFACE_BRIDGE already set up - $COMMAND start exiting"
73         exit 0
74     }
75     /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
76         echo "Cannot use interface $INTERFACE_LAN - exiting"
77         exit 1
78     }
79
80     #Getting host IP/masklen
81     address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
82     [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
83     
84     broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $4;}')
85     [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
86
87     gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
88     [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
89
90     ### do it
91     #Restarting udev
92     echo "Starting udev ..."
93     /sbin/udevd restart
94     if modprobe kqemu &> /dev/null ; then
95         echo "(bridge-init) kqemu loaded"
96     else
97         echo "(bridge-init) WARNING : Could not modprobe kqemu"
98     fi
99     #Loading the tun/tap model
100     if modprobe tun ; then
101         echo "tun loaded"
102         # Giving read/write access
103         echo "Granting read/write acces to the tun device"
104         chmod 666 /dev/net/tun
105     else
106         echo "Could not modprobe tun - exiting"
107         exit 1
108     fi
109
110     # creating the bridge
111     echo "Creating bridge INTERFACE_BRIDGE=$INTERFACE_BRIDGE"
112     brctl addbr $INTERFACE_BRIDGE
113     #brctl stp $INTERFACE_BRIDGE yes
114     brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
115     echo "Activating promiscuous mode INTERFACE_LAN=$INTERFACE_LAN"
116     /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
117     sleep 2
118     echo "Setting bridge address=$address broadcast=$broadcast"
119     # static
120     /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
121     sleep 1
122         
123     #Reconfigure the routing table
124     echo "Configuring gateway=$gateway"
125     route add default gw $gateway
126
127     echo "========== $COMMAND: exiting start - beg"
128     ifconfig
129     netstat -rn
130     echo "========== $COMMAND: exiting start - end"
131 }
132
133 #Adding a new interface to the bridge: this is used by qemu-ifup 
134 function add () {
135
136     [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
137     INTERFACE_LAN=$1; shift
138
139     echo "========== $COMMAND: entering add - beg"
140     ifconfig
141     netstat -rn
142     echo "========== $COMMAND: entering add - end"
143
144     echo "Activating link for $INTERFACE_LAN..."
145     /sbin/ip link set $INTERFACE_LAN up
146     sleep 1
147     echo "Adding $INTERFACE_LAN to $INTERFACE_BRIDGE"
148     brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
149
150     # turn off filtering on this interface
151     ########## from the test environment
152     # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH
153     CONFIG=qemu.conf
154     [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; }
155     . $CONFIG
156
157     echo "Tweaking iptables"
158     iptables-save > iptables.pre
159     # rewrite a new config - quick and dirty
160     ./iptables.py iptables.pre iptables.post $IP
161     iptables-restore < iptables.post
162
163     echo "========== $COMMAND: exiting add - beg"
164
165     ifconfig
166     netstat -rn
167
168     echo "Installed iptables"
169     iptables-save
170     
171     echo "========== $COMMAND: exiting add - end"
172 }
173
174 #Stop the bridge and restore the original setting
175 function stop () {
176     # take extra arg for ifname, if provided
177     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
178
179     ### Checking
180     type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
181
182     /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null || {
183         echo "Bridge interface $INTERFACE_BRIDGE does not exist - $COMMAND stop exiting"
184         exit 0
185     }
186     brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
187     /sbin/ifconfig $INTERFACE_BRIDGE down
188     brctl delbr $INTERFACE_BRIDGE
189     /sbin/service network restart
190     /sbin/service iptables restart
191 }
192
193 function main () {
194
195     case "$1" in
196         start)
197             shift; start "$@" ;;
198         stop)
199             shift; stop "$@" ;;
200         add)
201             shift; add "$@" ;;
202         *)
203             echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;
204     esac
205     exit 0
206 }
207
208 # redirect stderr as well
209 main "$@" 2>&1