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 # take extra arg for ifname, if provided
25 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
28 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
30 #if we have already configured the same host_box no need to do it again
31 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
32 echo "Interface bridge $INTERFACE_BRIDGE already exist."
35 /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
36 echo "Cannot use interface $INTERFACE_LAN - exiting"
40 #Getting host IP/masklen
41 address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}')
42 [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
44 broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $4;}')
45 [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
47 gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
48 [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
52 echo "Starting udev ..."
54 if modprobe kqemu &> /dev/null ; then
57 echo "WARNING : Could not modprobe kqemu"
59 #Loading the tun/tap model
60 if modprobe tun ; then
62 # Giving read/write access
63 echo "Granting read/write acces to the tun device"
64 chmod 666 /dev/net/tun
66 echo "Could not modprobe tun - exiting"
71 echo "Creating bridge INTERFACE_BRIDGE=$INTERFACE_BRIDGE"
72 brctl addbr $INTERFACE_BRIDGE
73 #brctl stp $INTERFACE_BRIDGE yes
74 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
75 echo "Activating promiscuous mode INTERFACE_LAN=$INTERFACE_LAN"
76 /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
78 echo "Setting bridge address=$address broadcast=$broadcast"
80 /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
83 #Reconfigure the routing table
84 echo "Configuring gateway=$gateway"
85 route add default gw $gateway
89 #Adding a new interface to the bridge: this is used by qemu-ifup
92 [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
93 INTERFACE_LAN=$1; shift
95 echo "Activating link for $INTERFACE_LAN..."
96 /sbin/ip link set $INTERFACE_LAN up
98 echo "Adding $INTERFACE_LAN to $INTERFACE_BRIDGE"
99 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
101 # turn off filtering on this interface
102 ########## from the test environment
103 # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH
105 [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; }
108 echo "Tweaking iptables"
109 iptables-save > iptables.pre
110 # rewrite a new config - quick and dirty
111 ./iptables.py iptables.pre iptables.post $IP
112 iptables-restore < iptables.post
114 echo "Installed iptables"
119 #Stop the bridge and restore the original setting
121 # take extra arg for ifname, if provided
122 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
125 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
127 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null || {
128 echo "Interface bridge $INTERFACE_BRIDGE does not exist."
131 brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
132 /sbin/ifconfig $INTERFACE_BRIDGE down
133 brctl delbr $INTERFACE_BRIDGE
134 /sbin/service network restart
135 /sbin/service iptables restart
148 echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;
153 # redirect stderr as well