xen/network-multinet

390 lines
11 KiB
Bash

#!/bin/sh
#============================================================================
# network-multinet
#
# Version = 3.0.0
# Date = 2008-01-30
#
# Maintainer(s) = Ron Terry - ron (at) pronetworkconsulting (dot) com
#
# The latest version can be found at:
#
# http://pronetworkconsulting.com/linux/scripts/network-multinet.html
#
# Description:
#
# Replacement for the Xen network-bridge, network-nat and network-route
# scripts. This script allows for the creation of multiple networks.
#
# This script can create 6 types of networks:
#
# bridged: -Networks that are connected to a physical network device
# in Dom0 and on which Dom0 can communitcate
# -This is the traditional type of network created in xen by
# the basic network-bridge script.
# -VMs on these network(s) appear to be on the real network(s)
#
# nohost: -Networks that are connected to Dom0 but on which Dom0 cannot
# communitcate
# -These can be used to allow virtual machines to communicate
# with the outside world but not with Dom0.
# (Usefull if you want to isolate traffic away from Dom0)
#
# hostonly: -Networks that are connected to Dom0 but are private from
# the physical network
# -This type of network will allow VMs connected to it to
# access only Dom0 and other VMs connected to the network.
# -This type of network is similiar to a VMware "HOST ONLY"
# network.
#
# nat: -Networks that are connected to Dom0 and are privet from the
# physical network but VMs can get out to the physical network
# -This type of network will allow VMs connected to it to access
# Dom0,the "outside world" via NAT and other VMs connected to it.
# -This type of network is similiar to a VMware "NAT" network.
#
# routed: -Networks that are not directly connected to the physical network
# but whi's traffic is directly routed to other networks
# -This type of network will allow VMs connected to it to access
# Dom0,the "outside world" via routing through Dom0 and other VMs
# connected to it.
#
# empty: -Networks that are not connected to either Dom0 or the physical
# network
# -These can be used to allow VMs in DomUs to communicate only
# with other DomUs and not Dom0.
#
#
# This script accepts the (start|stop|restart|status) parameters.
#
# This script requires that the vif-bridge script be used as the vif
# creation script (as opposed to vif-nat/vif-route).
#
# This script will test for the presence of the physical interfaces
# configured to be connected to bridged networks and only attempt to
# create networks on the ones that are present and up.
#
# Edit the NETWORK_LIST variable to define which networks to create on which
# interfaces. The default is to create a bridged network on the first
# interface active network interface.
#
# To enable this script edit the network-script field in the
# /etc/xen/xend-config.sxp file.
#
# Example: (network-script network-multinet)
#
# Depends on: $SCRIPT_PATH/multinet-common.sh
#
# Config file: /etc/sysconfig/xend
#
# Usage: network-multinet (start|stop|restart|status)
#
# Vars:
#
# --------------------------- In this script ----------------------------
#
# SCRIPT_PATH -Path to the directory containing the xen network
# configuration scripts (typically /etc/xen/scripts)
#
# ------------------------- In the config file --------------------------
# NETWORK_LIST -Space delimited list of network devices to create networks
# on using the following format:
#
# <network type>,<number of network type>,<network device>,<mac address>,<IP address/CIDR NetMask>,<dhcp server status>
#
# Example with 3 virtual devices:
#
# "bridge,0,default,default,dhcp-off nat,0,none,172.23.0.1/16,dhcp-off hostonly,0,none,172.23.0.1/16,dhcp-off"
#
# NAT_EXTERNAL_INTERFACE -Network interface to use as the external interface
# for NATed and Routed networks
#
#============================================================================
#### Read config files and set variables ##################################
# Source the configuration File
. /etc/sysconfig/xend
SCRIPT_PATH="/etc/xen/scripts"
#### Script Functions #####################################################
usage() {
# Gives help about usage parameters
echo "Usage: $0 {start|stop|restart|status}"
exit 1
}
get_option() {
# Determine which option was passed from the command line.
case "$1" in
start|stop|restart|status)
CMD_OPT="$1"
;;
*)
usage
;;
esac
}
. $SCRIPT_PATH/multinet-common.sh
make_config_dirs() {
# Create temporary storage directory if needed.
if ! [ -d "$NETWORK_SAVE_PATH" ]
then
mkdir $NETWORK_SAVE_PATH
fi
}
#***** Network Creation Main Function *************************************
create_networks() {
for NETWORK in $NETWORK_LIST
do
local NET_TYPE=`echo $NETWORK | cut -d "," -f 1`
local NET_NUMBER=`echo $NETWORK | cut -d "," -f 2`
local NET_DEV=`echo $NETWORK | cut -d "," -f 3`
local NET_DEV_IP=`echo $NETWORK | cut -d "," -f 4`
local NET_DHCP_SRV=`echo $NETWORK | cut -d "," -f 5`
case $NET_DHCP_SRV in
dhcp-on)
DHCP_SRV="on"
;;
*)
DHCP_SRV="off"
;;
esac
# Find the name of the network interface for the first bridged network
#---------------------------------------------------------------------
case $NET_DEV in
default)
local NET_DEV=`ip route list | awk '/^default / { print $NF }'`
;;
esac
case $NET_TYPE in
bridge)
# Create the network
#---------------------------------------------------------------------
configure_bridged_networks $CMD_OPT $NET_DEV $NET_NUMBER
;;
nat|route|hostonly)
# Create the network
#---------------------------------------------------------------------
configure_local_networks $CMD_OPT $NET_DEV $NET_TYPE $NET_NUMBER $NET_DEV_IP $NET_DHCP_SRV
;;
nohost)
# Create the network
#---------------------------------------------------------------------
configure_nohost_networks $CMD_OPT $NET_DEV $NET_NUMBER
;;
empty)
# Create the network
#---------------------------------------------------------------------
configure_empty_networks $CMD_OPT $NET_NUMBER
;;
esac
done
}
#***** Pre/Post Start/Stop Functions **************************************
run_prestart_scripts() {
echo ""
echo "============================================================"
echo "Running pre-start scripts"
echo
test -d $PLUGIN_DIR/pre-start || mkdir -p $PLUGIN_DIR/pre-start
if ls $PLUGIN_DIR/pre-start/*.sh > /dev/null 2>&1
then
for SCRIPT in `ls $PLUGIN_DIR/pre-start/*.sh`
do
echo ""
echo " Running $SCRIPT"
echo
$SCRIPT prestart
echo
echo "------------------------------------------------------------"
done
else
echo " No pre-start scripts to run. Continuing ..."
echo
fi
echo "============================================================"
}
run_poststart_scripts() {
echo ""
echo "============================================================"
echo "Running post-start scripts"
echo
test -d $PLUGIN_DIR/post-start || mkdir -p $PLUGIN_DIR/post-start
if ls $PLUGIN_DIR/post-start/*.sh > /dev/null 2>&1
then
for SCRIPT in `ls $PLUGIN_DIR/post-start/*.sh`
do
echo ""
echo " Running $SCRIPT"
echo
$SCRIPT poststart
echo
echo "------------------------------------------------------------"
done
else
echo " No post-start scripts to run. Continuing ..."
echo
fi
echo "============================================================"
}
run_prestop_scripts() {
echo ""
echo "============================================================"
echo "Running pre-stop scripts"
echo
test -d $PLUGIN_DIR/pre-stop || mkdir -p $PLUGIN_DIR/pre-stop
if ls $PLUGIN_DIR/pre-stop/*.sh > /dev/null 2>&1
then
for SCRIPT in `ls $PLUGIN_DIR/pre-stop/*.sh`
do
echo ""
echo " Running $SCRIPT"
echo
$SCRIPT prestop
echo
echo "------------------------------------------------------------"
done
else
echo " No pre-stop scripts to run. Continuing ..."
echo
fi
echo "============================================================"
}
run_poststop_scripts() {
echo ""
echo "============================================================"
echo "Running post-stop scripts"
echo
test -d $PLUGIN_DIR/post-stop || mkdir -p $PLUGIN_DIR/post-stop
if ls $PLUGIN_DIR/post-stop/*.sh > /dev/null 2>&1
then
for SCRIPT in `ls $PLUGIN_DIR/post-stop/*.sh`
do
echo ""
echo " Running $SCRIPT"
echo
$SCRIPT poststop
echo
echo "------------------------------------------------------------"
done
else
echo " No post-stop scripts to run. Continuing ..."
echo
fi
echo "============================================================"
}
#### Start, Stop, Status Functions ########################################
start_xend_network() {
echo ""
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo " Starting the xend network environment"
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
# Determine if we are using SuSEfirewall2
use_sf2 start
# Run pre-start scripts
run_prestart_scripts
manage_firewall prestart
# Create the predefined networks
create_networks
# Run post-start scripts
manage_firewall poststart
run_poststart_scripts
}
stop_xend_network() {
echo ""
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
echo " Stopping the xend network environment"
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
# Run pre-stop scripts
run_prestop_scripts
manage_firewall prestop
# Remove the networks
remove_all_networks
# Run post-stop scripts
manage_firewall poststop
run_poststop_scripts
# Clean-up if we are using the SuSEfirewall2
use_sf2 stop
}
show_xend_network_status() {
create_networks
}
#### Main Code Body #######################################################
get_option "$1"
make_config_dirs
touch $NETWORKTAB
case $CMD_OPT in
start)
# Start the Xen network
start_xend_network
# Start the DHCP server if it exists
#do_dhcpd start
;;
stop)
# Stop the DHCP server if it exists
#do_dhcpd stop
# Stop the Xen network
stop_xend_network
;;
restart)
# Stop the DHCP server if it exists
#do_dhcpd stop
# Stop the Xen network
CMD_OPT="stop"
stop_xend_network
# Start the Xen network
CMD_OPT="start"
start_xend_network
# Start the DHCP server if it exists
#do_dhcpd start
;;
status)
show_xend_network_status
#do_dhcpd status
;;
esac
exit 0