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