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