Use rpcbind's rpcinfo everywhere (Closes: #707589).
[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 NEED_STATD=
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 # Exit if required binaries are missing.
32 [ -x /sbin/rpc.statd ] || exit 0
33
34 #
35 # Parse the fstab file, and determine whether we need idmapd and gssd. (The
36 # /etc/defaults settings, if any, will override our autodetection.) This code
37 # is partially adapted from the mountnfs.sh script in the sysvinit package.
38 #
39 AUTO_NEED_IDMAPD=no
40 AUTO_NEED_GSSD=no
41
42 if [ -f /etc/fstab ]; then
43     exec 9<&0 </etc/fstab
44
45     while read DEV MTPT FSTYPE OPTS REST
46     do
47         case $DEV in
48             ''|\#*)
49                 continue
50                 ;;
51         esac
52         # FSTYPE "nfs" can be NFSv4 now
53         #if [ "$FSTYPE" = "nfs4" ]; then
54             AUTO_NEED_IDMAPD=yes
55         #fi
56         case "$OPTS" in
57
58             sec=krb5|*,sec=krb5|sec=krb5,*|*,sec=krb5i,*|sec=krb5i|*,sec=krb5i|sec=krb5i,*|*,sec=krb5i,*|sec=krb5p|*,sec=krb5p|sec=krb5p,*|*,sec=krb5p,*)
59             AUTO_NEED_GSSD=yes
60             ;;
61         esac
62     done
63
64     exec 0<&9 9<&-
65 fi
66
67 #
68 # We also need idmapd if we run an NFSv4 server. It's fairly difficult
69 # to autodetect whether there are NFSv4 exports or not, and idmapd is not a
70 # particularily heavy daemon, so we auto-enable it if we find an /etc/exports
71 # file. This does not mean that there are NFSv4 or other mounts active (or
72 # even that nfs-kernel-server is installed), but it matches what the "start"
73 # condition in nfs-kernel-server's init script does, which has a value in
74 # itself.
75 #
76 if [ -f /etc/exports ] && grep -q '^[[:space:]]*[^#]*/' /etc/exports; then
77     AUTO_NEED_IDMAPD=yes
78 fi
79
80 case "$NEED_STATD" in
81     yes|no)
82         ;;
83     *)
84         NEED_STATD=yes
85         ;;
86 esac
87
88 case "$NEED_IDMAPD" in
89     yes|no)     
90         ;;
91     *)
92         NEED_IDMAPD=$AUTO_NEED_IDMAPD
93         ;;
94 esac
95
96 case "$NEED_GSSD" in
97     yes|no)     
98         ;;
99     *)
100         NEED_GSSD=$AUTO_NEED_GSSD
101         ;;
102 esac
103
104 do_modprobe() {
105     if [ -x /sbin/modprobe -a -f /proc/modules ]
106     then
107         modprobe -q "$1" || true
108     fi
109 }
110
111 do_mount() {
112     if ! grep -E -qs "$1\$" /proc/filesystems
113     then
114         return 1
115     fi
116     if ! mountpoint -q "$2"
117     then
118         mount -t "$1" "$1" "$2"
119         return
120     fi
121     return 0
122 }
123
124 do_umount() {
125     if mountpoint -q "$1"
126     then
127         umount "$1"
128     fi
129     return 0
130 }
131
132 # See how we were called.
133 case "$1" in
134   start)
135         log_daemon_msg "Starting $DESC"
136
137         if [ "$NEED_STATD" = yes ]; then
138             log_progress_msg "statd"
139             
140             # See if rpcbind is running
141             if [ -x /usr/sbin/rpcinfo ]; then
142                 /usr/sbin/rpcinfo -p >/dev/null 2>&1
143                 RET=$?
144                 if [ $RET != 0 ]; then
145                    echo
146                    log_warning_msg "Not starting: portmapper is not running"
147                    exit 0
148                 fi
149             fi
150             start-stop-daemon --start --oknodo --quiet \
151                 --pidfile /var/run/rpc.statd.pid \
152                 --exec /sbin/rpc.statd -- $STATDOPTS
153             RET=$?
154             if [ $RET != 0 ]; then
155                 log_end_msg $RET
156                 exit $RET
157             else
158                 if [ -d /run/sendsigs.omit.d ]; then
159                     rm -f /run/sendsigs.omit.d/statd
160                     ln -s /var/run/rpc.statd.pid /run/sendsigs.omit.d/statd
161                 fi
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