e9cb4cab899eb9874dd352f45ba4b169759bdc4e
[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     if [ -x /sbin/modprobe -a -f /proc/modules ]
116     then
117         modprobe -q "$1" || true
118     fi
119 }
120
121 do_mount() {
122     if ! grep -E -qs "$1\$" /proc/filesystems
123     then
124         return 1
125     fi
126     if ! mountpoint -q "$2"
127     then
128         mount -t "$1" "$1" "$2"
129         return
130     fi
131     return 0
132 }
133
134 do_umount() {
135     if mountpoint -q "$1"
136     then
137         umount "$1"
138     fi
139     return 0
140 }
141
142 # See how we were called.
143 case "$1" in
144   start)
145         log_daemon_msg "Starting $DESC"
146
147         log_progress_msg "statd"
148         start-stop-daemon --start --oknodo --quiet \
149             --exec $PREFIX/sbin/rpc.statd -- $STATDOPTS
150         if [ $? != 0 ]; then
151             log_end_msg $?
152             exit $?
153         fi
154
155         if [ "$NEED_LOCKD" = yes ]
156         then
157             log_progress_msg "lockd"
158             start-stop-daemon --start --oknodo --quiet \
159                 --exec $PREFIX/sbin/rpc.lockd
160             if [ $? != 0 ]; then
161                 log_end_msg $?
162                 exit $?
163             fi
164         fi
165         if [ "$NEED_IDMAPD" = yes ] || [ "$NEED_GSSD" = yes ]
166         then
167             do_modprobe nfs
168             do_modprobe nfs4
169             if do_mount rpc_pipefs $PIPEFS_MOUNTPOINT
170             then
171                 if [ "$NEED_IDMAPD" = yes ]
172                 then
173                     log_progress_msg "idmapd"
174                     start-stop-daemon --start --oknodo --quiet \
175                             --exec /usr/sbin/rpc.idmapd
176                     if [ $? != 0 ]; then
177                         log_end_msg $?
178                         exit $?
179                     fi
180                 fi
181                 if [ "$NEED_GSSD" = yes ]
182                 then
183                     do_modprobe rpcsec_gss_krb5
184                     log_progress_msg "gssd"
185
186                     # we need this available; better to fail now than
187                     # mysteriously on the first mount
188                     if ! grep -q -E '^nfs[       ]' /etc/services; then
189                         log_action_end_msg 1 "broken /etc/services, please see /usr/share/doc/nfs-common/README.Debian.nfsv4"
190                         exit 1
191                     fi
192
193                     start-stop-daemon --start --oknodo --quiet \
194                             --exec /usr/sbin/rpc.gssd -- $RPCGSSDOPTS
195                     if [ $? != 0 ]; then
196                         log_end_msg $?
197                         exit $?
198                     fi
199                 fi
200             fi
201         fi
202         log_end_msg 0
203         ;;
204
205   stop)
206         log_daemon_msg "Stopping $DESC"
207
208         if [ "$NEED_GSSD" = yes ]
209         then
210             log_progress_msg "gssd"
211             start-stop-daemon --stop --oknodo --quiet \
212                     --name rpc.gssd
213             if [ $? != 0 ]; then
214                 log_end_msg $?
215                 exit $?
216             fi
217         fi
218         if [ "$NEED_IDMAPD" = yes ]
219         then
220             log_progress_msg "idmapd"
221             start-stop-daemon --stop --oknodo --quiet \
222                 --name rpc.idmapd
223             if [ $? != 0 ]; then
224                 log_end_msg $?
225                 exit $?
226             fi
227         fi
228         if [ "$NEED_LOCKD" = yes ]
229         then
230             log_progress_msg "lockd"
231             start-stop-daemon --stop --oknodo --quiet \
232                 --name rpc.lockd
233             if [ $? != 0 ]; then
234                 log_end_msg $?
235                 exit $?
236             fi
237         else
238             pkill -KILL -u root -x lockd || true
239         fi
240         log_progress_msg "statd"
241         start-stop-daemon --stop --oknodo --quiet \
242             --name rpc.statd
243         if [ $? != 0 ]; then
244             log_end_msg $?
245             exit $?
246         fi
247         do_umount $PIPEFS_MOUNTPOINT 2>/dev/null || true
248         log_end_msg 0
249         ;;
250
251   status)
252         if ! pidof rpc.statd >/dev/null
253         then
254             echo "rpc.statd not running"
255             exit 3
256         fi
257
258         if [ "$NEED_GSSD" = yes ]
259         then
260             if ! pidof rpc.gssd >/dev/null
261             then
262                 echo "rpc.statd running, but rpc.gssd halted"
263                 exit 3
264             fi
265         fi
266
267         if [ "$NEED_LOCKD" = yes ]
268         then
269             if ! pidof rpc.lockd >/dev/null
270             then
271                 echo "rpc.statd running, but rpc.lockd halted"
272                 exit 3
273             fi
274         fi
275             
276         if [ "$NEED_IDMAPD" = yes ]
277         then
278             if ! pidof rpc.idmapd >/dev/null
279             then
280                 echo "rpc.statd running, but rpc.idmapd halted"
281                 exit 3
282             fi
283         fi
284
285         echo "rpc.statd running"
286         exit 0
287         ;;
288
289   restart | force-reload)
290         $0 stop
291         sleep 1
292         $0 start
293         ;;
294
295   *)
296         log_success_msg "Usage: nfs-common {start|stop|status|restart}"
297         exit 1
298         ;;
299 esac
300
301 exit 0