improve the handling of hosts to notify with statd
[sahlberg/ctdb.git] / config / events.d / 60.nfs
1 #!/bin/sh
2 # script to manage nfs in a clustered environment
3
4 . /etc/ctdb/functions
5 loadconfig nfs
6
7 [ "$CTDB_MANAGES_NFS" = "yes" ] || exit 0
8 [ -z "$STATD_SHARED_DIRECTORY" ] && exit 0
9
10 cmd="$1"
11 shift
12
13 PATH=/usr/bin:/bin:/usr/sbin:/sbin:$PATH
14
15 case $cmd in 
16      startup)
17         /bin/mkdir -p /etc/ctdb/state/nfs
18         /bin/mkdir -p /etc/ctdb/state/statd/ip
19         /bin/mkdir -p $STATD_SHARED_DIRECTORY
20         ctdb_wait_directories "nfslock" "$STATD_SHARED_DIRECTORY"
21
22
23         # wait for all nfs exported directories to become available
24         nfs_dirs=`grep -v '^#' < /etc/exports | cut -d' ' -f1`
25         ctdb_wait_directories "NFS" $nfs_dirs
26
27         # make sure nfs is stopped before we start it, or it may get a bind error
28         service nfs stop > /dev/null 2>&1
29         service nfslock stop > /dev/null 2>&1
30
31         service nfslock start
32         service nfs start
33         ;;
34
35      shutdown)
36         service nfs stop
37         service nfslock stop
38         exit 0
39         ;;
40
41      takeip)
42         ip=$2
43
44         echo $ip >> /etc/ctdb/state/statd/restart
45
46         # having a list of what IPs we have allows statd to do the right 
47         # thing via /etc/ctdb/statd-callout
48         /bin/touch /etc/ctdb/state/statd/ip/$ip
49         exit 0
50         ;;
51
52      releaseip)
53         iface=$1
54         ip=$2
55         maskbits=$3
56
57         echo $ip >> /etc/ctdb/state/nfs/restart
58
59         echo $ip >> /etc/ctdb/state/statd/restart
60         /bin/rm -f /etc/ctdb/state/statd/ip/$ip
61         exit 0
62         ;;
63
64      recovered)
65         [ -f /etc/ctdb/state/nfs/restart ] && [ ! -z "$LOCKD_TCPPORT" ] && {
66                 # RST all tcp connections used for NLM to ensure that they do
67                 # not survive in ESTABLISHED state across a failover/failback
68                 # and create an ack storm
69                 netstat -tn |egrep "^tcp.*\s+[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:${LOCKD_TCPPORT}\s+.*ESTABLISHED" | awk '{print $4" "$5}' | while read dest src; do
70                         srcip=`echo $src | cut -d: -f1`
71                         srcport=`echo $src | cut -d: -f2`
72                         destip=`echo $dest | cut -d: -f1`
73                         destport=`echo $dest | cut -d: -f2`
74                         ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 
75 #                       ctdb killtcp $destip:$destport $srcip:$srcport >/dev/null 2>&1
76                 done
77         } > /dev/null 2>&1
78
79         [ -f /etc/ctdb/state/nfs/restart ] && {
80                 # RST all tcp connections used for NFS to ensure that they do
81                 # not survive in ESTABLISHED state across a failover/failback
82                 # and create an ack storm
83                 netstat -tn |egrep '^tcp.*\s+[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:2049\s+.*ESTABLISHED' | awk '{print $4" "$5}' | while read dest src; do
84                         srcip=`echo $src | cut -d: -f1`
85                         srcport=`echo $src | cut -d: -f2`
86                         destip=`echo $dest | cut -d: -f1`
87                         destport=`echo $dest | cut -d: -f2`
88                         ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 
89                         ctdb killtcp $destip:$destport $srcip:$srcport >/dev/null 2>&1
90                 done
91         } > /dev/null 2>&1
92         /bin/rm -f /etc/ctdb/state/nfs/restart
93
94         # if we have taken or released any ips we must send out
95         # statd notifications to recover lost nfs locks
96         [ -x /etc/ctdb/statd-callout ] && [ -f /etc/ctdb/state/statd/restart ] && {
97                 /etc/ctdb/statd-callout notify &
98         } >/dev/null 2>&1
99
100         /bin/rm -f /etc/ctdb/state/statd/restart
101         ;;
102
103       monitor)
104         # check that NFS responds to rpc requests
105         ctdb_check_rpc "NFS" 100003 3
106         ctdb_check_rpc "mount" 100005 1
107
108         # and that its directories are available
109         nfs_dirs=`grep -v '^#' < /etc/exports | cut -d' ' -f1`
110         ctdb_check_directories "nfs" $nfs_dirs
111
112         # check that lockd responds to rpc requests
113         ctdb_check_rpc "statd" 100024 1
114         ctdb_check_rpc "lockd" 100021 1
115         ctdb_check_directories "statd" $STATD_SHARED_DIRECTORY
116         ;;
117
118 esac
119
120 exit 0