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