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 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
72 echo "Bridge interface $INTERFACE_BRIDGE already set up - $COMMAND start exiting"
75 /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
76 echo "Cannot use interface $INTERFACE_LAN - exiting"
80 #Getting host IP/masklen
81 address=$(/sbin/ip addr 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 addr 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=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
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 /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
118 echo "Setting bridge address=$address broadcast=$broadcast"
120 /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
123 #Reconfigure the routing table
124 echo "Configuring gateway=$gateway"
125 route add default gw $gateway
127 echo "========== $COMMAND: exiting start - beg"
130 echo "========== $COMMAND: exiting start - end"
133 #Adding a new interface to the bridge: this is used by qemu-ifup
136 [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
137 INTERFACE_LAN=$1; shift
139 echo "========== $COMMAND: entering add - beg"
142 echo "========== $COMMAND: entering add - end"
144 echo "Activating link for $INTERFACE_LAN..."
145 /sbin/ip link set $INTERFACE_LAN up
147 echo "Adding $INTERFACE_LAN to $INTERFACE_BRIDGE"
148 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
150 # turn off filtering on this interface
151 ########## from the test environment
152 # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH
154 [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; }
157 echo "Tweaking iptables"
158 iptables-save > iptables.pre
159 # rewrite a new config - quick and dirty
160 ./iptables.py iptables.pre iptables.post $IP
161 iptables-restore < iptables.post
163 echo "========== $COMMAND: exiting add - beg"
168 echo "Installed iptables"
171 echo "========== $COMMAND: exiting add - end"
174 #Stop the bridge and restore the original setting
176 # take extra arg for ifname, if provided
177 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
180 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
182 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null || {
183 echo "Bridge interface $INTERFACE_BRIDGE does not exist - $COMMAND stop exiting"
186 brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
187 /sbin/ifconfig $INTERFACE_BRIDGE down
188 brctl delbr $INTERFACE_BRIDGE
189 /sbin/service network restart
190 /sbin/service iptables restart
203 echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;
208 # redirect stderr as well