e80026777b279646d84654fd3bfa06f3bb3caf62
[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                 # Having the 'nfsd' filesystem mounted (if available)
39                 # make client authentication more reliable.
40                 if [ -d /proc/fs/nfsd -a ! -f /proc/fs/nfsd/exports ]
41                 then mount -t nfsd nfsd /proc/fs/nfsd
42                 fi
43                 printf "Exporting directories for $DESC..."
44                 $PREFIX/sbin/exportfs -r
45                 echo "done."
46
47                 printf "Starting $DESC:"
48                 printf " nfsd"
49                 start-stop-daemon --start --quiet \
50                     --exec $PREFIX/sbin/rpc.nfsd -- $RPCNFSDCOUNT
51
52                 printf " mountd"
53
54                 # make sure 127.0.0.1 is a valid source for requests
55                 ClearAddr=
56                 if [ -f /proc/net/rpc/auth.unix.ip/channel ]
57                 then
58                     fgrep -qs 127.0.0.1 /proc/net/rpc/auth.unix.ip/content || {
59                         echo "nfsd 127.0.0.1 2147483647 localhost" >/proc/net/rpc/auth.unix.ip/channel
60                         ClearAddr=yes
61                     }
62                 fi
63
64                 $PREFIX/bin/rpcinfo -u localhost nfs 3 >/dev/null 2>&1 ||
65                     RPCMOUNTDOPTS="$RPCMOUNTDOPTS --no-nfs-version 3"
66
67                 [ -z "$ClearAddr" ] || echo "nfsd 127.0.0.1 1" >/proc/net/rpc/auth.unix.ip/channel
68
69                 start-stop-daemon --start --quiet \
70                     --exec $PREFIX/sbin/rpc.mountd -- $RPCMOUNTDOPTS
71                 echo "."
72         else
73                 echo "Not starting $DESC: No exports."
74         fi
75         ;;
76
77   stop)
78         printf "Stopping $DESC: mountd"
79         start-stop-daemon --stop --oknodo --quiet \
80             --name rpc.mountd --user 0
81         printf " nfsd"
82         start-stop-daemon --stop --oknodo --quiet \
83             --name nfsd --user 0 --signal 2
84         echo "."
85
86         printf "Unexporting directories for $DESC..."
87         $PREFIX/sbin/exportfs -au
88         if [ -f /proc/fs/nfsd/exports ] ; then umount /proc/fs/nfsd; fi
89         echo "done."
90         ;;
91
92   reload | force-reload)
93         printf "Re-exporting directories for $DESC..."
94         $PREFIX/sbin/exportfs -r
95         echo "done."
96         ;;
97
98   restart)
99         $0 stop
100         sleep 1
101         $0 start
102         ;;
103
104   *)
105         echo "Usage: nfs-kernel-server {start|stop|reload|force-reload|restart}"
106         exit 1
107         ;;
108 esac
109
110 exit 0