merge from tridge
[ctdb.git] / config / events.d / 10.interface
1 #!/bin/sh
2
3 #################################
4 # interface event script for ctdb
5 # this adds/removes IPs from your 
6 # public interface
7
8 . $CTDB_BASE/functions
9 loadconfig ctdb
10
11 cmd="$1"
12 shift
13
14 [ -z "$CTDB_PUBLIC_ADDRESSES" ] && {
15         CTDB_PUBLIC_ADDRESSES=$CTDB_BASE/public_addresses
16 }
17
18 [ ! -f "$CTDB_PUBLIC_ADDRESSES" ] && {
19         echo "No public addresses file found. Nothing to do for 10.interfaces"
20         exit 0
21 }
22
23 case $cmd in 
24      #############################
25      # called when ctdbd starts up
26      startup)
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
31         }
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
39                 }
40         done
41         ;;
42
43
44      ################################################
45      # called when ctdbd wants to claim an IP address
46      takeip)
47         if [ $# != 3 ]; then
48            echo "must supply interface, IP and maskbits"
49            exit 1
50         fi
51         iface=$1
52         ip=$2
53         maskbits=$3
54
55         # we make sure the interface is up first
56         /sbin/ip link set $iface up || {
57                  echo "Failed to bringup interface $iface"
58                  exit 1
59         }
60         /sbin/ip addr add $ip/$maskbits dev $iface || {
61                  echo "Failed to add $ip/$maskbits on dev $iface"
62                  exit 1
63         }
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
66
67         # flush our route cache
68         echo 1 > /proc/sys/net/ipv4/route/flush
69         ;;
70
71
72      ##################################################
73      # called when ctdbd wants to release an IP address
74      releaseip)
75         if [ $# != 3 ]; then
76            echo "must supply interface, IP and maskbits"
77            exit 1
78         fi
79
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
90         iface=$1
91         ip=$2
92         maskbits=$3
93
94         failed=0
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
99
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
103         secondaries=""
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}'`
106         fi
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"
112                 else 
113                     echo "re-adding secondary address $i to dev $iface"
114                     /sbin/ip addr add $i dev $iface || failed=1         
115                 fi
116             done
117         }
118         /sbin/iptables -D INPUT -i $iface -d $ip -j DROP
119         [ $failed = 0 ] || {
120                  echo "Failed to del $ip on dev $iface"
121                  exit 1
122         }
123
124         # flush our route cache
125         echo 1 > /proc/sys/net/ipv4/route/flush
126         ;;
127
128
129      ###########################################
130      # called when ctdbd has finished a recovery
131      recovered)
132         ;;
133
134      ####################################
135      # called when ctdbd is shutting down
136      shutdown)
137         ;;
138
139      monitor)
140         INTERFACES=`cat $CTDB_PUBLIC_ADDRESSES | 
141                 sed -e "s/^[^\t ]*[\t ]*//" -e "s/[\t ]*$//"`
142
143         [ "$CTDB_PUBLIC_INTERFACE" ] && INTERFACES="$CTDB_PUBLIC_INTERFACE $INTERFACES"
144
145         INTERFACES=`for IFACE in $INTERFACES ; do echo $IFACE ; done | sort | uniq`
146
147
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"
152                         exit 1
153                 }
154             else
155                 [ -z "$IFACE" ] || {
156                     /usr/sbin/ethtool $IFACE | grep 'Link detected: yes' > /dev/null || {
157                         echo "ERROR: No link on the public network interface $IFACE"
158                         exit 1
159                     }
160                 }
161             fi
162         done
163         ;;
164
165 esac
166
167 exit 0
168
169
170