#!/bin/bash # vserver helper functions to deal with DHCP _dhcp_set_file_variables(){ local IPDEV=$1 local VSERVER=$2 local PREFIX=$3 DHCP_INFO_FILE="/var/run/vservers/$PREFIX-$VSERVER.dhcp" DHCP_PID_FILE="/var/run/dhclient-$IPDEV:$PREFIX-$VSERVER.pid" DHCP_LEASE_FILE="/var/lib/dhcp/dhclient-$IPDEV:$PREFIX-$VSERVER.leases" DHCP_CONFIG_FILE="/var/lib/dhcp/dhclient-$IPDEV:$PREFIX-$VSERVER.conf" DHCP_SCRIPT_FILE="$USR_LIB_VSERVER/dhclient-script" } _dhcp_kill_dhclient(){ local IPDEV=$1 local VSERVER=$2 local PREFIX=$3 local SIGNAL=$4 _dhcp_set_file_variables $IPDEV $VSERVER $PREFIX # Use -s to make sure the pid file contains a pid... if [ -s "$DHCP_PID_FILE" ]; then kill $SIGNAL `cat $DHCP_PID_FILE` >/dev/null 2>&1 return $? fi return 1 } _dhcp_create_info_file(){ local IPDEV=$1 local VSERVER=$2 local PREFIX=$3 _dhcp_set_file_variables $IPDEV $VSERVER $PREFIX echo "DHCP_IP=\"$new_ip_address\"" > $DHCP_INFO_FILE echo "DHCP_MASK=\"$new_subnet_mask\"" >> $DHCP_INFO_FILE echo "DHCP_BCAST=\"$new_broadcast_address\"" >> $DHCP_INFO_FILE echo "DHCP_DNS=\"$new_domain_name_servers\"" >> $DHCP_INFO_FILE } dhcp_acquire_ip(){ local IPDEV=$1 local VSERVER=$2 local S_HOSTNAME=$3 local PREFIX=$4 if ! _dhcp_kill_dhclient $IPDEV $VSERVER "$PREFIX" "-0" ; then # dhclient is not running for this vserver MASTER=$(hostname) IDENT="vserver $PREFIX-$VSERVER@$MASTER" if [ ${#IDENT} = "32" ] ; then # bug with some dhcpd servers when client-identifier is 32 bytes long IDENT="$IDENT+" fi echo "send dhcp-client-identifier \"$IDENT\";" > $DHCP_CONFIG_FILE if [ "$PREFIX" = "0" ] ; then echo "send host-name \"$S_HOSTNAME\";" >> $DHCP_CONFIG_FILE fi rm -f $DHCP_INFO_FILE /sbin/dhclient -1 -q -cf $DHCP_CONFIG_FILE -lf $DHCP_LEASE_FILE \ -pf $DHCP_PID_FILE -sf $DHCP_SCRIPT_FILE $IPDEV \ -e VSERVER=$VSERVER -e PREFIX=$PREFIX if [ "$?" = "0" ] ; then # Sometimes the pid file is created a small amount of time after the dhclient command returns, and # we must use -s to make sure the pid has been commited to it. while [ ! -s "$DHCP_PID_FILE" ] ; do sleep 1 done else echo "Error running dhclient: $?" >&2 exit 1 fi fi if [ -f $DHCP_INFO_FILE ] ; then source $DHCP_INFO_FILE else echo "Can't find DHCP information file '$DHCP_INFO_FILE'." >&2 echo "Lease could not be acquired or has expired/changed." >&2 exit 1 fi } dhcp_relinquish_ip(){ local IPDEV=$1 local VSERVER=$2 local PREFIX=$3 _dhcp_kill_dhclient $IPDEV $VSERVER $PREFIX rm -f $DHCP_INFO_FILE $DHCP_PID_FILE }