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