1749d6f38b6ba2c6599d4e34e6c3f6c3b92ffcd2
[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 # Default Value for INTERFACE_LAN
21 # let's try to figure out the interface to use - try these in order
22 IFNAMES="eth0 eth1 eth2 eth3"
23 function discover_interface () {
24     for ifname in $IFNAMES; do
25         ip link show $ifname | grep -q UP && { INTERFACE_LAN=$ifname; return; }
26     done
27     # still not found ? that's bad
28     INTERFACE_LAN=unknown
29 }
30 discover_interface
31 echo $INTERFACE_LAN
32
33 # Fonction de mise en place du pont
34 function start () {
35
36     echo "========== $COMMAND: entering start - beg"
37     hostname
38     uname -a
39     ifconfig
40     netstat -rn
41     echo "========== $COMMAND: entering start - end"
42
43     # disable netfilter calls for bridge interface (they cause panick on 2.6.35 anyway)
44     #
45     # another option would be to accept the all forward packages for
46     # bridged interface like: -A FORWARD -m physdev --physdev-is-bridged -j ACCEPT
47     sysctl net.bridge.bridge-nf-call-iptables=0
48     sysctl net.bridge.bridge-nf-call-ip6tables=0
49     sysctl net.bridge.bridge-nf-call-arptables=0
50
51     # take extra arg for ifname, if provided
52     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
53
54     ### Checking
55     type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
56
57     #if we have already configured the same host_box no need to do it again
58     /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
59         echo "Bridge interface $INTERFACE_BRIDGE already set up - $COMMAND start exiting"
60         exit 0
61     }
62     /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
63         echo "Cannot use interface $INTERFACE_LAN - exiting"
64         exit 1
65     }
66
67     #Getting host IP/masklen
68     address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
69     [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
70     
71     broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $4;}')
72     [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
73
74     gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
75     [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
76
77     ### do it
78     #Restarting udev
79     echo "Starting udev ..."
80     /sbin/udevd restart
81     if modprobe kqemu &> /dev/null ; then
82         echo "(bridge-init) kqemu loaded"
83     else
84         echo "(bridge-init) WARNING : Could not modprobe kqemu"
85     fi
86     #Loading the tun/tap model
87     if modprobe tun ; then
88         echo "tun loaded"
89         # Giving read/write access
90         echo "Granting read/write acces to the tun device"
91         chmod 666 /dev/net/tun
92     else
93         echo "Could not modprobe tun - exiting"
94         exit 1
95     fi
96
97     # creating the bridge
98     echo "Creating bridge INTERFACE_BRIDGE=$INTERFACE_BRIDGE"
99     brctl addbr $INTERFACE_BRIDGE
100     #brctl stp $INTERFACE_BRIDGE yes
101     brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
102     echo "Activating promiscuous mode INTERFACE_LAN=$INTERFACE_LAN"
103     /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
104     sleep 2
105     echo "Setting bridge address=$address broadcast=$broadcast"
106     # static
107     /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
108     sleep 1
109         
110     #Reconfigure the routing table
111     echo "Configuring gateway=$gateway"
112     route add default gw $gateway
113
114     echo "========== $COMMAND: exiting start - beg"
115     ifconfig
116     netstat -rn
117     echo "========== $COMMAND: exiting start - end"
118 }
119
120 #Adding a new interface to the bridge: this is used by qemu-ifup 
121 function add () {
122
123     [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
124     INTERFACE_LAN=$1; shift
125
126     echo "========== $COMMAND: entering add - beg"
127     ifconfig
128     netstat -rn
129     echo "========== $COMMAND: entering add - end"
130
131     echo "Activating link for $INTERFACE_LAN..."
132     /sbin/ip link set $INTERFACE_LAN up
133     sleep 1
134     echo "Adding $INTERFACE_LAN to $INTERFACE_BRIDGE"
135     brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
136
137     # turn off filtering on this interface
138     ########## from the test environment
139     # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH
140     CONFIG=qemu.conf
141     [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; }
142     . $CONFIG
143
144     echo "Tweaking iptables"
145     iptables-save > iptables.pre
146     # rewrite a new config - quick and dirty
147     ./iptables.py iptables.pre iptables.post $IP
148     iptables-restore < iptables.post
149
150     echo "========== $COMMAND: exiting add - beg"
151
152     ifconfig
153     netstat -rn
154
155     echo "Installed iptables"
156     iptables-save
157     
158     echo "========== $COMMAND: exiting add - end"
159 }
160
161 #Stop the bridge and restore the original setting
162 function stop () {
163     # take extra arg for ifname, if provided
164     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
165
166     ### Checking
167     type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
168
169     /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null || {
170         echo "Bridge interface $INTERFACE_BRIDGE does not exist - $COMMAND stop exiting"
171         exit 0
172     }
173     brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
174     /sbin/ifconfig $INTERFACE_BRIDGE down
175     brctl delbr $INTERFACE_BRIDGE
176     /sbin/service network restart
177     /sbin/service iptables restart
178 }
179
180 function main () {
181
182     case "$1" in
183         start)
184             shift; start "$@" ;;
185         stop)
186             shift; stop "$@" ;;
187         add)
188             shift; add "$@" ;;
189         *)
190             echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;
191     esac
192     exit 0
193 }
194
195 # redirect stderr as well
196 main "$@" 2>&1