add a missing ||
[sahlberg/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
10
11 [ -z "$CTDB_PUBLIC_ADDRESSES" ] && {
12         CTDB_PUBLIC_ADDRESSES=$CTDB_BASE/public_addresses
13 }
14
15 [ ! -f "$CTDB_PUBLIC_ADDRESSES" ] && {
16         echo "No public addresses file found. Nothing to do for 10.interfaces"
17         exit 0
18 }
19
20 add_failover_block() {
21         # Make sure our chain exists
22         iptables -N ctdbfailover 2> /dev/null
23         
24         # make sure we link to it from INPUT
25         iptables -L INPUT -n | grep ctdbfailover >/dev/null 2>/dev/null || {
26                 iptables -I INPUT -j ctdbfailover
27         }
28         # block this ip
29         iptables -I ctdbfailover -i $1 -d $2 -j DROP
30 }
31
32 delete_failover_block() {
33         iptables -D ctdbfailover -i $1 -d $2 -j DROP 2>/dev/null
34 }
35
36 delete_all_failover_blocks() {
37         # make sure to remova all links to the ctdbfailover table
38         while iptables -L INPUT -n | grep ctdbfailover >/dev/null 2>/dev/null ; do
39                 iptables -D INPUT -j ctdbfailover
40         done
41         iptables -F ctdbfailover 2>/dev/null
42         iptables -X ctdbfailover 2>/dev/null
43 }
44
45 case "$1" in 
46      #############################
47      # called when ctdbd starts up
48      startup)
49         # make sure that we only respond to ARP messages from the NIC where
50         # a particular ip address is associated.
51         [ -f /proc/sys/net/ipv4/conf/all/arp_filter ] && {
52             echo 1 > /proc/sys/net/ipv4/conf/all/arp_filter
53         }
54         cat "$CTDB_PUBLIC_ADDRESSES" | cut -d/ -f1 | while read _IP; do
55                 _IP_HELD=`/sbin/ip addr show | grep "inet $_IP/"`
56                 [ -z "$_IP_HELD" ] || {
57                         _IFACE=`echo $_IP_HELD | sed -e "s/.*\s//"`
58                         _NM=`echo $_IP_HELD | sed -e "s/.*$_IP\///" -e "s/\s.*//"`
59                         echo "Removing public address $_IP/$_NM from device $_IFACE"
60                         /sbin/ip addr del $_IP/$_NM dev $_IFACE
61                 }
62         done
63         ;;
64
65
66      ################################################
67      # called when ctdbd wants to claim an IP address
68      takeip)
69         if [ $# != 4 ]; then
70            echo "must supply interface, IP and maskbits"
71            exit 1
72         fi
73         iface=$2
74         ip=$3
75         maskbits=$4
76
77         # we make sure the interface is up first
78         /sbin/ip link set $iface up || {
79                  echo "Failed to bringup interface $iface"
80                  exit 1
81         }
82         /sbin/ip addr add $ip/$maskbits brd + dev $iface || {
83                  echo "Failed to add $ip/$maskbits on dev $iface"
84         }
85         # cope with the script being killed while we have the interface blocked
86         delete_failover_block $iface $ip
87
88         # flush our route cache
89         echo 1 > /proc/sys/net/ipv4/route/flush
90         ;;
91
92
93      ##################################################
94      # called when ctdbd wants to release an IP address
95      releaseip)
96         if [ $# != 4 ]; then
97            echo "must supply interface, IP and maskbits"
98            exit 1
99         fi
100
101         # releasing an IP is a bit more complex than it seems. Once the IP
102         # is released, any open tcp connections to that IP on this host will end
103         # up being stuck. Some of them (such as NFS connections) will be unkillable
104         # so we need to use the killtcp ctdb function to kill them off. We also
105         # need to make sure that no new connections get established while we are 
106         # doing this! So what we do is this:
107         # 1) firewall this IP, so no new external packets arrive for it
108         # 2) use netstat -tn to find existing connections, and kill them 
109         # 3) remove the IP from the interface
110         # 4) remove the firewall rule
111         iface=$2
112         ip=$3
113         maskbits=$4
114
115         failed=0
116         # we do an extra delete to cope with the script being killed
117         delete_failover_block $iface $ip
118         add_failover_block $iface $ip
119         kill_tcp_connections $ip
120
121         # the ip tool will delete all secondary IPs if this is the primary. To work around
122         # this _very_ annoying behaviour we have to keep a record of the secondaries and re-add
123         # them afterwards. yuck
124         secondaries=""
125         if /sbin/ip addr list dev $iface primary | grep -q "inet $ip/$maskbits " ; then
126             secondaries=`/sbin/ip addr list dev $iface secondary | grep " inet " | awk '{print $2}'`
127         fi
128         /sbin/ip addr del $ip/$maskbits dev $iface || failed=1
129         [ -z "$secondaries" ] || {
130             for i in $secondaries; do
131                 if /sbin/ip addr list dev $iface | grep -q "inet $i" ; then
132                     echo "kept secondary $i on dev $iface"
133                 else 
134                     echo "re-adding secondary address $i to dev $iface"
135                     /sbin/ip addr add $i dev $iface || failed=1         
136                 fi
137             done
138         }
139         delete_failover_block $iface $ip
140         [ $failed = 0 ] || {
141                  echo "Failed to del $ip on dev $iface"
142                  exit 1
143         }
144
145         # flush our route cache
146         echo 1 > /proc/sys/net/ipv4/route/flush
147         ;;
148
149
150      ###########################################
151      # called when ctdbd has finished a recovery
152      recovered)
153         delete_all_failover_blocks
154         ;;
155
156      ####################################
157      # called when ctdbd is shutting down
158      shutdown)
159         delete_all_failover_blocks
160         ;;
161
162      monitor)
163         # make sure we dont block any ips when we are outside of recovery
164         delete_all_failover_blocks
165
166         INTERFACES=`cat $CTDB_PUBLIC_ADDRESSES | 
167                 sed -e "s/^[^\t ]*[\t ]*//" -e "s/[\t ]*$//"`
168
169         [ "$CTDB_PUBLIC_INTERFACE" ] && INTERFACES="$CTDB_PUBLIC_INTERFACE $INTERFACES"
170         [ "$CTDB_NATGW_PUBLIC_IFACE" ] && INTERFACES="$CTDB_NATGW_PUBLIC_IFACE $INTERFACES"
171
172         INTERFACES=`for IFACE in $INTERFACES ; do echo $IFACE ; done | sort | uniq`
173
174         for IFACE in $INTERFACES ; do
175             # These interfaces are sometimes bond devices
176             # When we use VLANs for bond interfaces, there will only
177             # be an entry in /proc for the underlying real interface
178             REALIFACE=`echo $IFACE |sed -e 's/\..*$//'`
179             [ -f /proc/net/bonding/$REALIFACE ] && {
180                 grep -q 'Currently Active Slave: None' /proc/net/bonding/$REALIFACE && {
181                         echo "ERROR: No active slaves for bond device $REALIFACE"
182                         exit 1
183                 }
184                 grep -q '^MII Status: up' /proc/net/bonding/$REALIFACE || {
185                         echo "ERROR: public network interface $REALIFACE is down"
186                         exit 1
187                 }
188                 exit 0;
189             }
190
191             case $IFACE in 
192             ib*)
193                 # we dont know how to test ib links
194                 ;;
195             *)
196                 [ -z "$IFACE" ] || {
197                     [ "$(basename $(readlink /sys/class/net/$IFACE/device/driver))" = virtio_net ] ||
198                     ethtool $IFACE | grep -q 'Link detected: yes' || {
199                         # On some systems, this is not successful when a
200                         # cable is plugged but the interface has not been
201                         # brought up previously. Bring the interface up and
202                         # try again...
203                         ip link set $IFACE up
204                         ethtool $IFACE | grep -q 'Link detected: yes' || {
205                             echo "ERROR: No link on the public network interface $IFACE"
206                             exit 1
207                         }
208                     }
209                 }
210                 ;;
211             esac
212         done
213         ;;
214     *)
215         ctdb_standard_event_handler "$@"
216         ;;
217 esac
218
219 exit 0
220