4076e31c66ecdc98aefe2d10807f76440e6f6304
[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                 if [ -d /run/sendsigs.omit.d ]; then
160                     rm -f /run/sendsigs.omit.d/statd
161                     ln -s /var/run/rpc.statd.pid /run/sendsigs.omit.d/statd
162                 fi
163             fi
164         fi
165
166         # Don't start idmapd and gssd if we don't have them (say, if /usr is not
167         # up yet).
168         [ -x /usr/sbin/rpc.idmapd ] || NEED_IDMAPD=no
169         [ -x /usr/sbin/rpc.gssd   ] || NEED_GSSD=no
170
171         if [ "$NEED_IDMAPD" = yes ] || [ "$NEED_GSSD" = yes ]
172         then
173             do_modprobe sunrpc
174             do_modprobe nfs
175             do_modprobe nfsd
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