#!/bin/bash # Establishment of a runtime environment for a # virtual machine under QEMU, This script allows the host box # to share its network connection with qemu-based guests # # Author: Amine chaoui # COMMAND=$(basename $0) cd $(dirname $0) # constant INTERFACE_BRIDGE=br0 # Default Value INTERFACE_LAN=eth0 # Fonction de mise en place du pont start () { # take extra arg for ifname, if provided [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; } ### Checking type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; } #if we have already configured the same host_box no need to do it again /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null && { echo "Interface bridge $INTERFACE_BRIDGE already exist." exit 0 } /sbin/ifconfig $INTERFACE_LAN &>/dev/null || { echo "Cannot use interface $INTERFACE_LAN - exiting" exit 1 } #Getting host IP/masklen address=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | awk '{print $2;}') [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; } broadcast=$(/sbin/ip addr show $INTERFACE_LAN | grep -v inet6 | grep inet | awk '{print $4;}') [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN" gateway=$(netstat -rn | grep '^0.0.0.0' | awk '{print $2;}') [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP" ### do it #Restarting udev echo "Starting udev ..." /sbin/udevd restart if modprobe kqemu ; then echo "kqemu loadded" else echo "WARNING : Could not modprobe kqemu" fi #Loding the tun/tap model if modprobe tun ; then echo "tun loaded" # Giving read/write access echo "Granting read/write acces to the tun device" chmod 666 /dev/net/tun else echo "Could not modprobe tun - exiting" exit 1 fi # creating the bridge echo "Creating bridge $INTERFACE_BRIDGE" brctl addbr $INTERFACE_BRIDGE #brctl stp $INTERFACE_BRIDGE yes brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN echo "Activating promiscuous mode $INTERFACE_LAN..." /sbin/ifconfig $INTERFACE_LAN 0.0.0.0 promisc up sleep 2 echo "Setting bridge $address $broadcast" # static /sbin/ifconfig $INTERFACE_BRIDGE $address broadcast $broadcast up sleep 1 #Reconfigure the routing table echo "Configuring the IP Gateway @:" $gateway route add default gw $gateway } #Adding a new interface to the bridge: this is used by qemu-ifup add () { [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; } INTERFACE_LAN=$1; shift echo "Activating link for $INTERFACE_LAN..." /sbin/ip link set $INTERFACE_LAN up sleep 1 echo "Adding $INTERFACE_LAN to $INTERFACE_BRIDGE" brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN # turn off filtering on this interface ########## from the test environment # expected vars are MACADDR, NODE_ISO, HOSTNAME, IP and TARGET_ARCH CONFIG=qemu.conf [ -f "$CONFIG" ] || { echo "Config file for qemu $CONFIG not found in $(pwd)" ; exit 1 ; } . $CONFIG echo "Tweaking iptables" iptables-save > iptables.pre # rewrite a new config - quick and dirty ./iptables.py iptables.pre iptables.post $IP iptables-restore < iptables.post } #Stop the bridge and restore the original setting stop () { # take extra arg for ifname, if provided [ -n "$1" ] && { INTERFACE_LAN=$1; shift ; } ### Checking type -p brctl &> /dev/null || { echo "brctl not found, please install bridge-utils" ; exit 1 ; } /sbin/ifconfig $INTERFACE_BRIDGE &> /dev/null || { echo "Interface bridge $INTERFACE_BRIDGE does not exist." exit 0 } brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN /sbin/ifconfig $INTERFACE_BRIDGE down brctl delbr $INTERFACE_BRIDGE /sbin/service network restart /sbin/service iptables restart } # case "$1" in start) shift; start "$@" ;; stop) shift; stop "$@" ;; add) shift; add "$@" ;; *) echo $"Usage: env-qemu {start|add|stop} [interface]" ; exit 1 ;; esac exit 0