Merge commit 'origin/master'
[obnox/samba/samba-obnox.git] / ctdb / 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 brd + dev $iface || {
61                  echo "Failed to add $ip/$maskbits on dev $iface"
62         }
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
65
66         # flush our route cache
67         echo 1 > /proc/sys/net/ipv4/route/flush
68         ;;
69
70
71      ##################################################
72      # called when ctdbd wants to release an IP address
73      releaseip)
74         if [ $# != 3 ]; then
75            echo "must supply interface, IP and maskbits"
76            exit 1
77         fi
78
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
89         iface=$1
90         ip=$2
91         maskbits=$3
92
93         failed=0
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
98
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
102         secondaries=""
103         if /sbin/ip addr list dev $iface primary | grep -q "inet $ip/$maskbits " ; then
104             secondaries=`/sbin/ip addr list dev $iface secondary | grep " inet " | awk '{print $2}'`
105         fi
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 -q "inet $i" ; then
110                     echo "kept secondary $i on dev $iface"
111                 else 
112                     echo "re-adding secondary address $i to dev $iface"
113                     /sbin/ip addr add $i dev $iface || failed=1         
114                 fi
115             done
116         }
117         iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
118         [ $failed = 0 ] || {
119                  echo "Failed to del $ip on dev $iface"
120                  exit 1
121         }
122
123         # flush our route cache
124         echo 1 > /proc/sys/net/ipv4/route/flush
125         ;;
126
127
128      ###########################################
129      # called when ctdbd has finished a recovery
130      recovered)
131         ;;
132
133      ####################################
134      # called when ctdbd is shutting down
135      shutdown)
136         ;;
137
138      monitor)
139         INTERFACES=`cat $CTDB_PUBLIC_ADDRESSES | 
140                 sed -e "s/^[^\t ]*[\t ]*//" -e "s/[\t ]*$//"`
141
142         [ "$CTDB_PUBLIC_INTERFACE" ] && INTERFACES="$CTDB_PUBLIC_INTERFACE $INTERFACES"
143
144         INTERFACES=`for IFACE in $INTERFACES ; do echo $IFACE ; done | sort | uniq`
145
146         for IFACE in $INTERFACES ; do
147             case $IFACE in 
148             ethX*|bond*)
149                 IFACE=`echo $IFACE |sed -e 's/\....$//'`
150                 grep -q 'Currently Active Slave: None' /proc/net/bonding/$IFACE && {
151                         echo "ERROR: No active slaves for bond device $IFACE"
152                         exit 1
153                 }
154                 grep -q '^MII Status: up' /proc/net/bonding/$IFACE || {
155                         echo "ERROR: public network interface $IFACE is down"
156                         exit 1
157                 }
158                 ;;
159             ib*)
160                 # we dont know how to test ib links
161                 ;;
162             *)
163                 [ -z "$IFACE" ] || {
164                     /usr/sbin/ethtool $IFACE | grep -q 'Link detected: yes' || {
165                         # On some systems, this is not successful when a
166                         # cable is plugged but the interface has not been
167                         # brought up previously. Bring the interface up and
168                         # try again...
169                         /sbin/ip link set $IFACE up
170                         /usr/sbin/ethtool $IFACE | grep -q 'Link detected: yes' || {
171                             echo "ERROR: No link on the public network interface $IFACE"
172                             exit 1
173                         }
174                     }
175                 }
176                 ;;
177             esac
178         done
179         ;;
180
181 esac
182
183 exit 0
184
185
186