xen/network-multi_bridge

338 lines
10 KiB
Plaintext
Raw Normal View History

#!/bin/bash
#============================================================================
# network-multi_bridge
#
# Version = 2.0.1
# Date = 2007-03-09
#
# Maintainer(s) = Ron Terry - ron (at) pronetworkconsulting (dot) com
#
# The latest version can be found at:
#
# http://pronetworkconsulting.com/linux/scripts/network-multi_bridge.html
#
# Description:
#
# Extension to and wrapper for the xen network-bridge script that allows for
# the creation of multiple bridges.
#
# This script can create 3 types of bridges:
#
# traditional bridges: -Bridges that contain both a physical network
# device (ethX) and a virtual network device (vethX)
# from Dom0.
# -This is the traditional type of network bridge
# created in xen by the network-bridge script.
#
# host bridges: -Bridgees that contain only a virtual network
# device (vethX) from Dom0.
# -This is similar to a VMware "Host Only" network.
#
# empty bridges: -Bridges that do not contain any physical or
# virtual network devices from Dom0.
# -These can be used to allow VMs in DomUs to
# communicate only with other DomUs and not Dom0.
#
# This script accepts the (start|stop|status) parameters and passes
# them to the network-bridge script for each physical interface
# specified in the NETDEV_LIST variable. This will allow the user
# to disassemble the traditional bridges containg physical network
# devices so that they may change the network configuration of the
# Physical network interfaces. If not specified it sends the start
# parameter.
#
# Host bridges do not need to be disassembled to change the IP address
# of the virtual interfaces because they do not contain interfaces that
# have been renamed like the traditional briges created by the
# network-bridge script do. The stop parameter does however cause them
# to be disassembled and removed.
#
# The Empty bridges do not contain interfaces from Dom0 so are not
# affected by IP address changes. The stop parameter does cause them
# to be removed as well.
#
# This script will test for the presence of the physical interfaces
# configured to be connected to traditional bridges and only attempt to
# create bridges on the ones that are present and up. It will also test
# for the presence of virtual interfaces configured to be connected to
# host bridges and only create bridges for the ones that exist and
# are not already connected to an existing bridge.
#
# Edit the NETDEV_LIST variable to define which physical interfaces
# you wish to create bridges on. The default is the first 3 network
# interfaces (eth0 eth1 eth2).
#
# Edit the HOST_BRIDGE_LIST variable to define which virtual interfaces
# you wish to create host bridges on. The defaule is the 3rd virtual
# interface (veth3)
#
# Edit the EMPTY_BRIDGE_LIST variable to define which empty bridges to
# create. This list should contain the numbers of the bridges to
# create (4 5 6 7)
#
# To enable this script edit the network-script field in the
# /etc/xen/xen-config.sxp file.
#
# Example: (network-script network-multi_bridge)
#
# Depends on: /etc/xen/scripts/xen-network-common.sh
# /etc/xen/scripts/network-bridge
#
# Can use: /etc/sysconfig/dom0config
#
# Usage: network-multi_bridge (start|stop|status)
#
# Vars:
#
# NETDEV_LIST -Space delimited list of physical network devices to
# create traditional bridges on
#
# HOST_BRIDGE_LIST -Space delimited list of virtual network devices to
# create host bridges on using the following format:
#
# <virtual network device>,<mac address>,<IP address/CIDR NetMask>
#
# Example with 2 virtual devices:
#
# "veth1,00:16:3E:01:00:01,172.16.0.1/16 veth2,00:16:3E:01:00:02,172.17.0.1/16"
#
# EMPTY_BRIDGE_LIST -Space delimited list of bridge numbers to create as
# empty bridges.
#
# BRIDGE_NAME -Name of bridge to create (example: xenbr)
#
# SCRIPT_PATH -Path to the directory conaining the xen network-bridge
# script (typically /etc/xen/scripts)
#
#============================================================================
#### Read config files and set variables ##################################
. /etc/xen/scripts/xen-network-common.sh
# If you source the /etc/sysconfig/dom0config file comment out the variables
# being set in this script.
#. /etc/sysconfig/dom0config
NETDEV_LIST="eth0 eth1 eth2"
HOST_BRIDGE_LIST="veth3,00:16:3E:01:00:03,172.16.0.1/16"
EMPTY_BRIDGE_LIST="4 5 6 7"
BRIDGE_NAME="xenbr"
SCRIPT_PATH="/etc/xen/scripts"
#### Script Fuinctions ####################################################
get_option() {
# Determine which option was passed from the command line.
# If nothing is passed it defaults to start
if [ -z "$1" ]
then
CMD_OPT="start"
else
case "$1" in
start|stop|status)
CMD_OPT="$1"
;;
*)
CMD_OPT="start"
;;
esac
fi
}
setup_host_interface() {
# Configure the MAC and IP address of a virtual device.
#
# This function is called by other fuctions.
#
# usage: setup_host_interface <virtual net device> <MAC Addr> <IP Addr>
local DEV="$1"
local MAC="$2"
local IPADDR="$3"
case $CMD_OPT in
start)
# take the interface down
ip link set $DEV down
# ... and configure it
ip link set $DEV addr $MAC
ip addr flush $DEV
ip addr add $IPADDR brd + dev $DEV
# bring it back up
ip link set $DEV up
;;
stop)
# take the interface down
ip link set $DEV down
# unconfigure it
ip link set $DEV addr fe:ff:ff:ff:ff:ff
ip addr flush $DEV
;;
status)
ip addr show $DEV
;;
esac
}
create_bridges() {
# Uses the network-bridge script to create bridges on physical devices in Dom0.
#
# This fuction passes the start,stop,status parameters on to the network-bridge
# script.
for NETDEVICE in $NETDEV_LIST
do
local BRIDGE_NUM=${NETDEVICE##${NETDEVICE%%[0-9]*}}
if /sbin/ip address show $NETDEVICE | grep -q UP
then
echo ""
echo "============================================================"
echo "Configuring Virtual Bridge: $BRIDGE_NAME$BRIDGE_NUM"
echo "using- Physical Interface: $NETDEVICE"
echo " Virtual Interface: vif$BRIDGE_NUM"
echo "============================================================"
echo ""
$SCRIPT_PATH/network-bridge $CMD_OPT netdev=$NETDEVICE bridge=$BRIDGE_NAME$BRIDGE_NUM vifnum=$BRIDGE_NUM
echo ""
echo "------------------------------------------------------------"
else
echo " Physical Interface $NETDEVICE is not up. Skipping $BRIDGE_NAME$BRIDGE_NUM"
echo "------------------------------------------------------------"
fi
done
}
create_host_bridges() {
# Creates bridges attached to virtual devices in Dom0.
#
# This fuction reads the start,stop,status parameter from the $CMD_OPT variable
# and responds respectively.
for HOSTDEVICE in $HOST_BRIDGE_LIST
do
local DEV=`echo $HOSTDEVICE|cut -d "," -f 1`
local MAC=`echo $HOSTDEVICE|cut -d "," -f 2`
local IPADDR=`echo $HOSTDEVICE|cut -d "," -f 3`
local BRIDGE_NUM=`echo $DEV|cut -c 5`
local VIF=vif0.$BRIDGE_NUM
local BR_NAME=$BRIDGE_NAME$BRIDGE_NUM
case $CMD_OPT in
start)
if ! brctl show | grep -q $DEV && /sbin/ip address show $DEV
then
echo ""
echo "============================================================"
echo "Configuring Virtual Host Bridge: $BR_NAME"
echo " using- Virtual Interface: $VIF"
echo " Virtual Device: $DEV"
echo "============================================================"
create_bridge $BR_NAME
setup_bridge_port $VIF
add_to_bridge $BR_NAME $VIF
setup_host_interface $DEV $MAC $IPADDR
echo "------------------------------------------------------------"
else
echo " Virtual Interface $DEV is already attached to a bridge or it does not exist."
echo " Skipping $BR_NAME"
echo "------------------------------------------------------------"
fi
;;
stop)
echo ""
echo "============================================================"
echo "Unconfiguring Virtual Host Bridge: $BR_NAME"
echo " using- Virtual Interface: $VIF"
echo " Virtual Device: $DEV"
echo "============================================================"
# unconfigure the veth
setup_host_interface $DEV $MAC $IPADDR
# remove vif from the bridge
brctl delif $BR_NAME $VIF
# unconfigure the vif
ip link set $VIF down
ip link set $VIF addr fe:ff:ff:ff:ff:ff
ip link set $VIF multicast on
ip link set $VIF arp on
ip addr flush $VIF
# unconfigure the bridge
ip link set $BR_NAME down
brctl delbr $BR_NAME
echo "------------------------------------------------------------"
;;
status)
echo ""
echo "============================================================"
echo "Status of Virtual Host Bridge: $BR_NAME"
echo " using- Virtual Interface: $VIF"
echo " Virtual Device: $DEV"
echo "============================================================"
brctl show | grep -w "^$BR_NAME"
echo ""
ip addr show $DEV
echo "============================================================"
;;
esac
done
}
create_empty_bridges() {
# Creates bridges attached to no devices in Dom0.
#
# This function reads the start,stop,status parameter from the $CMD_OPT
# variable and respondes respectively.
echo ""
echo "============================================================"
for BRIDGE in $EMPTY_BRIDGE_LIST
do
local BRIDGE_NUM=$BRIDGE
local BR_NAME=$BRIDGE_NAME$BRIDGE_NUM
case $CMD_OPT in
start)
if ! brctl show | grep -qw "^$BR_NAME"
then
echo "Configuring Virtual Empty Bridge: $BR_NAME"
create_bridge $BR_NAME
fi
;;
stop)
if brctl show | grep -qw "^$BR_NAME"
then
echo "Unconfiguring Virtual Empty Bridge: $BR_NAME"
ip link set $BR_NAME down
brctl delbr $BR_NAME
fi
;;
status)
brctl show $BR_NAME | grep -w "^$BR_NAME"
;;
esac
done
echo "============================================================"
}
#### Call Functions #######################################################
get_option "$1"
create_bridges
create_host_bridges
create_empty_bridges