#!/bin/bash #============================================================================ # xen-dhcpd # # Version = 1.0.2 # Date = 2007-09-14 # # Maintainer(s) = Ron Terry - ron (at) pronetworkconsulting (dot) com # # Contributers = Mike Friesenegger - mfriesenegger (at) novell (dot) com # # The latest version can be found at: # # http://pronetworkconsulting.com/linux/scripts/network-multinet.html # # Description: # # This script configures and enables the DHCP server for the networks on # which it has been defined to run in the Xen network condifuration script # config file. # #============================================================================ #### Read config files and set variables ################################## # Source the configuration File . /etc/sysconfig/xend DHCPD_CONF_FILE="/etc/dhcpd.conf" CONF_FILE_PATH="/etc/xen/conf" SYSCONFIG_FILE="/etc/sysconfig/dhcpd" #### Script Functions ##################################################### do_we_start_dhcpd() { if ! echo $LOCAL_BRIDGE_LIST|grep -q "dhcp-on" || ! echo $NETWORK_LIST|grep -q "dhcp-on" then echo " The DHCP daemon is not configured on any network" echo "" echo " Not starting dhcpd for Xen" echo "============================================================" exit 1 fi } cleanup_old_dhcp_config_files() { # A bit of housekeeping, cleaning up old configuration files #echo "cleaning up /etc/xen/conf/" #read rm -rf /etc/xen/conf/* #echo "cleaning up /etc/dhcpd-xen*" #read rm -rf /etc/dhcpd-xen* #echo "cleaning up /etc/sysconfig/network/ifcfg-veth-id*" #read rm -rf /etc/sysconfig/network/ifcfg-veth-id* } create_xen_dhcp_config_files() { # backup original dhcpd sysconfig file #echo "backup original dhcpd sysconfig file" #read cp "$SYSCONFIG_FILE" "$CONF_FILE_PATH"/dhcpd.xensave cp "$DHCPD_CONF_FILE" "$CONF_FILE_PATH/dhcpd.conf.xensave" for IFACE in $LOCAL_BRIDGE_LIST do # If DHCPON is set to dhcp-off, skip creating config files for vethX #--------------------------------------------------------------------- local DHCPON=`echo $IFACE|cut -d "," -f 5` test "$DHCPON" = "dhcp-off" && continue # Set local function variables #--------------------------------------------------------------------- local DEV=`echo $IFACE|cut -d "," -f 1` local MAC=`echo $IFACE|cut -d "," -f 2` local IPCIDR=`echo $IFACE|cut -d "," -f 3` local IPADDR=`echo $IPCIDR|cut -d "/" -f 1` local RANGE="`echo $IPADDR|cut -d "." -f 1,2,3`.`echo $XEN_DHCP_RANGE|cut -d "-" -f 1` - `echo $IPADDR|cut -d "." -f 1,2,3`.`echo $XEN_DHCP_RANGE|cut -d "-" -f 2`" local SUBNET=`ipcalc -n -b $IPCIDR|grep "Network:"|cut -d ":" -f 2|cut -d "/" -f 1` local NETMASK=`ipcalc -n -b $IPCIDR|grep "Netmask:"|cut -d ":" -f 2|cut -d "=" -f 1` local BRIDGE_NUM=${DEV##${DEV%%[0-9]*}} local BR_NAME=$BRIDGE_NAME$BRIDGE_NUM case $XEN_DHCP_DNS_SERVERS in gateway) # Use Dom0 as the DNS server local DNS=$IPADDR ;; *) # Specify DNS server(s) if test `echo $XEN_DHCP_DNS_SERVERS|grep -c ","` then local DNS=`echo $XEN_DHCP_DNS_SERVERS|sed "s/,/, /"` else local DNS=`echo $XEN_DHCP_DNS_SERVERS` fi ;; esac # echo out what we are doing #--------------------------------------------------------------------- echo "------------------------------------------------------------" echo " Configuring dhcpd for bridge: $BR_NAME" echo " on Interface: $DEV" echo " -------------------" echo " Subnet: $SUBNET" echo " Netmask: $NETMASK" echo " Range: $RANGE" echo " DNS Servers: $DNS" echo " Gateway: $IPADDR" echo "------------------------------------------------------------" # Create network ifcfg-veth config file # THIS IS NOT NECESSARY UNLESS YOU WANT TO SEE THE NIC IN YAST TOOLS #--------------------------------------------------------------------- echo "NAME='XEN Virtual Ethernet - $DEV'" > /etc/sysconfig/network/ifcfg-veth-id-$MAC # Create the dhcpd-xen.vethX.conf file #--------------------------------------------------------------------- echo "ddns-update-style none;" > /etc/dhcpd-xen.$DEV.conf echo "subnet $SUBNET netmask $NETMASK {" >> /etc/dhcpd-xen.$DEV.conf echo " range `echo $RANGE | tr -d -`;" >> /etc/dhcpd-xen.$DEV.conf echo " default-lease-time 14400;" >> /etc/dhcpd-xen.$DEV.conf echo " max-lease-time 14400;" >> /etc/dhcpd-xen.$DEV.conf echo " option domain-name-servers $DNS;" >> /etc/dhcpd-xen.$DEV.conf echo " option routers $IPADDR;" >> /etc/dhcpd-xen.$DEV.conf echo "}" >> /etc/dhcpd-xen.$DEV.conf # edit the dhcpd sysconfig file for xen #--------------------------------------------------------------------- #echo "editing DHCPD_INTERFACE in $SYSCONFIG_FILE" #read sed -i "s/^DHCPD_INTERFACE=\"\([^\"]*\)\"/DHCPD_INTERFACE=\"\1 veth-id-$MAC\"/" $SYSCONFIG_FILE #echo "editing DHCPD_CONF_INCLUDE_FILES in $SYSCONFIG_FILE" #read sed -i "s/^DHCPD_CONF_INCLUDE_FILES=\"\([^\"]*\)\"/DHCPD_CONF_INCLUDE_FILES=\"\1\/etc\/dhcpd-xen.$DEV.conf\"/" $SYSCONFIG_FILE # edit the dhcpd.conf file to include additional dhcpd configs for xen #--------------------------------------------------------------------- echo "include \"/etc/dhcpd-xen.$DEV.conf\";" >> $DHCPD_CONF_FILE done } restore_original_dhcp_config_files() { # Restore the original dhcpd.conf file #--------------------------------------------------------------------- #echo "restoring original dhcpd.conf" #read mv $CONF_FILE_PATH/dhcpd.xensave $SYSCONFIG_FILE > /dev/null 2>&1 mv $CONF_FILE_PATH/dhcpd.conf.xensave $DHCPD_CONF_FILE > /dev/null 2>&1 # FIXME: should I be doing this in the for loop below using sed? for IFACE in $LOCAL_BRIDGE_LIST do # If DHCPON is set to dhcp-off, skip creating config files for vethX #--------------------------------------------------------------------- local DHCPON=`echo $IFACE|cut -d "," -f 5` test "$DHCPON" = "dhcp-off" && continue # Set local function variables #--------------------------------------------------------------------- local DEV=`echo $IFACE|cut -d "," -f 1` local MAC=`echo $IFACE|cut -d "," -f 2` local IPCIDR=`echo $IFACE|cut -d "," -f 3` local IPADDR=`echo $IPCIDR|cut -d "/" -f 1` local RANGE="`echo $IPADDR|cut -d "." -f 1,2,3`.`echo $XEN_DHCP_RANGE|cut -d "-" -f 1` `echo $IPADDR|cut -d "." -f 1,2,3`.`echo $XEN_DHCP_RANGE|cut -d "-" -f 2`" local SUBNET=`ipcalc -n -b $IPCIDR|grep "Network:"|cut -d ":" -f 2|cut -d "/" -f 1` local NETMASK=`ipcalc -n -b $IPCIDR|grep "Netmask:"|cut -d ":" -f 2|cut -d "=" -f 1` # echo out what we are doing #--------------------------------------------------------------------- echo " Removing dhcpd configuration for bridge: $BR_NAME" echo "------------------------------------------------------------" # Delete network ifcfg-veth config file #--------------------------------------------------------------------- #echo "deleteing ifcfg-veth-$MAC" #read rm -rf /etc/sysconfig/network/ifcfg-veth-id-$MAC # Delete the dhcpd-xen.vethX.conf file #--------------------------------------------------------------------- #echo "deleteing dhcpd-xen.$DEV.conf" #read rm -rf /etc/dhcpd-xen.$DEV.conf # Restore the original dhcpd.conf file #--------------------------------------------------------------------- # FIXME: should I be doing this with sed here instead? done } #### Start, Stop, Status Functions ######################################## start_xen_dhcpd() { do_we_start_dhcpd #cleanup_old_dhcp_config_files create_xen_dhcp_config_files echo "" echo " Starting the DHCP Daemon on configured networks" /etc/init.d/dhcpd restart } stop_xen_dhcpd() { #/etc/init.d/dhcpd stop restore_original_dhcp_config_files if grep -Rl "Short-Description:.*DHCP Server" /etc/init.d/rc`runlevel|cut -d " " -f 2`.d|grep -q "S" then /etc/init.d/dhcpd restart fi } xen_dhcpd_status () { /etc/init.d/dhcpd status } #### Main Code Body ####################################################### # Check to see if dhcpd and ipcalc are installed if [ -e /usr/sbin/dhcpd ] && [ -e /etc/init.d/dhcpd ] && [ -e $SYSCONFIG_FILE ] && [ -e /usr/bin/ipcalc ] then echo "" echo "============================================================" case $1 in start) start_xen_dhcpd exit 0 ;; stop) stop_xen_dhcpd exit 0 ;; restart) stop_xen_dhcpd start_xen_dhcpd exit 0 ;; status) /etc/init.d/dhcpd status exit 0 ;; esac echo "============================================================" else echo "" echo "============================================================" if [ ! -e /usr/sbin/dhcpd ] && [ ! -e /etc/init.d/dhcpd ] && [ ! -e $SYSCONFIG_FILE ] then echo "" echo " The DHCP Daemon is not installed or is missing needed files." fi if [ ! -e /usr/bin/ipcalc ] then echo "" echo " The ipcalc package is not installed." fi echo "" echo " Skipping starting dhcpd for Xen" echo "============================================================" exit 1 fi