#!/bin/ksh -x # # /etc/qemu-bridge: Script to take a TAP passed in as # a parameter and bridge it to an # interface. # # Parameters: $1 - The TAP device # [$2] - Network interface to bridge TAP to # otherwise assume the first interface # # # Requires the bridge package from # http://www.whiteboard.ne.jp/~admin2/tuntap/#bridge # # Configuration Section: # # PRIMARY_INTERFACE should be set if you are using this directly from # qemu, and you don't want to let the automatic detection code define # the interface. It will be overridden if you pass in a defined # interface using the sol_qemu_bridge wrapper. # # BRDGADM_EXE is the location of the brdgadm command. by default, # it installs in /usr/local/bin. It only moves, if you move it # as the configure script doesn't accept --prefix= # PRIMARY_INTERFACE= BRDGADM_EXE=/usr/local/bin/brdgadm # ################################################################ # # Security. # # use RBAC to create a limited role which allows you to create the bridge # # If you can't do RBAC, you have to use sudo # # sudoer's entry: ALL={ALL} NOPASSWD: /usr/local/bin/brdgadm # SUDO_EXE=/opt/csw/bin/sudo # BRDGADM="${SUDO_EXE} ${BRDGADM_EXE}" # ################################################################# # # $1 = tap0, tap1, ... if [ -z "$1" ]; then echo "Usage: $0 interface [bridge interface]" exit 1 else TAP_INTERFACE="$1" fi # # we're setting the interface we want to bridge the tap to. # do some basic sanity checking. If they botch the interface, # just allocate one that exists. # if [ -n "$2" ]; then PRIMARY_INTERFACE="`/usr/sbin/ifconfig -a | /usr/bin/egrep -v '^[ ]|^lo0:' | /usr/bin/cut -f1 -d: | /usr/bin/grep $2 2> /dev/null | /usr/bin/awk '{ print $1 }'`" fi # # Figure out which interface to test for from existing bridges # IE, we have to delete any bridge that contains either the # defined TAP or the defined PRIMARY_INTERFACE # if [ -n "${PRIMARY_INTERFACE}" ]; then # Define the a variable to check existing interface is bridged TEST_INTERFACE="${PRIMARY_INTERFACE}" else # Define the a variable to check existing tap is bridged TEST_INTERFACE="${TAP_INTERFACE}" fi # # Get the list of bridged interfaces # BRIDGED=`${BRDGADM} -l 2> /dev/null | /usr/bin/egrep -v "^List of the interfaces|^--------" 2> /dev/null` if [ -n "${BRIDGED}" ]; then ORIG_INT="`/usr/bin/echo ${BRIDGED} | /usr/bin/awk '{ print $1 }'`" # # if the defined interface is part of a bridge group, or # the new tap is already part of a bridge group, # delete the bridge before we create it, otherwise # the creation of the bridge will fail. # if [ "${ORIG_INT}" = "${TEST_INTERFACE}" ]; then for i in ${BRIDGED} do ${BRDGADM} -d $i if [ $? -ne 0 ]; then echo "deletion of interface $i from the bridge failed, rc=$?" exit 1 fi done fi fi # # If an interface to bridge to is undefined, try and make a good guess # at the first interface that isn't lo0. # if [ -z "${PRIMARY_INTERFACE}" ]; then PRIMARY_INTERFACE="`/usr/sbin/ifconfig -a | /usr/bin/egrep -v '^[ ]|^lo0:' | /usr/bin/cut -f1 -d:| /usr/bin/awk '{ print $1 }'`" fi # # Create the new bridge # ${BRDGADM} -a ${PRIMARY_INTERFACE} ${BRDGADM} -a ${TAP_INTERFACE}