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"
64 # cope with the script being killed while we have the interface blocked
65 /sbin/iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
67 # flush our route cache
68 echo 1 > /proc/sys/net/ipv4/route/flush
72 ##################################################
73 # called when ctdbd wants to release an IP address
76 echo "must supply interface, IP and maskbits"
80 # releasing an IP is a bit more complex than it seems. Once the IP
81 # is released, any open tcp connections to that IP on this host will end
82 # up being stuck. Some of them (such as NFS connections) will be unkillable
83 # so we need to use the killtcp ctdb function to kill them off. We also
84 # need to make sure that no new connections get established while we are
85 # doing this! So what we do is this:
86 # 1) firewall this IP, so no new external packets arrive for it
87 # 2) use netstat -tn to find existing connections, and kill them
88 # 3) remove the IP from the interface
89 # 4) remove the firewall rule
95 # we do an extra delete to cope with the script being killed
96 /sbin/iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
97 /sbin/iptables -I INPUT -i $iface -d $ip -j DROP
98 kill_tcp_connections $ip
100 # the ip tool will delete all secondary IPs if this is the primary. To work around
101 # this _very_ annoying behaviour we have to keep a record of the secondaries and re-add
102 # them afterwards. yuck
104 if /sbin/ip addr list dev $iface primary | grep "inet $ip/$maskbits " > /dev/null; then
105 secondaries=`/sbin/ip addr list dev $iface secondary | grep " inet " | awk '{print $2}'`
107 /sbin/ip addr del $ip/$maskbits dev $iface || failed=1
108 [ -z "$secondaries" ] || {
109 for i in $secondaries; do
110 if /sbin/ip addr list dev $iface | grep "inet $i" > /dev/null; then
111 echo "kept secondary $i on dev $iface"
113 echo "re-adding secondary address $i to dev $iface"
114 /sbin/ip addr add $i dev $iface || failed=1
118 /sbin/iptables -D INPUT -i $iface -d $ip -j DROP
120 echo "Failed to del $ip on dev $iface"
124 # flush our route cache
125 echo 1 > /proc/sys/net/ipv4/route/flush
129 ###########################################
130 # called when ctdbd has finished a recovery
134 ####################################
135 # called when ctdbd is shutting down
140 INTERFACES=`cat $CTDB_PUBLIC_ADDRESSES |
141 sed -e "s/^[^\t ]*[\t ]*//" -e "s/[\t ]*$//"`
143 [ "$CTDB_PUBLIC_INTERFACE" ] && INTERFACES="$CTDB_PUBLIC_INTERFACE $INTERFACES"
145 INTERFACES=`for IFACE in $INTERFACES ; do echo $IFACE ; done | sort | uniq`
148 for IFACE in $INTERFACES ; do
149 if [ `echo $IFACE | grep '^bond*'` ] ; then
150 grep '^MII Status: up' /proc/net/bonding/$IFACE > /dev/null || {
151 echo "ERROR: public network interface $IFACE is down"
156 /usr/sbin/ethtool $IFACE | grep 'Link detected: yes' > /dev/null || {
157 echo "ERROR: No link on the public network interface $IFACE"