Imported Debian patch 1.0.9-8
[nfs-utils.git] / debian / nfs-common.init
1 #!/bin/sh
2
3 ### BEGIN INIT INFO
4 # Provides:          nfs-common
5 # Required-Start:    $time
6 # Default-Start:     2 3 4 5
7 # Default-Stop:      0 1 6
8 # Short-Description: NFS support files common to client and server
9 # Description:       NFS is a popular protocol for file sharing across
10 #                    TCP/IP networks. This service provides various
11 #                    support functions for NFS mounts.
12 ### END INIT INFO
13
14 # What is this?
15 DESC="NFS common utilities"
16
17 # Read config
18 DEFAULTFILE=/etc/default/nfs-common
19 PREFIX=
20 NEED_LOCKD=
21 NEED_IDMAPD=
22 NEED_GSSD=
23 PIPEFS_MOUNTPOINT=/var/lib/nfs/rpc_pipefs
24 RPCGSSDOPTS=
25 if [ -f $DEFAULTFILE ]; then
26     . $DEFAULTFILE
27 fi
28
29 . /lib/lsb/init-functions
30
31 # Determine whether lockd daemon is required.
32 case "$NEED_LOCKD" in
33 yes|no) ;;
34 *)  case `uname -r` in
35     '' | [01].* | 2.[0123].* )
36         # Older kernels may or may not need a lockd daemon.
37         # We must assume they do, unless we can prove otherwise.
38         # (A false positive here results only in a harmless message.)
39         NEED_LOCKD=yes
40         if test -f /proc/ksyms
41         then
42             grep -q lockdctl /proc/ksyms || NEED_LOCKD=no
43         fi
44         ;;
45
46     *)  # Modern kernels (>= 2.4) start a lockd thread automatically.
47         NEED_LOCKD=no
48         ;;
49     esac
50     ;;
51 esac
52
53 #
54 # Parse the fstab file, and determine whether we need idmapd and gssd. (The
55 # /etc/defaults settings, if any, will override our autodetection.) This code
56 # is partially adapted from the mountnfs.sh script in the sysvinit package.
57 #
58 AUTO_NEED_IDMAPD=no
59 AUTO_NEED_GSSD=no
60
61 exec 9<&0 </etc/fstab
62
63 while read DEV MTPT FSTYPE OPTS REST
64 do
65     if [ "$FSTYPE" = "nfs4" ]; then
66         AUTO_NEED_IDMAPD=yes
67     fi
68     case "$OPTS" in
69         sec=krb5|*,sec=krb5|sec=krb5,*|*,sec=krb5i,*|sec=krb5i|*,sec=krb5i|sec=krb5i,*|*,sec=krb5i,*|sec=krb5p|*,sec=krb5p|sec=krb5p,*|*,sec=krb5p,*)
70             AUTO_NEED_GSSD=yes
71         ;;
72     esac
73 done
74
75 exec 0<&9 9<&-
76
77 #
78 # We also need idmapd if we run an NFSv4 server. It's fairly difficult
79 # to autodetect whether there are NFSv4 exports or not, and idmapd is not a
80 # particularily heavy daemon, so we auto-enable it if we find an /etc/exports
81 # file. This does not mean that there are NFSv4 or other mounts active (or
82 # even that nfs-kernel-server is installed), but it matches what the "start"
83 # condition in nfs-kernel-server's init script does, which has a value in
84 # itself.
85 #
86 if [ -f /etc/exports ]; then
87     AUTO_NEED_IDMAPD=yes
88 fi
89
90
91 case "$NEED_IDMAPD" in
92     yes|no)     
93         ;;
94     *)
95         NEED_IDMAPD=$AUTO_NEED_IDMAPD
96         ;;
97 esac
98
99 case "$NEED_GSSD" in
100     yes|no)     
101         ;;
102     *)
103         NEED_GSSD=$AUTO_NEED_GSSD
104         ;;
105 esac
106
107 # Exit if required binaries are missing.
108 [ -x $PREFIX/sbin/rpc.statd ] || exit 0
109 [ -x $PREFIX/sbin/rpc.lockd ] || [ "$NEED_LOCKD"  = no ] || exit 0
110 [ -x /usr/sbin/rpc.idmapd   ] || [ "$NEED_IDMAPD" = no ] || exit 0
111 [ -x /usr/sbin/rpc.gssd     ] || [ "$NEED_GSSD"   = no ] || exit 0
112
113 do_modprobe() {
114     modprobe -q "$1" || true
115 }
116
117 do_mount() {
118     if ! grep -E -qs "$1\$" /proc/filesystems
119     then
120         return 1
121     fi
122     if ! mountpoint -q "$2"
123     then
124         mount -t "$1" "$1" "$2"
125         return
126     fi
127     return 0
128 }
129
130 do_umount() {
131     if mountpoint -q "$1"
132     then
133         umount "$1"
134     fi
135     return 0
136 }
137
138 # See how we were called.
139 case "$1" in
140   start)
141         log_daemon_msg "Starting $DESC"
142
143         log_progress_msg "statd"
144         start-stop-daemon --start --oknodo --quiet \
145             --exec $PREFIX/sbin/rpc.statd -- $STATDOPTS
146         if [ $? != 0 ]; then
147             log_end_msg $?
148             exit $?
149         fi
150
151         if [ "$NEED_LOCKD" = yes ]
152         then
153             log_progress_msg "lockd"
154             start-stop-daemon --start --oknodo --quiet \
155                 --exec $PREFIX/sbin/rpc.lockd
156             if [ $? != 0 ]; then
157                 log_end_msg $?
158                 exit $?
159             fi
160         fi
161         if [ "$NEED_IDMAPD" = yes ] || [ "$NEED_GSSD" = yes ]
162         then
163             do_modprobe nfs
164             do_modprobe nfs4
165             if do_mount rpc_pipefs $PIPEFS_MOUNTPOINT
166             then
167                 if [ "$NEED_IDMAPD" = yes ]
168                 then
169                     log_progress_msg "idmapd"
170                     start-stop-daemon --start --oknodo --quiet \
171                             --exec /usr/sbin/rpc.idmapd
172                     if [ $? != 0 ]; then
173                         log_end_msg $?
174                         exit $?
175                     fi
176                 fi
177                 if [ "$NEED_GSSD" = yes ]
178                 then
179                     do_modprobe rpcsec_gss_krb5
180                     log_progress_msg "gssd"
181                     start-stop-daemon --start --oknodo --quiet \
182                             --exec /usr/sbin/rpc.gssd -- $RPCGSSDOPTS
183                     if [ $? != 0 ]; then
184                         log_end_msg $?
185                         exit $?
186                     fi
187                 fi
188             fi
189         fi
190         log_end_msg 0
191         ;;
192
193   stop)
194         log_daemon_msg "Stopping $DESC"
195
196         if [ "$NEED_GSSD" = yes ]
197         then
198             log_progress_msg "gssd"
199             start-stop-daemon --stop --oknodo --quiet \
200                     --name rpc.gssd
201             if [ $? != 0 ]; then
202                 log_end_msg $?
203                 exit $?
204             fi
205         fi
206         if [ "$NEED_IDMAPD" = yes ]
207         then
208             log_progress_msg "idmapd"
209             start-stop-daemon --stop --oknodo --quiet \
210                 --name rpc.idmapd
211             if [ $? != 0 ]; then
212                 log_end_msg $?
213                 exit $?
214             fi
215         fi
216         if [ "$NEED_LOCKD" = yes ]
217         then
218             log_progress_msg "lockd"
219             start-stop-daemon --stop --oknodo --quiet \
220                 --name rpc.lockd
221             if [ $? != 0 ]; then
222                 log_end_msg $?
223                 exit $?
224             fi
225         else
226             pkill -KILL -u root -x lockd || true
227         fi
228         log_progress_msg "statd"
229         start-stop-daemon --stop --oknodo --quiet \
230             --name rpc.statd
231         if [ $? != 0 ]; then
232             log_end_msg $?
233             exit $?
234         fi
235         do_umount $PIPEFS_MOUNTPOINT 2>/dev/null || true
236         log_end_msg 0
237         ;;
238
239   status)
240         if ! pidof rpc.statd >/dev/null
241         then
242             echo "rpc.statd not running"
243             exit 3
244         fi
245
246         if [ "$NEED_GSSD" = yes ]
247         then
248             if ! pidof rpc.gssd >/dev/null
249             then
250                 echo "rpc.statd running, but rpc.gssd halted"
251                 exit 3
252             fi
253         fi
254
255         if [ "$NEED_LOCKD" = yes ]
256         then
257             if ! pidof rpc.lockd >/dev/null
258             then
259                 echo "rpc.statd running, but rpc.lockd halted"
260                 exit 3
261             fi
262         fi
263             
264         if [ "$NEED_IDMAPD" = yes ]
265         then
266             if ! pidof rpc.idmapd >/dev/null
267             then
268                 echo "rpc.statd running, but rpc.idmapd halted"
269                 exit 3
270             fi
271         fi
272
273         echo "rpc.statd running"
274         exit 0
275         ;;
276
277   restart | force-reload)
278         $0 stop
279         sleep 1
280         $0 start
281         ;;
282
283   *)
284         log_success_msg "Usage: nfs-common {start|stop|status|restart}"
285         exit 1
286         ;;
287 esac
288
289 exit 0