Imported Debian patch 1.0.10-2
[nfs-utils.git] / debian / nfs-kernel-server.init
1 #!/bin/sh
2
3 ### BEGIN INIT INFO
4 # Provides:          nfs-kernel-server
5 # Required-Start:    $portmap $time
6 # Required-Stop:     $portmap $time
7 # Default-Start:     2 3 4 5
8 # Default-Stop:      0 1 6
9 # Short-Description: Kernel NFS server support
10 # Description:       NFS is a popular protocol for file sharing across
11 #                    TCP/IP networks. This service provides NFS server
12 #                    functionality, which is configured via the
13 #                    /etc/exports file.
14 ### END INIT INFO
15
16 # What is this?
17 DESC="NFS kernel daemon"
18 PREFIX=/usr
19
20 # Exit if required binaries are missing.
21 [ -x $PREFIX/sbin/rpc.nfsd    ] || exit 0
22 [ -x $PREFIX/sbin/rpc.mountd  ] || exit 0
23 [ -x $PREFIX/sbin/exportfs    ] || exit 0
24
25 # Read config
26 DEFAULTFILE=/etc/default/nfs-kernel-server
27 RPCNFSDCOUNT=8
28 RPCNFSDPRIORITY=0
29 RPCMOUNTDOPTS=
30 NEED_SVCGSSD=no
31 RPCSVCGSSDOPTS=
32 PROCNFSD_MOUNTPOINT=/proc/fs/nfsd
33 if [ -f $DEFAULTFILE ]; then
34     . $DEFAULTFILE
35 fi
36
37 . /lib/lsb/init-functions
38
39 do_modprobe() {
40     if [ -x /sbin/modprobe -a -f /proc/modules ]
41     then
42         modprobe -q "$1" || true
43     fi
44 }
45
46 do_mount() {
47     if ! grep -E -qs "$1\$" /proc/filesystems
48     then
49         return 1
50     fi
51     if ! mountpoint -q "$2"
52     then
53         mount -t "$1" "$1" "$2"
54         return
55     fi
56     return 0
57 }
58
59 # See how we were called.
60 case "$1" in
61   start)
62         if [ -f /etc/exports ]
63         then
64                 do_modprobe nfsd
65
66                 # See if our running kernel supports the NFS kernel server
67                 if [ -f /proc/kallsyms ] && ! grep -qE 'init_nf(sd|     )' /proc/kallsyms; then
68                         log_warning_msg "Not starting $DESC: no support in current kernel."
69                         exit 0
70                 fi
71                 
72                 do_mount nfsd $PROCNFSD_MOUNTPOINT || NEED_SVCGSSD=no
73                 log_begin_msg "Exporting directories for $DESC..."
74                 $PREFIX/sbin/exportfs -r
75                 if [ $? != 0 ]; then
76                         log_end_msg $?
77                         exit $?
78                 fi
79                 log_end_msg 0
80
81                 log_daemon_msg "Starting $DESC"
82                 log_progress_msg "nfsd"
83                 start-stop-daemon --start --oknodo --quiet \
84                     --nicelevel $RPCNFSDPRIORITY \
85                     --exec $PREFIX/sbin/rpc.nfsd -- $RPCNFSDCOUNT
86                 if [ $? != 0 ]; then
87                         log_end_msg $?
88                         exit $?
89                 fi
90
91                 # make sure 127.0.0.1 is a valid source for requests
92                 ClearAddr=
93                 if [ -f /proc/net/rpc/auth.unix.ip/channel ]
94                 then
95                     fgrep -qs 127.0.0.1 /proc/net/rpc/auth.unix.ip/content || {
96                         echo "nfsd 127.0.0.1 2147483647 localhost" >/proc/net/rpc/auth.unix.ip/channel
97                         ClearAddr=yes
98                     }
99                 fi
100
101                 $PREFIX/bin/rpcinfo -u localhost nfs 3 >/dev/null 2>&1 ||
102                     RPCMOUNTDOPTS="$RPCMOUNTDOPTS --no-nfs-version 3"
103
104                 [ -z "$ClearAddr" ] || echo "nfsd 127.0.0.1 1" >/proc/net/rpc/auth.unix.ip/channel
105         
106                 if [ "$NEED_SVCGSSD" = "yes" ]; then
107                         log_progress_msg "svcgssd"
108                         start-stop-daemon --start --oknodo --quiet \
109                             --exec $PREFIX/sbin/rpc.svcgssd -- $RPCSVCGSSDOPTS
110                         if [ $? != 0 ]; then
111                                 log_end_msg $?
112                                 exit $?
113                         fi
114                 fi
115
116                 log_progress_msg "mountd"
117                 start-stop-daemon --start --oknodo --quiet \
118                     --exec $PREFIX/sbin/rpc.mountd -- $RPCMOUNTDOPTS
119                 if [ $? != 0 ]; then
120                         log_end_msg $?
121                         exit $?
122                 fi
123
124                 log_end_msg 0
125         else
126                 log_warning_msg "Not starting $DESC: no exports."
127         fi
128         ;;
129
130   stop)
131         log_daemon_msg "Stopping $DESC"
132         
133         log_progress_msg "mountd"
134         start-stop-daemon --stop --oknodo --quiet \
135             --name rpc.mountd --user 0
136         if [ $? != 0 ]; then
137                 log_end_msg $?
138                 exit $?
139         fi
140                 
141         if [ "$NEED_SVCGSSD" = "yes" ]; then
142                 log_progress_msg "svcgssd"
143                 start-stop-daemon --stop --oknodo --quiet \
144                     --name rpc.svcgssd --user 0
145                 if [ $? != 0 ]; then
146                         log_end_msg $?
147                         exit $?
148                 fi
149         fi
150
151         log_progress_msg "nfsd"
152         start-stop-daemon --stop --oknodo --quiet \
153             --name nfsd --user 0 --signal 2
154         if [ $? != 0 ]; then
155                 log_end_msg $?
156                 exit $?
157         fi
158
159         log_end_msg 0
160
161         log_begin_msg "Unexporting directories for $DESC..."
162         $PREFIX/sbin/exportfs -au
163         if [ $? != 0 ]; then
164                 log_end_msg $?
165                 exit $?
166         fi
167         log_end_msg 0
168
169         if mountpoint -q /proc/nfs/nfsd
170         then
171             $PREFIX/sbin/exportfs -f
172         fi
173         ;;
174
175   status)
176         if pidof nfsd >/dev/null
177         then
178             echo "nfsd running"
179             exit 0
180         else
181             echo "nfsd not running"
182             exit 3
183         fi
184         ;;
185
186   reload | force-reload)
187         log_begin_msg "Re-exporting directories for $DESC..."
188         $PREFIX/sbin/exportfs -r
189         log_end_msg $?
190         exit $?
191         ;;
192
193   restart)
194         $0 stop
195         sleep 1
196         $0 start
197         ;;
198
199   *)
200         log_success_msg "Usage: nfs-kernel-server {start|stop|status|reload|force-reload|restart}"
201         exit 1
202         ;;
203 esac
204
205 exit 0