#!/bin/sh # script called from /etc/qemu-ifup to bridge a tap network # interface to an ethernet interface. # # Usage: qemu_bridge [-dhcp] tap_interface eth_interface # # qemu_bridge [-dhcp] tap_interface # (find eth interface automatically) # # if -dhcp is specified, use dhcp to get bridge ip, otherwise # reuse ethernet interface settings. DHCP="" if [ "$1" = "-dhcp" ]; then shift DHCP=yes fi # tap interface TAP_IF="$1" if ! ifconfig -a | grep -q "^$TAP_IF"; then echo "qemu_bridge: Tap interface '$TAP_IF' doesn't exist, aborting." exit 1 fi # The network interface to attach the network bridge to. IF="$2" if [ -z "$IF" ]; then # use default route's interface if possible IF=`route -n | grep '^0.0.0.0' | xargs echo | cut -d\ -f8` if [ -z "$IF" ]; then # otherwise get first configured interface IF=`ifconfig -s | tail +2 | grep -v '^lo\|^tun\|^tap\|^br0' | head -1 | cut -d\ -f1` fi fi if ! ifconfig -a | grep -q "^$IF"; then echo "qemu_bridge: couldn't find ethernet interface, aborting." exit 1 fi # create bridge if it doesn't exist already if ! ifconfig -a | grep -q '^br0'; then if [ -n "$DHCP" ]; then echo "Creating bridge br0 for interfaces $IF, $TAP_IF (dhcp)" else # If not using DHCP, get eth interface settings IP=`ifconfig $IF | head -2 | tail -1 | sed -e 's/.*addr:\([0-9.]*\).*/\1/'` BROADCAST=`ifconfig $IF | head -2 | tail -1 | sed -e 's/.*cast:\([0-9.]*\).*/\1/'` NETMASK=`ifconfig $IF | head -2 | tail -1 | sed -e 's/.*Mask:\([0-9.]*\).*/\1/'` GATEWAY=`route -n | grep $IF | sed -e 's/[0-9.]* *\([0-9.]*\).*/\1/' | grep -v '0\.0\.0\.0' | head -1` echo "Creating bridge br0 for interfaces $IF, $TAP_IF (ip $IP, bcast $BROADCAST, gw $GATEWAY)" fi # Warning: if the host is a diskless setup (knoppix terminal server), # bringing down the interface will hang the pc ... ifconfig $IF down brctl addbr br0 ifconfig $IF 0.0.0.0 promisc up echo "Configuring bridge..." brctl stp br0 off brctl setfd br0 1 brctl sethello br0 1 brctl addif br0 $IF if [ -n "$DHCP" ]; then echo "Bringing up br0 (dhcp)..." [ -x /sbin/pump ] && PUMP="/sbin/pump -i" [ -x /sbin/dhclient ] && PUMP="/sbin/dhclient" $PUMP br0 else echo "Bringing up br0 ($IP)..." ifconfig br0 $IP netmask $NETMASK broadcast $BROADCAST up if [ -n "$GATEWAY" ]; then route add default gw $GATEWAY fi fi fi # add tap interface to bridge ifconfig $TAP_IF 0.0.0.0 promisc up brctl addif br0 $TAP_IF