#!/bin/bash # Thierry Parmentelat # Copyright (C) 2010 INRIA # # 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 # COMMAND=$(basename $0) cd $(dirname $0) # turn on verbosity set -x # constant INTERFACE_BRIDGE=br0 #################### compute INTERFACE_LAN # use /proc/net/dev instead of a hard-wired list function gather_interfaces () { python < /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 ip address show $INTERFACE_BRIDGE &> /dev/null && { echo "Bridge interface $INTERFACE_BRIDGE already set up - $COMMAND start exiting" exit 0 } ip address show $INTERFACE_LAN &> /dev/null || { echo "Cannot use interface $INTERFACE_LAN - exiting" exit 1 } #Getting host IP/masklen address=$(/sbin/ip address show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}') [ -z "$address" ] && { echo "ERROR: Could not determine IP address for $INTERFACE_LAN" ; exit 1 ; } broadcast=$(/sbin/ip address show $INTERFACE_LAN | grep -v inet6 | grep inet | head --lines=1 | awk '{print $4;}') [ -z "$broadcast" ] && echo "WARNING: Could not determine broadcast address for $INTERFACE_LAN" gateway=$(ip route show | grep default | awk '{print $3;}') [ -z "$gateway" ] && echo "WARNING: Could not determine gateway IP" ### do it #Restarting udev #echo "Starting udev ..." #/sbin/udevd restart #if modprobe kqemu &> /dev/null ; then # echo "(bridge-init) kqemu loaded" #else # echo "(bridge-init) WARNING : Could not modprobe kqemu" #fi #Loading 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=$INTERFACE_BRIDGE" brctl addbr $INTERFACE_BRIDGE #brctl stp $INTERFACE_BRIDGE yes brctl addif $INTERFACE_BRIDGE $INTERFACE_LAN echo "Activating promiscuous mode INTERFACE_LAN=$INTERFACE_LAN" ip link set dev $INTERFACE_LAN promisc on sleep 2 echo "Setting bridge address=$address broadcast=$broadcast" # static ip address add $address broadcast $broadcast dev $INTERFACE_BRIDGE # turn on bridge interface ip link set dev $INTERFACE_BRIDGE up ip address del $address dev $INTERFACE_LAN sleep 1 #Reconfigure the routing table echo "Adding default route via gateway=$gateway on dev $INTERFACE_LAN" ip route add 0.0.0.0/0 via $gateway dev $INTERFACE_BRIDGE echo "========== $COMMAND: exiting start - beg" ip address show ip route show echo "========== $COMMAND: exiting start - end" } #Adding a new interface to the bridge: this is used by qemu-ifup function add () { [[ -z "$@" ]] && { echo "Usage: $COMMAND add ifname" ; exit 1 ; } INTERFACE_LAN=$1; shift echo "========== $COMMAND: entering add - beg" ip address show ip route show echo "========== $COMMAND: entering add - end" 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 echo "========== $COMMAND: exiting add - beg" ip address show ip route show echo "Installed iptables" iptables-save echo "========== $COMMAND: exiting add - end" } #Stop the bridge and restore the original setting function 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 ; } ip address show $INTERFACE_BRIDGE &> /dev/null || { echo "Bridge interface $INTERFACE_BRIDGE does not exist - $COMMAND stop exiting" exit 0 } address=$(/sbin/ip address show $INTERFACE_BRIDGE | grep -v inet6 | grep inet | head --lines=1 | awk '{print $2;}') brctl delif $INTERFACE_BRIDGE $INTERFACE_LAN ip address del $address dev $INTERFACE_BRIDGE brctl delbr $INTERFACE_BRIDGE systemctl NetworkManager restart } function main () { 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 } # redirect stderr as well main "$@" 2>&1