3 #################################
4 # interface event script for ctdb
5 # this adds/removes IPs from your
11 [ -z "$CTDB_PUBLIC_ADDRESSES" ] && {
12 CTDB_PUBLIC_ADDRESSES=$CTDB_BASE/public_addresses
15 [ ! -f "$CTDB_PUBLIC_ADDRESSES" ] && {
16 echo "No public addresses file found. Nothing to do for 10.interfaces"
22 local INTERFACES=`cat $CTDB_PUBLIC_ADDRESSES |
23 sed -e "s/^[^\t ]*[\t ]*//" -e "s/,/ /g" -e "s/[\t ]*$//"`
25 [ "$CTDB_PUBLIC_INTERFACE" ] && INTERFACES="$CTDB_PUBLIC_INTERFACE $INTERFACES"
26 [ "$CTDB_NATGW_PUBLIC_IFACE" ] && INTERFACES="$CTDB_NATGW_PUBLIC_IFACE $INTERFACES"
28 local IFACES=`ctdb ifaces -Y | grep -v '^:Name:LinkStatus:References:'`
34 IFACE=`echo -n "$I" | cut -d ':' -f2`
35 INTERFACES="$IFACE $INTERFACES"
38 INTERFACES=`for IFACE in $INTERFACES ; do echo $IFACE ; done | sort | uniq`
42 for IFACE in $INTERFACES ; do
44 ip addr show $IFACE 2>/dev/null >/dev/null || {
45 echo Interface $IFACE does not exist but it is used by public addresses.
49 # These interfaces are sometimes bond devices
50 # When we use VLANs for bond interfaces, there will only
51 # be an entry in /proc for the underlying real interface
52 local REALIFACE=`echo $IFACE |sed -e 's/\..*$//'`
53 [ -f /proc/net/bonding/$REALIFACE ] && {
54 grep -q 'Currently Active Slave: None' /proc/net/bonding/$REALIFACE && {
55 echo "ERROR: No active slaves for bond device $REALIFACE"
57 ctdb setifacelink $IFACE down
60 grep -q '^MII Status: up' /proc/net/bonding/$REALIFACE || {
61 echo "ERROR: public network interface $REALIFACE is down"
63 ctdb setifacelink $IFACE down
66 ok=1 # we only set ok for interfaces known to ctdbd
67 ctdb setifacelink $IFACE up
73 # loopback is always working
74 ok=1 # we only set ok for interfaces known to ctdbd
75 ctdb setifacelink $IFACE up
78 # we dont know how to test ib links
79 ok=1 # we only set ok for interfaces known to ctdbd
80 ctdb setifacelink $IFACE up
84 [ "$(basename $(readlink /sys/class/net/$IFACE/device/driver) 2>/dev/null)" = virtio_net ] ||
85 ethtool $IFACE | grep -q 'Link detected: yes' || {
86 # On some systems, this is not successful when a
87 # cable is plugged but the interface has not been
88 # brought up previously. Bring the interface up and
90 /sbin/ip link set $IFACE up
91 ethtool $IFACE | grep -q 'Link detected: yes' || {
92 echo "ERROR: No link on the public network interface $IFACE"
94 ctdb setifacelink $IFACE down
98 ok=1 # we only set ok for interfaces known to ctdbd
99 ctdb setifacelink $IFACE up
106 test x"$fail" = x"0" && {
110 test x"$ok" = x"1" && {
118 #############################
119 # called when ctdbd starts up
121 # make sure that we only respond to ARP messages from the NIC where
122 # a particular ip address is associated.
123 [ -f /proc/sys/net/ipv4/conf/all/arp_filter ] && {
124 echo 1 > /proc/sys/net/ipv4/conf/all/arp_filter
128 #############################
129 # called after ctdbd has done its initial recovery
130 # and we start the services to become healthy
132 # Assume all links are good initially
133 INTERFACES=`for IFACE in $INTERFACES ; do echo $IFACE ; done | sort | uniq`
135 for IFACE in $INTERFACES ; do
136 ctdb setifacelink $IFACE down
144 ################################################
145 # called when ctdbd wants to claim an IP address
148 echo "must supply interface, IP and maskbits"
155 add_ip_to_iface $iface $ip $maskbits || {
159 # cope with the script being killed while we have the interface blocked
160 iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
162 # flush our route cache
163 echo 1 > /proc/sys/net/ipv4/route/flush
167 ##################################################
168 # called when ctdbd wants to release an IP address
171 echo "must supply interface, IP and maskbits"
175 # releasing an IP is a bit more complex than it seems. Once the IP
176 # is released, any open tcp connections to that IP on this host will end
177 # up being stuck. Some of them (such as NFS connections) will be unkillable
178 # so we need to use the killtcp ctdb function to kill them off. We also
179 # need to make sure that no new connections get established while we are
180 # doing this! So what we do is this:
181 # 1) firewall this IP, so no new external packets arrive for it
182 # 2) use netstat -tn to find existing connections, and kill them
183 # 3) remove the IP from the interface
184 # 4) remove the firewall rule
190 # we do an extra delete to cope with the script being killed
191 iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
192 iptables -I INPUT -i $iface -d $ip -j DROP
193 kill_tcp_connections $ip
195 delete_ip_from_iface $iface $ip $maskbits || {
196 iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
200 iptables -D INPUT -i $iface -d $ip -j DROP 2> /dev/null
202 # flush our route cache
203 echo 1 > /proc/sys/net/ipv4/route/flush
206 ##################################################
207 # called when ctdbd wants to update an IP address
210 echo "must supply old interface, new interface, IP and maskbits"
214 # moving an IP is a bit more complex than it seems.
215 # First we drop all traffic on the old interface.
216 # Then we try to add the ip to the new interface and before
217 # we finally remove it from the old interface.
219 # 1) firewall this IP, so no new external packets arrive for it
220 # 2) add the IP to the new interface
221 # 3) remove the IP from the old interface
222 # 4) remove the firewall rule
223 # 5) use ctdb gratiousarp to propagate the new mac address
224 # 6) use netstat -tn to find existing connections, and tickle them
231 # we do an extra delete to cope with the script being killed
232 iptables -D INPUT -i $oiface -d $ip -j DROP 2> /dev/null
233 iptables -I INPUT -i $oiface -d $ip -j DROP
235 delete_ip_from_iface $oiface $ip $maskbits 2>/dev/null
236 delete_ip_from_iface $niface $ip $maskbits 2>/dev/null
238 add_ip_to_iface $niface $ip $maskbits || {
239 iptables -D INPUT -i $oiface -d $ip -j DROP 2> /dev/null
243 # cope with the script being killed while we have the interface blocked
244 iptables -D INPUT -i $oiface -d $ip -j DROP 2> /dev/null
246 # flush our route cache
247 echo 1 > /proc/sys/net/ipv4/route/flush
249 # propagate the new mac address
250 ctdb gratiousarp $ip $niface
252 # tickle all existing connections, so that dropped packets
253 # are retransmited and the tcp streams work
255 tickle_tcp_connections $ip
260 ###########################################
261 # called when ctdbd has finished a recovery
265 ####################################
266 # called when ctdbd is shutting down
274 test x"$ret" = x"2" && {
275 test x"$CTDB_PARTIALLY_ONLINE_INTERFACES" != x"yes" && {
278 # as long as we have one interface available don't become
283 test x"$ret" != x"0" && {
288 ctdb_standard_event_handler "$@"