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
10 # Author: Amine chaoui
13 COMMAND=$(basename $0)
24 # Fonction de mise en place du pont
27 echo "========== $COMMAND: entering start - beg"
32 echo "========== $COMMAND: entering start - end"
34 # -A FORWARD -m physdev --physdev-is-bridged -j ACCEPT
36 # take extra arg for ifname, if provided
37 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
40 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
42 #if we have already configured the same host_box no need to do it again
43 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
44 echo "Bridge interface $INTERFACE_BRIDGE already set up - $COMMAND start exiting"
47 /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
48 echo "Cannot use interface $INTERFACE_LAN - exiting"
52 #Getting host IP/masklen
53 address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
54 [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
56 broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $4;}')
57 [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
59 gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
60 [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
64 echo "Starting udev ..."
66 if modprobe kqemu &> /dev/null ; then
67 echo "(bridge-init) kqemu loaded"
69 echo "(bridge-init) WARNING : Could not modprobe kqemu"
71 #Loading the tun/tap model
72 if modprobe tun ; then
74 # Giving read/write access
75 echo "Granting read/write acces to the tun device"
76 chmod 666 /dev/net/tun
78 echo "Could not modprobe tun - exiting"
83 echo "Creating bridge INTERFACE_BRIDGE=$INTERFACE_BRIDGE"
84 brctl addbr $INTERFACE_BRIDGE
85 #brctl stp $INTERFACE_BRIDGE yes
86 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
87 echo "Activating promiscuous mode INTERFACE_LAN=$INTERFACE_LAN"
88 /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
90 echo "Setting bridge address=$address broadcast=$broadcast"
92 /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
95 #Reconfigure the routing table
96 echo "Configuring gateway=$gateway"
97 route add default gw $gateway
99 echo "========== $COMMAND: exiting start - beg"
102 echo "========== $COMMAND: exiting start - end"
105 #Adding a new interface to the bridge: this is used by qemu-ifup
108 [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
109 INTERFACE_LAN=$1; shift
111 echo "========== $COMMAND: entering add - beg"
114 echo "========== $COMMAND: entering add - end"
116 echo "Activating link for $INTERFACE_LAN..."
117 /sbin/ip link set $INTERFACE_LAN up
119 echo "Adding $INTERFACE_LAN to $INTERFACE_BRIDGE"
120 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
122 # turn off filtering on this interface
123 ########## from the test environment
124 # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH
126 [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; }
129 echo "Tweaking iptables"
130 iptables-save > iptables.pre
131 # rewrite a new config - quick and dirty
132 ./iptables.py iptables.pre iptables.post $IP
133 iptables-restore < iptables.post
135 echo "========== $COMMAND: exiting add - beg"
140 echo "Installed iptables"
143 echo "========== $COMMAND: exiting add - end"
146 #Stop the bridge and restore the original setting
148 # take extra arg for ifname, if provided
149 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
152 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
154 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null || {
155 echo "Bridge interface $INTERFACE_BRIDGE does not exist - $COMMAND stop exiting"
158 brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
159 /sbin/ifconfig $INTERFACE_BRIDGE down
160 brctl delbr $INTERFACE_BRIDGE
161 /sbin/service network restart
162 /sbin/service iptables restart
175 echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;
180 # redirect stderr as well