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 # Default Value for 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("virbr")==0: continue
29 if ifname.find("tap")==0: continue
34 # let's try to figure out the interface to use - try these in order
35 function discover_interface () {
36 for ifname in $(gather_interfaces); do
37 ip link show $ifname | grep -qi 'state UP' && { INTERFACE_LAN=$ifname; return; }
39 # still not found ? that's bad
45 # Fonction de mise en place du pont
48 echo "========== $COMMAND: entering start - beg"
53 echo "========== $COMMAND: entering start - end"
55 # disable netfilter calls for bridge interface (they cause panick on 2.6.35 anyway)
57 # another option would be to accept the all forward packages for
58 # bridged interface like: -A FORWARD -m physdev --physdev-is-bridged -j ACCEPT
59 sysctl net.bridge.bridge-nf-call-iptables=0
60 sysctl net.bridge.bridge-nf-call-ip6tables=0
61 sysctl net.bridge.bridge-nf-call-arptables=0
63 # take extra arg for ifname, if provided
64 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
67 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
69 #if we have already configured the same host_box no need to do it again
70 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
71 echo "Bridge interface $INTERFACE_BRIDGE already set up - $COMMAND start exiting"
74 /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
75 echo "Cannot use interface $INTERFACE_LAN - exiting"
79 #Getting host IP/masklen
80 address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
81 [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
83 broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $4;}')
84 [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
86 gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
87 [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
91 echo "Starting udev ..."
93 if modprobe kqemu &> /dev/null ; then
94 echo "(bridge-init) kqemu loaded"
96 echo "(bridge-init) WARNING : Could not modprobe kqemu"
98 #Loading the tun/tap model
99 if modprobe tun ; then
101 # Giving read/write access
102 echo "Granting read/write acces to the tun device"
103 chmod 666 /dev/net/tun
105 echo "Could not modprobe tun - exiting"
109 # creating the bridge
110 echo "Creating bridge INTERFACE_BRIDGE=$INTERFACE_BRIDGE"
111 brctl addbr $INTERFACE_BRIDGE
112 #brctl stp $INTERFACE_BRIDGE yes
113 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
114 echo "Activating promiscuous mode INTERFACE_LAN=$INTERFACE_LAN"
115 /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
117 echo "Setting bridge address=$address broadcast=$broadcast"
119 /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
122 #Reconfigure the routing table
123 echo "Configuring gateway=$gateway"
124 route add default gw $gateway
126 echo "========== $COMMAND: exiting start - beg"
129 echo "========== $COMMAND: exiting start - end"
132 #Adding a new interface to the bridge: this is used by qemu-ifup
135 [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
136 INTERFACE_LAN=$1; shift
138 echo "========== $COMMAND: entering add - beg"
141 echo "========== $COMMAND: entering add - end"
143 echo "Activating link for $INTERFACE_LAN..."
144 /sbin/ip link set $INTERFACE_LAN up
146 echo "Adding $INTERFACE_LAN to $INTERFACE_BRIDGE"
147 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
149 # turn off filtering on this interface
150 ########## from the test environment
151 # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH
153 [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; }
156 echo "Tweaking iptables"
157 iptables-save > iptables.pre
158 # rewrite a new config - quick and dirty
159 ./iptables.py iptables.pre iptables.post $IP
160 iptables-restore < iptables.post
162 echo "========== $COMMAND: exiting add - beg"
167 echo "Installed iptables"
170 echo "========== $COMMAND: exiting add - end"
173 #Stop the bridge and restore the original setting
175 # take extra arg for ifname, if provided
176 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
179 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
181 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null || {
182 echo "Bridge interface $INTERFACE_BRIDGE does not exist - $COMMAND stop exiting"
185 brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
186 /sbin/ifconfig $INTERFACE_BRIDGE down
187 brctl delbr $INTERFACE_BRIDGE
188 /sbin/service network restart
189 /sbin/service iptables restart
202 echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;
207 # redirect stderr as well