3 # Establishment of a runtime environment for a
4 # virtual machine under QEMU, This script allows the host box
5 # to share its network connection with qemu-based guests
10 COMMAND=$(basename $0)
21 # Fonction de mise en place du pont
24 echo "========== $COMMAND: entering start - beg"
29 echo "========== $COMMAND: entering start - end"
31 # take extra arg for ifname, if provided
32 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
35 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
37 #if we have already configured the same host_box no need to do it again
38 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
39 echo "Bridge interface $INTERFACE_BRIDGE already set up - $COMMAND start exiting"
42 /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
43 echo "Cannot use interface $INTERFACE_LAN - exiting"
47 #Getting host IP/masklen
48 address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
49 [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
51 broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $4;}')
52 [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
54 gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
55 [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
59 echo "Starting udev ..."
61 if modprobe kqemu &> /dev/null ; then
62 echo "(bridge-init) kqemu loaded"
64 echo "(bridge-init) WARNING : Could not modprobe kqemu"
66 #Loading the tun/tap model
67 if modprobe tun ; then
69 # Giving read/write access
70 echo "Granting read/write acces to the tun device"
71 chmod 666 /dev/net/tun
73 echo "Could not modprobe tun - exiting"
78 echo "Creating bridge INTERFACE_BRIDGE=$INTERFACE_BRIDGE"
79 brctl addbr $INTERFACE_BRIDGE
80 #brctl stp $INTERFACE_BRIDGE yes
81 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
82 echo "Activating promiscuous mode INTERFACE_LAN=$INTERFACE_LAN"
83 /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
85 echo "Setting bridge address=$address broadcast=$broadcast"
87 /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
90 #Reconfigure the routing table
91 echo "Configuring gateway=$gateway"
92 route add default gw $gateway
94 echo "========== $COMMAND: exiting start - beg"
97 echo "========== $COMMAND: exiting start - end"
100 #Adding a new interface to the bridge: this is used by qemu-ifup
103 [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
104 INTERFACE_LAN=$1; shift
106 echo "========== $COMMAND: entering add - beg"
109 echo "========== $COMMAND: entering add - end"
111 echo "Activating link for $INTERFACE_LAN..."
112 /sbin/ip link set $INTERFACE_LAN up
114 echo "Adding $INTERFACE_LAN to $INTERFACE_BRIDGE"
115 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
117 # turn off filtering on this interface
118 ########## from the test environment
119 # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH
121 [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; }
124 echo "Tweaking iptables"
125 iptables-save > iptables.pre
126 # rewrite a new config - quick and dirty
127 ./iptables.py iptables.pre iptables.post $IP
128 iptables-restore < iptables.post
130 echo "========== $COMMAND: exiting add - beg"
135 echo "Installed iptables"
138 echo "========== $COMMAND: exiting add - end"
141 #Stop the bridge and restore the original setting
143 # take extra arg for ifname, if provided
144 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
147 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
149 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null || {
150 echo "Bridge interface $INTERFACE_BRIDGE does not exist - $COMMAND stop exiting"
153 brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
154 /sbin/ifconfig $INTERFACE_BRIDGE down
155 brctl delbr $INTERFACE_BRIDGE
156 /sbin/service network restart
157 /sbin/service iptables restart
170 echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;
175 # redirect stderr as well