#!/bin/bash USERID="-u $UID" GROUP="-g $UID" BRIDGE=br0 if [ "x$1" = "x" ]; then echo "Usage: ./qemu-bridge.sh {start/stop}" exit 1 fi if [ $EUID -ne 0 ]; then echo "Error: This script ($0) must be run with root privileges" exit 1 fi TUNCTL=/usr/sbin/tunctl if [ ! -x "$TUNCTL" ]; then echo "Error: Unable to find tunctl binary in '/usr/bin'" exit 1 fi IFCONFIG=`which ifconfig 2> /dev/null` if [ "x$IFCONFIG" = "x" ]; then # better than nothing... IFCONFIG=/sbin/ifconfig fi if [ ! -x "$IFCONFIG" ]; then echo "$IFCONFIG cannot be executed" exit 1 fi BRCTL=`which brctl 2> /dev/null` if [ "x$BRCTL" = "x" ]; then # better than nothing... BRCTL=/sbin/brctl fi if [ ! -x "$BRCTL" ]; then echo "$BRCTL cannot be executed" exit 1 fi DHCLIENT=`which dhclient 2> /dev/null` if [ "x$DHCLIENT" = "x" ]; then # better than nothing... BRCTL=/sbin/dhclient fi if [ ! -x "$DHCLIENT" ]; then echo "$DHCLIENT cannot be executed" exit 1 fi ETH=`$IFCONFIG | grep eth | cut -d' ' -f1` if [ "$1" = "start" ]; then echo "Creating bridge configurations..." TAP=`$TUNCTL -b $GROUP 2>&1` STATUS=$? if [ $STATUS -ne 0 ]; then # If tunctl -g fails, try using tunctl -u, for older host kernels # which do not support the TUNSETGROUP ioctl TAP=`$TUNCTL -b $USERID 2>&1` STATUS=$? if [ $STATUS -ne 0 ]; then echo "tunctl failed:" exit 1 fi fi #Create Linux bridge $BRCTL addbr $BRIDGE STATUS=$? if [ $STATUS -ne 0 ]; then echo "brctl addbr failed:" exit 1 fi #Add interfaces to bridge $BRCTL addif $BRIDGE $ETH $BRCTL addif $BRIDGE $TAP $IFCONFIG $BRIDGE up $IFCONFIG $TAP up #Remove IP from ethX and assign it to the bridge $IFCONFIG $ETH 0.0.0.0 promisc $DHCLIENT $BRIDGE elif [ "$1" = "stop" ]; then TAP=`$IFCONFIG | grep tap | cut -d' ' -f1` echo "Deleting bridge configurations $ETH $TAP..." #Put interface down $IFCONFIG $BRIDGE down $IFCONFIG $ETH down $IFCONFIG $TAP down #Delete the brige $BRCTL delbr $BRIDGE $TUNCTL -d $TAP $IFCONFIG $ETH up $DHCLIENT $ETH fi