Only use rpcbind's rpcinfo when it's available
[nfs-utils.git] / debian / nfs-common.init
1 #!/bin/bash
2
3 ### BEGIN INIT INFO
4 # Provides:          nfs-common
5 # Required-Start:    $portmap $time
6 # Required-Stop:     $time
7 # Default-Start:     2 3 4 5 S
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_STATD=
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 # Exit if required binaries are missing.
33 [ -x $PREFIX/sbin/rpc.statd ] || exit 0
34
35 #
36 # Parse the fstab file, and determine whether we need idmapd and gssd. (The
37 # /etc/defaults settings, if any, will override our autodetection.) This code
38 # is partially adapted from the mountnfs.sh script in the sysvinit package.
39 #
40 AUTO_NEED_IDMAPD=no
41 AUTO_NEED_GSSD=no
42
43 if [ -f /etc/fstab ]; then
44     exec 9<&0 </etc/fstab
45
46     while read DEV MTPT FSTYPE OPTS REST
47     do
48         case $DEV in
49             ''|\#*)
50                 continue
51                 ;;
52         esac
53         # FSTYPE "nfs" can be NFSv4 now
54         #if [ "$FSTYPE" = "nfs4" ]; then
55             AUTO_NEED_IDMAPD=yes
56         #fi
57         case "$OPTS" in
58
59             sec=krb5|*,sec=krb5|sec=krb5,*|*,sec=krb5i,*|sec=krb5i|*,sec=krb5i|sec=krb5i,*|*,sec=krb5i,*|sec=krb5p|*,sec=krb5p|sec=krb5p,*|*,sec=krb5p,*)
60             AUTO_NEED_GSSD=yes
61             ;;
62         esac
63     done
64
65     exec 0<&9 9<&-
66 fi
67
68 #
69 # We also need idmapd if we run an NFSv4 server. It's fairly difficult
70 # to autodetect whether there are NFSv4 exports or not, and idmapd is not a
71 # particularily heavy daemon, so we auto-enable it if we find an /etc/exports
72 # file. This does not mean that there are NFSv4 or other mounts active (or
73 # even that nfs-kernel-server is installed), but it matches what the "start"
74 # condition in nfs-kernel-server's init script does, which has a value in
75 # itself.
76 #
77 if [ -f /etc/exports ] && grep -q '^[[:space:]]*[^#]*/' /etc/exports; then
78     AUTO_NEED_IDMAPD=yes
79 fi
80
81 case "$NEED_STATD" in
82     yes|no)
83         ;;
84     *)
85         NEED_STATD=yes
86         ;;
87 esac
88
89 case "$NEED_IDMAPD" in
90     yes|no)     
91         ;;
92     *)
93         NEED_IDMAPD=$AUTO_NEED_IDMAPD
94         ;;
95 esac
96
97 case "$NEED_GSSD" in
98     yes|no)     
99         ;;
100     *)
101         NEED_GSSD=$AUTO_NEED_GSSD
102         ;;
103 esac
104
105 do_modprobe() {
106     if [ -x /sbin/modprobe -a -f /proc/modules ]
107     then
108         modprobe -q "$1" || true
109     fi
110 }
111
112 do_mount() {
113     if ! grep -E -qs "$1\$" /proc/filesystems
114     then
115         return 1
116     fi
117     if ! mountpoint -q "$2"
118     then
119         mount -t "$1" "$1" "$2"
120         return
121     fi
122     return 0
123 }
124
125 do_umount() {
126     if mountpoint -q "$1"
127     then
128         umount "$1"
129     fi
130     return 0
131 }
132
133 # See how we were called.
134 case "$1" in
135   start)
136         log_daemon_msg "Starting $DESC"
137
138         if [ "$NEED_STATD" = yes ]; then
139             log_progress_msg "statd"
140             
141             # See if rpcbind is running
142             if [ -x /usr/sbin/rpcinfo ]; then
143                 /usr/sbin/rpcinfo -p >/dev/null 2>&1
144                 RET=$?
145                 if [ $RET != 0 ]; then
146                    echo
147                    log_warning_msg "Not starting: portmapper is not running"
148                    exit 0
149                 fi
150             fi
151             start-stop-daemon --start --oknodo --quiet \
152                 --pidfile /var/run/rpc.statd.pid \
153                 --exec $PREFIX/sbin/rpc.statd -- $STATDOPTS
154             RET=$?
155             if [ $RET != 0 ]; then
156                 log_end_msg $RET
157                 exit $RET
158             else
159                 mkdir -p /lib/init/rw/sendsigs.omit.d
160                 rm -f /lib/init/rw/sendsigs.omit.d/statd
161                 ln -s /var/run/rpc.statd.pid /lib/init/rw/sendsigs.omit.d/statd
162             fi
163         fi
164
165         # Don't start idmapd and gssd if we don't have them (say, if /usr is not
166         # up yet).
167         [ -x /usr/sbin/rpc.idmapd ] || NEED_IDMAPD=no
168         [ -x /usr/sbin/rpc.gssd   ] || NEED_GSSD=no
169
170         if [ "$NEED_IDMAPD" = yes ] || [ "$NEED_GSSD" = yes ]
171         then
172             do_modprobe sunrpc
173             do_modprobe nfs
174             do_modprobe nfsd
175             if do_mount rpc_pipefs $PIPEFS_MOUNTPOINT
176             then
177                 if [ "$NEED_IDMAPD" = yes ]
178                 then
179                     log_progress_msg "idmapd"
180                     start-stop-daemon --start --oknodo --quiet \
181                             --exec /usr/sbin/rpc.idmapd
182                     RET=$?
183                     if [ $RET != 0 ]; then
184                         log_end_msg $RET
185                         exit $RET
186                     fi
187                 fi
188                 if [ "$NEED_GSSD" = yes ]
189                 then
190                     do_modprobe rpcsec_gss_krb5
191                     log_progress_msg "gssd"
192
193                     # we need this available; better to fail now than
194                     # mysteriously on the first mount
195                     if ! grep -q -E '^nfs[       ]' /etc/services; then
196                         log_action_end_msg 1 "broken /etc/services, please see /usr/share/doc/nfs-common/README.Debian.nfsv4"
197                         exit 1
198                     fi
199
200                     start-stop-daemon --start --oknodo --quiet \
201                             --exec /usr/sbin/rpc.gssd -- $RPCGSSDOPTS
202                     RET=$?
203                     if [ $RET != 0 ]; then
204                         log_end_msg $RET
205                         exit $RET
206                     fi
207                 fi
208             fi
209         fi
210         log_end_msg 0
211         ;;
212
213   stop)
214         log_daemon_msg "Stopping $DESC"
215
216         if [ "$NEED_GSSD" = yes ]
217         then
218             log_progress_msg "gssd"
219             start-stop-daemon --stop --oknodo --quiet \
220                     --name rpc.gssd
221             RET=$?
222             if [ $RET != 0 ]; then
223                 log_end_msg $RET
224                 exit $RET
225             fi
226         fi
227         if [ "$NEED_IDMAPD" = yes ]
228         then
229             log_progress_msg "idmapd"
230             start-stop-daemon --stop --oknodo --quiet \
231                 --name rpc.idmapd
232             RET=$?
233             if [ $RET != 0 ]; then
234                 log_end_msg $RET
235                 exit $RET
236             fi
237         fi
238         if [ "$NEED_STATD" = yes ]
239         then
240             log_progress_msg "statd"
241             start-stop-daemon --stop --oknodo --quiet \
242                 --name rpc.statd
243             RET=$?
244             if [ $RET != 0 ]; then
245                 log_end_msg $RET
246                 exit $RET
247             fi
248         fi
249         do_umount $PIPEFS_MOUNTPOINT 2>/dev/null || true
250         log_end_msg 0
251         ;;
252
253   status)
254         if [ "$NEED_STATD" = yes ]
255         then
256             if ! pidof rpc.statd >/dev/null
257             then
258                 echo "rpc.statd not running"
259                 exit 3
260             fi
261         fi
262
263         if [ "$NEED_GSSD" = yes ]
264         then
265             if ! pidof rpc.gssd >/dev/null
266             then
267                 echo "rpc.gssd not running"
268                 exit 3
269             fi
270         fi
271
272         if [ "$NEED_IDMAPD" = yes ]
273         then
274             if ! pidof rpc.idmapd >/dev/null
275             then
276                 echo "rpc.idmapd not running"
277                 exit 3
278             fi
279         fi
280
281         echo "all daemons running"
282         exit 0
283         ;;
284
285   restart | force-reload)
286         $0 stop
287         sleep 1
288         $0 start
289         ;;
290
291   *)
292         log_success_msg "Usage: nfs-common {start|stop|status|restart}"
293         exit 1
294         ;;
295 esac
296
297 exit 0