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 # let's try to figure out the interface to use - try these in order
22 IFNAMES="eth0 eth1 eth2 eth3"
23 function discover_interface () {
24 for ifname in $IFNAMES; do
25 ip link show $ifname | grep -q UP && { INTERFACE_LAN=$ifname; return; }
27 # still not found ? that's bad
33 # Fonction de mise en place du pont
36 echo "========== $COMMAND: entering start - beg"
41 echo "========== $COMMAND: entering start - end"
43 # disable netfilter calls for bridge interface (they cause panick on 2.6.35 anyway)
45 # another option would be to accept the all forward packages for
46 # bridged interface like: -A FORWARD -m physdev --physdev-is-bridged -j ACCEPT
47 sysctl net.bridge.bridge-nf-call-iptables=0
48 sysctl net.bridge.bridge-nf-call-ip6tables=0
49 sysctl net.bridge.bridge-nf-call-arptables=0
51 # take extra arg for ifname, if provided
52 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
55 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
57 #if we have already configured the same host_box no need to do it again
58 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
59 echo "Bridge interface $INTERFACE_BRIDGE already set up - $COMMAND start exiting"
62 /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
63 echo "Cannot use interface $INTERFACE_LAN - exiting"
67 #Getting host IP/masklen
68 address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
69 [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
71 broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $4;}')
72 [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
74 gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
75 [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
79 echo "Starting udev ..."
81 if modprobe kqemu &> /dev/null ; then
82 echo "(bridge-init) kqemu loaded"
84 echo "(bridge-init) WARNING : Could not modprobe kqemu"
86 #Loading the tun/tap model
87 if modprobe tun ; then
89 # Giving read/write access
90 echo "Granting read/write acces to the tun device"
91 chmod 666 /dev/net/tun
93 echo "Could not modprobe tun - exiting"
98 echo "Creating bridge INTERFACE_BRIDGE=$INTERFACE_BRIDGE"
99 brctl addbr $INTERFACE_BRIDGE
100 #brctl stp $INTERFACE_BRIDGE yes
101 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
102 echo "Activating promiscuous mode INTERFACE_LAN=$INTERFACE_LAN"
103 /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
105 echo "Setting bridge address=$address broadcast=$broadcast"
107 /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
110 #Reconfigure the routing table
111 echo "Configuring gateway=$gateway"
112 route add default gw $gateway
114 echo "========== $COMMAND: exiting start - beg"
117 echo "========== $COMMAND: exiting start - end"
120 #Adding a new interface to the bridge: this is used by qemu-ifup
123 [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
124 INTERFACE_LAN=$1; shift
126 echo "========== $COMMAND: entering add - beg"
129 echo "========== $COMMAND: entering add - end"
131 echo "Activating link for $INTERFACE_LAN..."
132 /sbin/ip link set $INTERFACE_LAN up
134 echo "Adding $INTERFACE_LAN to $INTERFACE_BRIDGE"
135 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
137 # turn off filtering on this interface
138 ########## from the test environment
139 # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH
141 [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; }
144 echo "Tweaking iptables"
145 iptables-save > iptables.pre
146 # rewrite a new config - quick and dirty
147 ./iptables.py iptables.pre iptables.post $IP
148 iptables-restore < iptables.post
150 echo "========== $COMMAND: exiting add - beg"
155 echo "Installed iptables"
158 echo "========== $COMMAND: exiting add - end"
161 #Stop the bridge and restore the original setting
163 # take extra arg for ifname, if provided
164 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
167 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
169 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null || {
170 echo "Bridge interface $INTERFACE_BRIDGE does not exist - $COMMAND stop exiting"
173 brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
174 /sbin/ifconfig $INTERFACE_BRIDGE down
175 brctl delbr $INTERFACE_BRIDGE
176 /sbin/service network restart
177 /sbin/service iptables restart
190 echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;
195 # redirect stderr as well