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)
17 # Fonction de mise en place du pont
20 # take extra arg for ifname, if provided
21 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
24 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
26 #if we have already configured the same host_box no need to do it again
27 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && {
28 echo "Interface bridge $INTERFACE_BRIDGE already exist."
31 /sbin/ifconfig $INTERFACE_LAN &>/dev/null || {
32 echo "Cannot use interface $INTERFACE_LAN - exiting"
36 #Getting host IP/masklen
37 address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | awk '{print $2;}')
38 [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; }
40 broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | awk '{print $4;}')
41 [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN"
43 gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}')
44 [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP"
48 echo "Starting udev ..."
50 if modprobe kqemu ; then
53 echo "WARNING : Could not modprobe kqemu"
55 #Loding the tun/tap model
56 if modprobe tun ; then
58 # Giving read/write access
59 echo "Granting read/write acces to the tun device"
60 chmod 666 /dev/net/tun
62 echo "Could not modprobe tun - exiting"
67 echo "Creating bridge $INTERFACE_BRIDGE"
68 brctl addbr $INTERFACE_BRIDGE
69 #brctl stp $INTERFACE_BRIDGE yes
70 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
71 echo "Activating promiscuous mode $INTERFACE_LAN..."
72 /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
74 echo "Setting bridge $address $broadcast"
76 /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up
79 #Reconfigure the routing table
80 echo "Configuring the IP Gateway @:" $gateway
81 route add default gw $gateway
84 # wipe the host firewall otherwise the guest qemu can't access the LAN
85 echo "Wiping the firewall..."
90 #Adding a new interface to the bridge: this is used by qemu-ifup
93 [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; }
94 INTERFACE_LAN=$1; shift
96 echo "Activating link for $INTERFACE_LAN..."
97 /sbin/ip link set $INTERFACE_LAN up
99 echo "Adding $INTERFACE_BRIDGE to $INTERFACE_BRIDGE"
100 brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN
101 # putting a virtual interface in promisc mode seems like an odd thing to do
102 # echo "Activating promiscuous mode on $INTERFACE_LAN ..."
103 # /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up
109 #Stop the bridge and restore the original setting
111 # take extra arg for ifname, if provided
112 [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; }
115 type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; }
117 /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null || {
118 echo "Interface bridge $INTERFACE_BRIDGE does not exist."
121 brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN
122 /sbin/ifconfig $INTERFACE_BRIDGE down
123 brctl delbr $INTERFACE_BRIDGE
124 /sbin/service network restart
136 echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;;