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 ip address del $address dev $INTERFACE_LAN
124 #Reconfigure the routing table
125 echo "Adding default route via gateway=$gateway on dev $INTERFACE_LAN"
126 ip route add default via $gateway dev $INTERFACE_BRIDGE
128 echo "========== $COMMAND: exiting start - beg"
131 echo "========== $COMMAND: exiting start - end"
134 #Adding a new interface to the bridge: this is used by qemu-ifup
137 [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
138 INTERFACE_LAN=$1; shift
140 echo "========== $COMMAND: entering add - beg"
143 echo "========== $COMMAND: entering add - end"
145 echo "Activating link for $INTERFACE_LAN..."
146 /sbin/ip link set $INTERFACE_LAN up
148 echo "Adding $INTERFACE_LAN to $INTERFACE_BRIDGE"
149 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
151 # turn off filtering on this interface
152 ########## from the test environment
153 # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH
155 [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; }
158 echo "Tweaking iptables"
159 iptables-save > iptables.pre
160 # rewrite a new config - quick and dirty
161 ./iptables.py iptables.pre iptables.post $IP
162 iptables-restore < iptables.post
164 echo "========== $COMMAND: exiting add - beg"
169 echo "Installed iptables"
172 echo "========== $COMMAND: exiting add - end"
175 #Stop the bridge and restore the original setting
177 # take extra arg for ifname, if provided
178 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
181 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
183 ip address show $INTERFACE_BRIDGE &> /dev/null || {
184 echo "Bridge interface $INTERFACE_BRIDGE does not exist - $COMMAND stop exiting"
187 address=$(/sbin/ip address show $INTERFACE_BRIDGE | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
188 brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
189 ip address del $address dev $INTERFACE_BRIDGE
190 brctl delbr $INTERFACE_BRIDGE
191 /sbin/service network restart
192 /sbin/service iptables restart
205 echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;
210 # redirect stderr as well