3 #################################
4 # interface event script for ctdb
5 # this adds/removes IPs from your
14 [ -z "$CTDB_PUBLIC_ADDRESSES" ] && {
15 CTDB_PUBLIC_ADDRESSES=$CTDB_BASE/public_addresses
18 [ ! -f "$CTDB_PUBLIC_ADDRESSES" ] && {
19 echo "No public addresses file found. Nothing to do for 10.interfaces"
24 #############################
25 # called when ctdbd starts up
27 # make sure that we only respond to ARP messages from the NIC where
28 # a particular ip address is associated.
29 [ -f /proc/sys/net/ipv4/conf/all/arp_filter ] && {
30 echo 1 > /proc/sys/net/ipv4/conf/all/arp_filter
32 cat "$CTDB_PUBLIC_ADDRESSES" | cut -d/ -f1 | while read _IP; do
33 _IP_HELD=`/sbin/ip addr show | grep "inet $_IP/"`
34 [ -z "$_IP_HELD" ] || {
35 _IFACE=`echo $_IP_HELD | sed -e "s/.*\s//"`
36 _NM=`echo $_IP_HELD | sed -e "s/.*$_IP\///" -e "s/\s.*//"`
37 echo "Removing public address $_IP/$_NM from device $_IFACE"
38 /sbin/ip addr del $_IP/$_NM dev $_IFACE
44 ################################################
45 # called when ctdbd wants to claim an IP address
48 echo "must supply interface, IP and maskbits"
55 # we make sure the interface is up first
56 /sbin/ip link set $iface up || {
57 echo "Failed to bringup interface $iface"
60 /sbin/ip addr add $ip/$maskbits dev $iface || {
61 echo "Failed to add $ip/$maskbits on dev $iface"
63 # cope with the script being killed while we have the interface blocked
64 iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
66 # flush our route cache
67 echo 1 > /proc/sys/net/ipv4/route/flush
71 ##################################################
72 # called when ctdbd wants to release an IP address
75 echo "must supply interface, IP and maskbits"
79 # releasing an IP is a bit more complex than it seems. Once the IP
80 # is released, any open tcp connections to that IP on this host will end
81 # up being stuck. Some of them (such as NFS connections) will be unkillable
82 # so we need to use the killtcp ctdb function to kill them off. We also
83 # need to make sure that no new connections get established while we are
84 # doing this! So what we do is this:
85 # 1) firewall this IP, so no new external packets arrive for it
86 # 2) use netstat -tn to find existing connections, and kill them
87 # 3) remove the IP from the interface
88 # 4) remove the firewall rule
94 # we do an extra delete to cope with the script being killed
95 iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
96 iptables -I INPUT -i $iface -d $ip -j DROP
97 kill_tcp_connections $ip
99 # the ip tool will delete all secondary IPs if this is the primary. To work around
100 # this _very_ annoying behaviour we have to keep a record of the secondaries and re-add
101 # them afterwards. yuck
103 if /sbin/ip addr list dev $iface primary | grep "inet $ip/$maskbits " > /dev/null; then
104 secondaries=`/sbin/ip addr list dev $iface secondary | grep " inet " | awk '{print $2}'`
106 /sbin/ip addr del $ip/$maskbits dev $iface || failed=1
107 [ -z "$secondaries" ] || {
108 for i in $secondaries; do
109 if /sbin/ip addr list dev $iface | grep "inet $i" > /dev/null; then
110 echo "kept secondary $i on dev $iface"
112 echo "re-adding secondary address $i to dev $iface"
113 /sbin/ip addr add $i dev $iface || failed=1
117 iptables -D INPUT -i $iface -d $ip -j DROP
119 echo "Failed to del $ip on dev $iface"
123 # flush our route cache
124 echo 1 > /proc/sys/net/ipv4/route/flush
128 ###########################################
129 # called when ctdbd has finished a recovery
133 ####################################
134 # called when ctdbd is shutting down
139 INTERFACES=`cat $CTDB_PUBLIC_ADDRESSES |
140 sed -e "s/^[^\t ]*[\t ]*//" -e "s/[\t ]*$//"`
142 [ "$CTDB_PUBLIC_INTERFACE" ] && INTERFACES="$CTDB_PUBLIC_INTERFACE $INTERFACES"
144 INTERFACES=`for IFACE in $INTERFACES ; do echo $IFACE ; done | sort | uniq`
146 for IFACE in $INTERFACES ; do
149 IFACE=`echo $IFACE |sed -e 's/\....$//'`
150 grep '^MII Status: up' /proc/net/bonding/$IFACE > /dev/null || {
151 echo "ERROR: public network interface $IFACE is down"
156 # we dont know how to test ib links
160 /usr/sbin/ethtool $IFACE | grep 'Link detected: yes' > /dev/null || {
161 echo "ERROR: No link on the public network interface $IFACE"