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