Imported Debian patch 1.1.0~rc1-1
[nfs-utils.git] / debian / nfs-common.init
1 #!/bin/sh
2
3 ### BEGIN INIT INFO
4 # Provides:          nfs-common
5 # Required-Start:    $time
6 # Required-Stop:     $time
7 # Default-Start:     2 3 4 5
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_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 #
32 # Parse the fstab file, and determine whether we need idmapd and gssd. (The
33 # /etc/defaults settings, if any, will override our autodetection.) This code
34 # is partially adapted from the mountnfs.sh script in the sysvinit package.
35 #
36 AUTO_NEED_IDMAPD=no
37 AUTO_NEED_GSSD=no
38
39 exec 9<&0 </etc/fstab
40
41 while read DEV MTPT FSTYPE OPTS REST
42 do
43     if [ "$FSTYPE" = "nfs4" ]; then
44         AUTO_NEED_IDMAPD=yes
45     fi
46     case "$OPTS" in
47         sec=krb5|*,sec=krb5|sec=krb5,*|*,sec=krb5i,*|sec=krb5i|*,sec=krb5i|sec=krb5i,*|*,sec=krb5i,*|sec=krb5p|*,sec=krb5p|sec=krb5p,*|*,sec=krb5p,*)
48             AUTO_NEED_GSSD=yes
49         ;;
50     esac
51 done
52
53 exec 0<&9 9<&-
54
55 #
56 # We also need idmapd if we run an NFSv4 server. It's fairly difficult
57 # to autodetect whether there are NFSv4 exports or not, and idmapd is not a
58 # particularily heavy daemon, so we auto-enable it if we find an /etc/exports
59 # file. This does not mean that there are NFSv4 or other mounts active (or
60 # even that nfs-kernel-server is installed), but it matches what the "start"
61 # condition in nfs-kernel-server's init script does, which has a value in
62 # itself.
63 #
64 if [ -f /etc/exports ]; then
65     AUTO_NEED_IDMAPD=yes
66 fi
67
68
69 case "$NEED_IDMAPD" in
70     yes|no)     
71         ;;
72     *)
73         NEED_IDMAPD=$AUTO_NEED_IDMAPD
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 # Exit if required binaries are missing.
86 [ -x $PREFIX/sbin/rpc.statd ] || exit 0
87 [ -x /usr/sbin/rpc.idmapd   ] || [ "$NEED_IDMAPD" = no ] || exit 0
88 [ -x /usr/sbin/rpc.gssd     ] || [ "$NEED_GSSD"   = no ] || exit 0
89
90 do_modprobe() {
91     if [ -x /sbin/modprobe -a -f /proc/modules ]
92     then
93         modprobe -q "$1" || true
94     fi
95 }
96
97 do_mount() {
98     if ! grep -E -qs "$1\$" /proc/filesystems
99     then
100         return 1
101     fi
102     if ! mountpoint -q "$2"
103     then
104         mount -t "$1" "$1" "$2"
105         return
106     fi
107     return 0
108 }
109
110 do_umount() {
111     if mountpoint -q "$1"
112     then
113         umount "$1"
114     fi
115     return 0
116 }
117
118 # See how we were called.
119 case "$1" in
120   start)
121         log_daemon_msg "Starting $DESC"
122
123         log_progress_msg "statd"
124         start-stop-daemon --start --oknodo --quiet \
125             --exec $PREFIX/sbin/rpc.statd -- $STATDOPTS
126         if [ $? != 0 ]; then
127             log_end_msg $?
128             exit $?
129         fi
130
131         if [ "$NEED_IDMAPD" = yes ] || [ "$NEED_GSSD" = yes ]
132         then
133             do_modprobe nfs
134             do_modprobe nfs4
135             if do_mount rpc_pipefs $PIPEFS_MOUNTPOINT
136             then
137                 if [ "$NEED_IDMAPD" = yes ]
138                 then
139                     log_progress_msg "idmapd"
140                     start-stop-daemon --start --oknodo --quiet \
141                             --exec /usr/sbin/rpc.idmapd
142                     if [ $? != 0 ]; then
143                         log_end_msg $?
144                         exit $?
145                     fi
146                 fi
147                 if [ "$NEED_GSSD" = yes ]
148                 then
149                     do_modprobe rpcsec_gss_krb5
150                     log_progress_msg "gssd"
151
152                     # we need this available; better to fail now than
153                     # mysteriously on the first mount
154                     if ! grep -q -E '^nfs[       ]' /etc/services; then
155                         log_action_end_msg 1 "broken /etc/services, please see /usr/share/doc/nfs-common/README.Debian.nfsv4"
156                         exit 1
157                     fi
158
159                     start-stop-daemon --start --oknodo --quiet \
160                             --exec /usr/sbin/rpc.gssd -- $RPCGSSDOPTS
161                     if [ $? != 0 ]; then
162                         log_end_msg $?
163                         exit $?
164                     fi
165                 fi
166             fi
167         fi
168         log_end_msg 0
169         ;;
170
171   stop)
172         log_daemon_msg "Stopping $DESC"
173
174         if [ "$NEED_GSSD" = yes ]
175         then
176             log_progress_msg "gssd"
177             start-stop-daemon --stop --oknodo --quiet \
178                     --name rpc.gssd
179             if [ $? != 0 ]; then
180                 log_end_msg $?
181                 exit $?
182             fi
183         fi
184         if [ "$NEED_IDMAPD" = yes ]
185         then
186             log_progress_msg "idmapd"
187             start-stop-daemon --stop --oknodo --quiet \
188                 --name rpc.idmapd
189             if [ $? != 0 ]; then
190                 log_end_msg $?
191                 exit $?
192             fi
193         fi
194         log_progress_msg "statd"
195         start-stop-daemon --stop --oknodo --quiet \
196             --name rpc.statd
197         if [ $? != 0 ]; then
198             log_end_msg $?
199             exit $?
200         fi
201         do_umount $PIPEFS_MOUNTPOINT 2>/dev/null || true
202         log_end_msg 0
203         ;;
204
205   status)
206         if ! pidof rpc.statd >/dev/null
207         then
208             echo "rpc.statd not running"
209             exit 3
210         fi
211
212         if [ "$NEED_GSSD" = yes ]
213         then
214             if ! pidof rpc.gssd >/dev/null
215             then
216                 echo "rpc.statd running, but rpc.gssd halted"
217                 exit 3
218             fi
219         fi
220
221         if [ "$NEED_IDMAPD" = yes ]
222         then
223             if ! pidof rpc.idmapd >/dev/null
224             then
225                 echo "rpc.statd running, but rpc.idmapd halted"
226                 exit 3
227             fi
228         fi
229
230         echo "rpc.statd running"
231         exit 0
232         ;;
233
234   restart | force-reload)
235         $0 stop
236         sleep 1
237         $0 start
238         ;;
239
240   *)
241         log_success_msg "Usage: nfs-common {start|stop|status|restart}"
242         exit 1
243         ;;
244 esac
245
246 exit 0