diff options
author | Martin Schwenke <martin@meltin.net> | 2012-03-07 16:18:12 +1100 |
---|---|---|
committer | Martin Schwenke <martin@meltin.net> | 2012-03-22 15:30:27 +1100 |
commit | 476cf45049360ca212d02efc6a0627110ec42319 (patch) | |
tree | b4664b9b3ecb39fcd0a77c2b7a6a3dad54360813 | |
parent | 0b2c3d7d246964a7b162562c40c1d918bcc63eb9 (diff) | |
download | samba-476cf45049360ca212d02efc6a0627110ec42319.tar.gz samba-476cf45049360ca212d02efc6a0627110ec42319.tar.xz samba-476cf45049360ca212d02efc6a0627110ec42319.zip |
Eventscript functions - no longer require interface_modify.sh
Make add_ip_to_iface() and delete_ip_from_iface() do their own locking
so the external script is no longer required.
Signed-off-by: Martin Schwenke <martin@meltin.net>
(This used to be ctdb commit 93f90caf91246074d9359bf31a39b26212cccc42)
-rwxr-xr-x | ctdb/config/functions | 106 |
1 files changed, 73 insertions, 33 deletions
diff --git a/ctdb/config/functions b/ctdb/config/functions index d97bb5ca6b..ca89c38379 100755 --- a/ctdb/config/functions +++ b/ctdb/config/functions @@ -779,48 +779,88 @@ startstop_nfslock() { add_ip_to_iface() { - local _iface=$1 - local _ip=$2 - local _maskbits=$3 - local _state_dir="$CTDB_VARDIR/state/interface_modify" - local _lockfile="$_state_dir/$_iface.flock" - local _readd_base="$_state_dir/$_iface.readd.d" - - mkdir -p $_state_dir || { - ret=$? - echo "Failed to mkdir -p $_state_dir - $ret" - return $ret - } + _iface=$1 + _ip=$2 + _maskbits=$3 - test -f $_lockfile || { - touch $_lockfile - } + _lockfile="${CTDB_VARDIR}/state/interface_modify_${_iface}.flock" + [ -f "$_lockfile" ] || touch "$_lockfile" + + ( + # Note: use of return/exit/die() below only gets us out of the + # sub-shell, which is actually what we want. That is, the + # function should just return non-zero. + + flock --timeout 30 0 || \ + die "add_ip_to_iface: unable to get lock for ${_iface}" + + # Ensure interface is up + ip link set "$_iface" up || \ + die "Failed to bringup interface $_iface" - flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh add "$_iface" "$_ip" "$_maskbits" "$_readd_base" - return $? + ip addr add "$_ip/$_maskbits" brd + dev "$_iface" || \ + die "Failed to add $_ip/$_maskbits on dev $_iface" + ) <"$_lockfile" + + # Do nothing here - return above only gets us out of the subshell + # and doing anything here will affect the return code. } delete_ip_from_iface() { - local _iface=$1 - local _ip=$2 - local _maskbits=$3 - local _state_dir="$CTDB_VARDIR/state/interface_modify" - local _lockfile="$_state_dir/$_iface.flock" - local _readd_base="$_state_dir/$_iface.readd.d" - - mkdir -p $_state_dir || { - ret=$? - echo "Failed to mkdir -p $_state_dir - $ret" - return $ret - } + _iface=$1 + _ip=$2 + _maskbits=$3 + + _lockfile="${CTDB_VARDIR}/state/interface_modify_${_iface}.flock" + [ -f "$_lockfile" ] || touch "$_lockfile" + + ( + # Note: use of return/exit/die() below only gets us out of the + # sub-shell, which is actually what we want. That is, the + # function should just return non-zero. - test -f $_lockfile || { - touch $_lockfile + flock --timeout 30 0 || \ + die "delete_ip_from_iface: unable to get lock for ${_iface}" + + _im="$_ip/$_maskbits" # shorthand for readability + + # "ip addr del" will delete all secondary IPs if this is the + # primary. To work around this _very_ annoying behaviour we + # have to keep a record of the secondaries and re-add them + # afterwards. Yuck! + + _secondaries="" + if ip addr list dev "$_iface" primary | grep -Fq "inet $_im " ; then + _secondaries=$(ip addr list dev "$_iface" secondary | \ + awk '$1 == "inet" { print $2 }') + fi + + local _rc=0 + ip addr del "$_im" dev "$_iface" || { + echo "Failed to del $_ip on dev $_iface" + _rc=1 } - flock --timeout 30 $_lockfile $CTDB_BASE/interface_modify.sh delete "$_iface" "$_ip" "$_maskbits" "$_readd_base" - return $? + if [ -n "$_secondaries" ] ; then + for _i in $_secondaries; do + if ip addr list dev "$_iface" | grep -Fq "inet $_i" ; then + echo "Kept secondary $_i on dev $_iface" + else + echo "Re-adding secondary address $_i to dev $_iface" + ip addr add $_i brd + dev $_iface || { + echo "Failed to re-add address $_i to dev $_iface" + _rc=1 + } + fi + done + fi + + return $_rc + ) <"$_lockfile" + + # Do nothing here - return above only gets us out of the subshell + # and doing anything here will affect the return code. } ######################################################## |