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)
18 # Fonction de mise en place du pont
21 # take extra arg for ifname, if provided
22 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
25 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
27 #if we have already configured the same host_box no need to do it again
28 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
29 echo "Interface bridge $INTERFACE_BRIDGE already exist."
32 /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
33 echo "Cannot use interface $INTERFACE_LAN - exiting"
37 #Getting host IP/masklen
38 address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | awk '{print $2;}')
39 [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
41 broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | awk '{print $4;}')
42 [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
44 gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
45 [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
49 echo "Starting udev ..."
51 if modprobe kqemu ; then
54 echo "WARNING : Could not modprobe kqemu"
56 #Loding the tun/tap model
57 if modprobe tun ; then
59 # Giving read/write access
60 echo "Granting read/write acces to the tun device"
61 chmod 666 /dev/net/tun
63 echo "Could not modprobe tun - exiting"
68 echo "Creating bridge $INTERFACE_BRIDGE"
69 brctl addbr $INTERFACE_BRIDGE
70 #brctl stp $INTERFACE_BRIDGE yes
71 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
72 echo "Activating promiscuous mode $INTERFACE_LAN..."
73 /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
75 echo "Setting bridge $address $broadcast"
77 /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
80 #Reconfigure the routing table
81 echo "Configuring the IP Gateway @:" $gateway
82 route add default gw $gateway
86 #Adding a new interface to the bridge: this is used by qemu-ifup
89 [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
90 INTERFACE_LAN=$1; shift
92 echo "Activating link for $INTERFACE_LAN..."
93 /sbin/ip link set $INTERFACE_LAN up
95 echo "Adding $INTERFACE_LAN to $INTERFACE_BRIDGE"
96 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
98 # turn off filtering on this interface
99 ########## from the test environment
100 # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH
102 [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; }
105 echo "Tweaking iptables"
106 iptables-save > iptables.pre
107 # rewrite a new config - quick and dirty
108 ./iptables.py iptables.pre iptables.post $IP
109 iptables-restore < iptables.post
113 #Stop the bridge and restore the original setting
115 # take extra arg for ifname, if provided
116 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
119 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
121 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null || {
122 echo "Interface bridge $INTERFACE_BRIDGE does not exist."
125 brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
126 /sbin/ifconfig $INTERFACE_BRIDGE down
127 brctl delbr $INTERFACE_BRIDGE
128 /sbin/service network restart
129 /sbin/service iptables restart
141 echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;