create a function "remote_ip" which can be used from scripts to remove a single ip...
authorRonnie Sahlberg <ronniesahlberg@gmail.com>
Wed, 8 Apr 2009 02:49:28 +0000 (12:49 +1000)
committerRonnie Sahlberg <ronniesahlberg@gmail.com>
Wed, 8 Apr 2009 02:49:28 +0000 (12:49 +1000)
use this fucntion from the natgw eventscript

config/events.d/11.natgw
config/functions

index 2d256ba81c173974767554d2c637f56073406945..1e0543d0484c1bf5b85e9f46e5b64c75410bdc34 100644 (file)
@@ -14,16 +14,17 @@ cmd="$1"
 shift
 PATH=/usr/bin:/bin:/usr/sbin:/sbin:$PATH
 
+
 delete_all() {
-       ip addr add $NATGW_PUBLIC_IP dev $NATGW_PUBLIC_IFACE >/dev/null 2>/dev/null
-       ip addr del $NATGW_PUBLIC_IP_HOST dev $NATGW_PRIVATE_IFACE >/dev/null 2>/dev/null
+       remove_ip $NATGW_PUBLIC_IP $NATGW_PUBLIC_IFACE
+       remove_ip $NATGW_PUBLIC_IP_HOST $NATGW_PRIVATE_IFACE
+
        ip route del 0.0.0.0/0 metric 10 >/dev/null 2>/dev/null
 
        # Delete the masquerading setup from a previous iteration where we
        # were the NAT-GW
        iptables -D POSTROUTING -t nat -s $NATGW_PRIVATE_NETWORK -d ! $NATGW_PRIVATE_NETWORK -j MASQUERADE >/dev/null 2>/dev/null
 
-       ip addr del $NATGW_PUBLIC_IP dev $NATGW_PUBLIC_IFACE >/dev/null 2>/dev/null
 }
 
 case $cmd in 
index 2f0b01fe242883489882892c134ee247b4ba15a7..8b29fe4430aafabd433bd47e3a781c8b27ec769d 100644 (file)
@@ -437,6 +437,30 @@ startstop_nfslock() {
        esac
 }
 
+########################################################
+# remove an ip address from an interface
+########################################################
+remove_ip() {
+       # the ip tool 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 $2 primary | grep -q "inet $1 " ; then
+           secondaries=`ip addr list dev $2 secondary | grep " inet " | awk '{print $2}'`
+       fi
+       ip addr del $1 dev $2 >/dev/null 2>/dev/null || failed=1
+       [ -z "$secondaries" ] || {
+           for i in $secondaries; do
+               if ip addr list dev $2 | grep -q "inet $i" ; then
+                   echo "kept secondary $i on dev $2"
+               else 
+                   echo "re-adding secondary address $i to dev $2"
+                   ip addr add $i dev $2 || failed=1           
+               fi
+           done
+       }
+}
+
 ########################################################
 # load a site local config file
 ########################################################
@@ -445,3 +469,4 @@ startstop_nfslock() {
        . $CTDB_BASE/rc.local
 }
 
+