enable 127.0.0.1 before checking for V3 support
[nfs-utils.git] / debian / nfs-kernel-server.init
1 #!/bin/sh
2 #
3 # nfs-kernel-server
4 #               This shell script takes care of starting and stopping
5 #               the kernel-mode NFS server.
6 #
7 # chkconfig: 345 60 20
8 # description: NFS is a popular protocol for file sharing across TCP/IP \
9 #              networks. This service provides NFS server functionality, \
10 #              which is configured via the /etc/exports file.
11 #
12
13 set -e
14
15 # What is this?
16 DESC="NFS kernel daemon"
17 PREFIX=/usr
18
19 # Exit if required binaries are missing.
20 [ -x $PREFIX/sbin/rpc.nfsd ] || exit 0
21 [ -x $PREFIX/sbin/rpc.mountd ] || exit 0
22 [ -x $PREFIX/sbin/exportfs ] || exit 0
23
24 # Read config
25 DEFAULTFILE=/etc/default/nfs-kernel-server
26 RPCNFSDCOUNT=8
27 RPCMOUNTDOPTS=
28 if [ -f $DEFAULTFILE ]; then
29     . $DEFAULTFILE
30 fi
31
32 # See how we were called.
33 case "$1" in
34   start)
35         cd /    # daemons should have root dir as cwd
36         if grep -q '^/' /etc/exports
37         then
38                 printf "Exporting directories for $DESC..."
39                 $PREFIX/sbin/exportfs -r
40                 echo "done."
41
42                 printf "Starting $DESC:"
43                 printf " nfsd"
44                 start-stop-daemon --start --quiet \
45                     --exec $PREFIX/sbin/rpc.nfsd -- $RPCNFSDCOUNT
46                 printf " mountd"
47                 # make sure 127.0.0.1 is a valid source for requests
48                 ClearAddr=
49                 if [ -f /proc/net/rpc/auth.unix.ip/channel ] ; then
50                   if   grep -s 127.0.0.1 /proc/net/rpc/auth.unix.ip/content > /dev/null ; then
51                       : address already known
52                   else
53                       echo nfsd 127.0.0.1 2147483647 localhost > /proc/net/rpc/auth.unix.ip/channel
54                       ClearAddr=yes
55                   fi
56                 fi
57
58                 $PREFIX/bin/rpcinfo -u localhost nfs 3 >/dev/null 2>&1 ||
59                     RPCMOUNTDOPTS="$RPCMOUNTDOPTS --no-nfs-version 3"
60
61                 if [ -n "$ClearAddr" ]; then
62                         echo nfsd 127.0.0.1 1  > /proc/net/rpc/auth.unix.ip/channel
63                 fi
64
65                 start-stop-daemon --start --quiet \
66                     --exec $PREFIX/sbin/rpc.mountd -- $RPCMOUNTDOPTS
67                 echo "."
68         else
69                 echo "Not starting $DESC: No exports."
70         fi
71         ;;
72
73   stop)
74         printf "Stopping $DESC: mountd"
75         start-stop-daemon --stop --oknodo --quiet \
76             --name rpc.mountd --user 0
77         printf " nfsd"
78         start-stop-daemon --stop --oknodo --quiet \
79             --name nfsd --user 0 --signal 2
80         echo "."
81
82         printf "Unexporting directories for $DESC..."
83         $PREFIX/sbin/exportfs -au
84         echo "done."
85         ;;
86
87   reload | force-reload)
88         printf "Re-exporting directories for $DESC..."
89         $PREFIX/sbin/exportfs -r
90         echo "done."
91         ;;
92
93   restart)
94         $0 stop
95         sleep 1
96         $0 start
97         ;;
98
99   *)
100         echo "Usage: nfs-kernel-server {start|stop|reload|force-reload|restart}"
101         exit 1
102         ;;
103 esac
104
105 exit 0