3 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
4 # Copyright (C) 2010 INRIA
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
11 COMMAND=$(basename $0)
20 #################### compute INTERFACE_LAN
21 # use /proc/net/dev instead of a hard-wired list
22 function gather_interfaces () {
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
35 function discover_interface () {
36 for ifname in $(gather_interfaces); do
37 ip link show $ifname | grep -qi 'state UP' && { echo $ifname; return; }
39 # still not found ? that's bad
42 INTERFACE_LAN=$(discover_interface)
43 echo Using physical interface $INTERFACE_LAN
46 # Fonction de mise en place du pont
49 echo "========== $COMMAND: entering start - beg"
54 echo "========== $COMMAND: entering start - end"
56 # disable netfilter calls for bridge interface (they cause panick on 2.6.35 anyway)
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
64 # take extra arg for ifname, if provided
65 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
68 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
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"
75 ip address show $INTERFACE_LAN &> /dev/null || {
76 echo "Cannot use interface $INTERFACE_LAN - exiting"
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 ; }
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"
87 gateway=$(ip route show | grep default | awk '{print $3;}')
88 [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
92 #echo "Starting udev ..."
94 #if modprobe kqemu &> /dev/null ; then
95 # echo "(bridge-init) kqemu loaded"
97 # echo "(bridge-init) WARNING : Could not modprobe kqemu"
99 #Loading the tun/tap model
100 if modprobe tun ; then
102 # Giving read/write access
103 echo "Granting read/write acces to the tun device"
104 chmod 666 /dev/net/tun
106 echo "Could not modprobe tun - exiting"
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
118 echo "Setting bridge address=$address broadcast=$broadcast"
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
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
130 echo "========== $COMMAND: exiting start - beg"
133 echo "========== $COMMAND: exiting start - end"
136 #Adding a new interface to the bridge: this is used by qemu-ifup
139 [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
140 INTERFACE_LAN=$1; shift
142 echo "========== $COMMAND: entering add - beg"
145 echo "========== $COMMAND: entering add - end"
147 echo "Activating link for $INTERFACE_LAN..."
148 /sbin/ip link set $INTERFACE_LAN up
150 echo "Adding $INTERFACE_LAN to $INTERFACE_BRIDGE"
151 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
153 # turn off filtering on this interface
154 ########## from the test environment
155 # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH
157 [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; }
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
166 echo "========== $COMMAND: exiting add - beg"
171 echo "Installed iptables"
174 echo "========== $COMMAND: exiting add - end"
177 #Stop the bridge and restore the original setting
179 # take extra arg for ifname, if provided
180 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
183 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
185 ip address show $INTERFACE_BRIDGE &> /dev/null || {
186 echo "Bridge interface $INTERFACE_BRIDGE does not exist - $COMMAND stop exiting"
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
206 echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;
211 # redirect stderr as well