use systemctl instead of service
[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     ip address show
53     ip route show
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     ip address show $INTERFACE_BRIDGE &> /dev/null && {
72         echo "Bridge interface $INTERFACE_BRIDGE already set up - $COMMAND start exiting"
73         exit 0
74     }
75     ip address show $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 address 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 address 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=$(ip route show | grep default | awk '{print $3;}')
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     ip link set dev $INTERFACE_LAN promisc on
117     sleep 2
118     echo "Setting bridge address=$address broadcast=$broadcast"
119     # static
120     ip address add $address broadcast $broadcast dev $INTERFACE_BRIDGE
121     # turn on bridge interface
122     ip link set dev $INTERFACE_BRIDGE up
123     ip address del $address dev $INTERFACE_LAN
124     sleep 1
125
126     #Reconfigure the routing table
127     echo "Adding default route via gateway=$gateway on dev $INTERFACE_LAN"
128     ip route add 0.0.0.0/0 via $gateway dev $INTERFACE_BRIDGE
129
130     echo "========== $COMMAND: exiting start - beg"
131     ip address show
132     ip route show
133     echo "========== $COMMAND: exiting start - end"
134 }
135
136 #Adding a new interface to the bridge: this is used by qemu-ifup
137 function add () {
138
139     [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
140     INTERFACE_LAN=$1; shift
141
142     echo "========== $COMMAND: entering add - beg"
143     ip address show
144     ip route show
145     echo "========== $COMMAND: entering add - end"
146
147     echo "Activating link for $INTERFACE_LAN..."
148     /sbin/ip link set $INTERFACE_LAN up
149     sleep 1
150     echo "Adding $INTERFACE_LAN to $INTERFACE_BRIDGE"
151     brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
152
153     # turn off filtering on this interface
154     ########## from the test environment
155     # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH
156     CONFIG=qemu.conf
157     [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; }
158     . $CONFIG
159
160     echo "Tweaking iptables"
161     iptables-save > iptables.pre
162     # rewrite a new config - quick and dirty
163     ./iptables.py iptables.pre iptables.post $IP
164     iptables-restore < iptables.post
165
166     echo "========== $COMMAND: exiting add - beg"
167
168     ip address show
169     ip route show
170
171     echo "Installed iptables"
172     iptables-save
173
174     echo "========== $COMMAND: exiting add - end"
175 }
176
177 #Stop the bridge and restore the original setting
178 function stop () {
179     # take extra arg for ifname, if provided
180     [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
181
182     ### Checking
183     type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
184
185     ip address show $INTERFACE_BRIDGE &> /dev/null || {
186         echo "Bridge interface $INTERFACE_BRIDGE does not exist - $COMMAND stop exiting"
187         exit 0
188     }
189     address=$(/sbin/ip address show $INTERFACE_BRIDGE | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
190     brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
191     ip address del $address dev $INTERFACE_BRIDGE
192     brctl delbr $INTERFACE_BRIDGE
193     systemctl NetworkManager restart
194 }
195
196 function main () {
197
198     case "$1" in
199         start)
200             shift; start "$@" ;;
201         stop)
202             shift; stop "$@" ;;
203         add)
204             shift; add "$@" ;;
205         *)
206             echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;
207     esac
208     exit 0
209 }
210
211 # redirect stderr as well
212 main "$@" 2>&1