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