Imported Debian patch 1.0.7-6
[nfs-utils.git] / debian / nfs-common.init
1 #!/bin/sh
2 #
3 # nfs-common    This shell script takes care of starting and stopping
4 #               common daemons required for NFS clients and servers.
5 #
6 # chkconfig: 345 20 80
7 # description: NFS is a popular protocol for file sharing across \
8 #              TCP/IP networks. This service provides NFS file \
9 #              locking functionality.
10 #
11
12 set -e
13
14 # What is this?
15 DESC="NFS common utilities"
16
17 # Read config
18 DEFAULTFILE=/etc/default/nfs-common
19 PREFIX=
20 NEED_LOCKD=
21 NEED_IDMAPD=
22 IDMAPD_PIDFILE=/var/run/rpc.idmapd.pid
23 NEED_GSSD=
24 GSSD_PIDFILE=/var/run/rpc.gssd.pid
25 PIPEFS_MOUNTPOINT=/var/lib/nfs/rpc_pipefs
26 RPCGSSDOPTS=
27 if [ -f $DEFAULTFILE ]; then
28     . $DEFAULTFILE
29 fi
30
31 # Determine whether lockd daemon is required.
32 case "$NEED_LOCKD" in
33 yes|no) ;;
34 *)  case `uname -r` in
35     '' | [01].* | 2.[0123].* )
36         # Older kernels may or may not need a lockd daemon.
37         # We must assume they do, unless we can prove otherwise.
38         # (A false positive here results only in a harmless message.)
39         NEED_LOCKD=yes
40         if test -f /proc/ksyms
41         then
42             grep -q lockdctl /proc/ksyms || NEED_LOCKD=no
43         fi
44         ;;
45
46     *)  # Modern kernels (>= 2.4) start a lockd thread automatically.
47         NEED_LOCKD=no
48         ;;
49     esac
50     ;;
51 esac
52
53 #
54 # Parse the fstab file, and determine whether we need idmapd and gssd. (The
55 # /etc/defaults settings, if any, will override our autodetection.) This code
56 # is partially adapted from the mountnfs.sh script in the sysvinit package.
57 #
58 AUTO_NEED_IDMAPD=no
59 AUTO_NEED_GSSD=no
60
61 exec 9<&0 </etc/fstab
62
63 while read DEV MTPT FSTYPE OPTS REST
64 do
65     if [ "$FSTYPE" = "nfs4" ]; then
66         AUTO_NEED_IDMAPD=yes
67     fi
68     case "$OPTS" in
69         sec=krb5|*,sec=krb5|sec=krb5,*|*,sec=krb5i,*|sec=krb5i|*,sec=krb5i|sec=krb5i,*|*,sec=krb5i,*|sec=krb5p|*,sec=krb5p|sec=krb5p,*|*,sec=krb5p,*)
70             AUTO_NEED_GSSD=yes
71         ;;
72     esac
73 done
74
75 exec 0<&9 9<&-
76
77 case "$NEED_IDMAPD" in
78     yes|no)     
79         ;;
80     *)
81         NEED_IDMAPD=$AUTO_NEED_IDMAPD
82         ;;
83 esac
84
85 case "$NEED_GSSD" in
86     yes|no)     
87         ;;
88     *)
89         NEED_GSSD=$AUTO_NEED_GSSD
90         ;;
91 esac
92
93 # Exit if required binaries are missing.
94 [ -x $PREFIX/sbin/rpc.statd ] || exit 0
95 [ -x $PREFIX/sbin/rpc.lockd ] || [ "$NEED_LOCKD"  = no ] || exit 0
96 [ -x /usr/sbin/rpc.idmapd   ] || [ "$NEED_IDMAPD" = no ] || exit 0
97 [ -x /usr/sbin/rpc.gssd     ] || [ "$NEED_GSSD"   = no ] || exit 0
98
99 do_modprobe() {
100     modprobe -q "$1" || true
101 }
102
103 do_mount() {
104     if ! grep -E -qs "$1\$" /proc/filesystems
105     then
106         return 1
107     fi
108     if ! mountpoint -q "$2"
109     then
110         mount -t "$1" "$1" "$2"
111         return
112     fi
113     return 0
114 }
115
116 # See how we were called.
117 case "$1" in
118   start)
119         cd /    # daemons should have root dir as cwd
120         printf "Starting $DESC:"
121         printf " statd"
122         start-stop-daemon --start --quiet \
123             --exec $PREFIX/sbin/rpc.statd -- $STATDOPTS
124         if [ "$NEED_LOCKD" = yes ]
125         then
126             printf " lockd"
127             start-stop-daemon --start --quiet \
128                 --exec $PREFIX/sbin/rpc.lockd  || true
129         fi
130         if [ "$NEED_IDMAPD" = yes ] || [ "$NEED_GSSD" = yes ]
131         then
132             do_modprobe nfs
133             do_modprobe nfs4
134             if do_mount rpc_pipefs $PIPEFS_MOUNTPOINT
135             then
136                 if [ "$NEED_IDMAPD" = yes ]
137                 then
138                     printf " idmapd"
139                     start-stop-daemon --start --quiet \
140                             --make-pidfile --pidfile $IDMAPD_PIDFILE \
141                             --exec /usr/sbin/rpc.idmapd
142                 fi
143                 if [ "$NEED_GSSD" = yes ]
144                 then
145                     do_modprobe rpcsec_gss_krb5
146                     printf " gssd"
147                     start-stop-daemon --start --quiet \
148                             --make-pidfile --pidfile $GSSD_PIDFILE \
149                             --exec /usr/sbin/rpc.gssd -- $RPCGSSDOPTS
150                 fi
151             fi
152         fi
153         echo "."
154         ;;
155
156   stop)
157         printf "Stopping $DESC:"
158         if [ "$NEED_GSSD" = yes ]
159         then
160             printf " gssd"
161             start-stop-daemon --stop --oknodo --quiet \
162                     --name rpc.gssd
163             rm -f $GSSD_PIDFILE
164         fi
165         if [ "$NEED_IDMAPD" = yes ]
166         then
167             printf " idmapd"
168             start-stop-daemon --stop --oknodo --quiet \
169                 --name rpc.idmapd
170             rm -f $IDMAPD_PIDFILE
171         fi
172         if [ "$NEED_LOCKD" = yes ]
173         then
174             printf " lockd"
175             start-stop-daemon --stop --oknodo --quiet \
176                 --name rpc.lockd || true
177         else
178             pkill -KILL -u root -x lockd || true
179         fi
180         printf " statd"
181         start-stop-daemon --stop --oknodo --quiet \
182             --name rpc.statd
183         echo "."
184         ;;
185
186   restart | force-reload)
187         $0 stop
188         sleep 1
189         $0 start
190         ;;
191
192   *)
193         echo "Usage: nfs-common {start|stop|restart}"
194         exit 1
195         ;;
196 esac
197
198 exit 0