misc debian fixed from trond
[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 PROCNFSD_MOUNTPOINT=/proc/fs/nfsd
29 if [ -f $DEFAULTFILE ]; then
30     . $DEFAULTFILE
31 fi
32
33 do_modprobe() {
34         modprobe -q $1 || true
35 }
36
37 do_mount() {
38         if ! grep -E -qs "$1\$" /proc/filesystems
39         then
40                 return 1
41         fi
42         if ! mountpoint -q $2
43         then
44                 mount -t $1 $3 $1 $2
45                 return
46         fi
47         return 0
48 }
49
50 # See how we were called.
51 case "$1" in
52   start)
53         cd /    # daemons should have root dir as cwd
54         if grep -q '^/' /etc/exports
55         then
56                 do_modprobe nfsd
57                 do_mount nfsd $PROCNFSD_MOUNTPOINT || true
58                 printf "Exporting directories for $DESC..."
59                 $PREFIX/sbin/exportfs -r
60                 echo "done."
61
62                 printf "Starting $DESC:"
63                 printf " nfsd"
64                 start-stop-daemon --start --quiet \
65                     --exec $PREFIX/sbin/rpc.nfsd -- $RPCNFSDCOUNT
66
67                 printf " mountd"
68
69                 # make sure 127.0.0.1 is a valid source for requests
70                 ClearAddr=
71                 if [ -f /proc/net/rpc/auth.unix.ip/channel ]
72                 then
73                     fgrep -qs 127.0.0.1 /proc/net/rpc/auth.unix.ip/content || {
74                         echo "nfsd 127.0.0.1 2147483647 localhost" >/proc/net/rpc/auth.unix.ip/channel
75                         ClearAddr=yes
76                     }
77                 fi
78
79                 $PREFIX/bin/rpcinfo -u localhost nfs 3 >/dev/null 2>&1 ||
80                     RPCMOUNTDOPTS="$RPCMOUNTDOPTS --no-nfs-version 3"
81
82                 [ -z "$ClearAddr" ] || echo "nfsd 127.0.0.1 1" >/proc/net/rpc/auth.unix.ip/channel
83
84                 start-stop-daemon --start --quiet \
85                     --exec $PREFIX/sbin/rpc.mountd -- $RPCMOUNTDOPTS
86                 echo "."
87         else
88                 echo "Not starting $DESC: No exports."
89         fi
90         ;;
91
92   stop)
93         printf "Stopping $DESC: mountd"
94         start-stop-daemon --stop --oknodo --quiet \
95             --name rpc.mountd --user 0
96         printf " nfsd"
97         start-stop-daemon --stop --oknodo --quiet \
98             --name nfsd --user 0 --signal 2
99         echo "."
100
101         printf "Unexporting directories for $DESC..."
102         $PREFIX/sbin/exportfs -au
103         echo "done."
104         ;;
105
106   reload | force-reload)
107         printf "Re-exporting directories for $DESC..."
108         $PREFIX/sbin/exportfs -r
109         echo "done."
110         ;;
111
112   restart)
113         $0 stop
114         sleep 1
115         $0 start
116         ;;
117
118   *)
119         echo "Usage: nfs-kernel-server {start|stop|reload|force-reload|restart}"
120         exit 1
121         ;;
122 esac
123
124 exit 0