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