92691f9ae51ee325d6a63f8d37336c80a13f207d
[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=/run/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             mkdir -p "$PIPEFS_MOUNTPOINT"
176             if do_mount rpc_pipefs $PIPEFS_MOUNTPOINT
177             then
178                 if [ "$NEED_IDMAPD" = yes ]
179                 then
180                     log_progress_msg "idmapd"
181                     start-stop-daemon --start --oknodo --quiet \
182                             --exec /usr/sbin/rpc.idmapd
183                     RET=$?
184                     if [ $RET != 0 ]; then
185                         log_end_msg $RET
186                         exit $RET
187                     fi
188                 fi
189                 if [ "$NEED_GSSD" = yes ]
190                 then
191                     do_modprobe rpcsec_gss_krb5
192                     log_progress_msg "gssd"
193
194                     # we need this available; better to fail now than
195                     # mysteriously on the first mount
196                     if ! grep -q -E '^nfs[       ]' /etc/services; then
197                         log_action_end_msg 1 "broken /etc/services, please see /usr/share/doc/nfs-common/README.Debian.nfsv4"
198                         exit 1
199                     fi
200
201                     start-stop-daemon --start --oknodo --quiet \
202                             --exec /usr/sbin/rpc.gssd -- $RPCGSSDOPTS
203                     RET=$?
204                     if [ $RET != 0 ]; then
205                         log_end_msg $RET
206                         exit $RET
207                     fi
208                 fi
209             fi
210         fi
211         log_end_msg 0
212         ;;
213
214   stop)
215         log_daemon_msg "Stopping $DESC"
216
217         if [ "$NEED_GSSD" = yes ]
218         then
219             log_progress_msg "gssd"
220             start-stop-daemon --stop --oknodo --quiet \
221                     --name rpc.gssd
222             RET=$?
223             if [ $RET != 0 ]; then
224                 log_end_msg $RET
225                 exit $RET
226             fi
227         fi
228         if [ "$NEED_IDMAPD" = yes ]
229         then
230             log_progress_msg "idmapd"
231             start-stop-daemon --stop --oknodo --quiet \
232                 --name rpc.idmapd
233             RET=$?
234             if [ $RET != 0 ]; then
235                 log_end_msg $RET
236                 exit $RET
237             fi
238         fi
239         if [ "$NEED_STATD" = yes ]
240         then
241             log_progress_msg "statd"
242             start-stop-daemon --stop --oknodo --quiet \
243                 --name rpc.statd
244             RET=$?
245             if [ $RET != 0 ]; then
246                 log_end_msg $RET
247                 exit $RET
248             fi
249         fi
250         do_umount $PIPEFS_MOUNTPOINT 2>/dev/null || true
251         log_end_msg 0
252         ;;
253
254   status)
255         if [ "$NEED_STATD" = yes ]
256         then
257             if ! pidof rpc.statd >/dev/null
258             then
259                 echo "rpc.statd not running"
260                 exit 3
261             fi
262         fi
263
264         if [ "$NEED_GSSD" = yes ]
265         then
266             if ! pidof rpc.gssd >/dev/null
267             then
268                 echo "rpc.gssd not running"
269                 exit 3
270             fi
271         fi
272
273         if [ "$NEED_IDMAPD" = yes ]
274         then
275             if ! pidof rpc.idmapd >/dev/null
276             then
277                 echo "rpc.idmapd not running"
278                 exit 3
279             fi
280         fi
281
282         echo "all daemons running"
283         exit 0
284         ;;
285
286   restart | force-reload)
287         $0 stop
288         sleep 1
289         $0 start
290         ;;
291
292   *)
293         log_success_msg "Usage: nfs-common {start|stop|status|restart}"
294         exit 1
295         ;;
296 esac
297
298 exit 0