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