Imported Debian patch 1.0.7-9
[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 [ -x $PREFIX/sbin/rpc.svcgssd ] || exit 0
24
25 # Read config
26 DEFAULTFILE=/etc/default/nfs-kernel-server
27 RPCNFSDCOUNT=8
28 RPCNFSDPRIORITY=0
29 RPCMOUNTDOPTS=
30 NEED_SVCGSSD=yes
31 RPCGSSDOPTS=
32 RPCSVCGSSDOPTS=
33 PROCNFSD_MOUNTPOINT=/proc/fs/nfsd
34 if [ -f $DEFAULTFILE ]; then
35     . $DEFAULTFILE
36 fi
37
38 do_modprobe() {
39     if [ -x /sbin/modprobe ]
40     then
41         modprobe -q "$1" || true
42     fi
43 }
44
45 do_mount() {
46     if ! grep -E -qs "$1\$" /proc/filesystems
47     then
48         return 1
49     fi
50     if ! mountpoint -q "$2"
51     then
52         mount -t "$1" "$1" "$2"
53         return
54     fi
55     return 0
56 }
57
58 # See how we were called.
59 case "$1" in
60   start)
61         cd /    # daemons should have root dir as cwd
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 -q init_nfsd /proc/kallsyms; then
68                         echo "Not starting $DESC: no support in current kernel."
69                         exit 0
70                 fi
71                 
72                 do_mount nfsd $PROCNFSD_MOUNTPOINT || NEED_SVCGSSD=no
73                 printf "Exporting directories for $DESC..."
74                 $PREFIX/sbin/exportfs -r
75                 echo "done."
76
77                 printf "Starting $DESC:"
78                 if [ "$NEED_SVCGSSD" = yes ]
79                 then
80                     printf " svcgssd"
81                     start-stop-daemon --start --quiet \
82                             --make-pidfile --pidfile /var/run/rpc.svcgssd.pid \
83                             --exec $PREFIX/sbin/rpc.svcgssd -- $RPCSVCGSSDOPTS
84                 fi
85
86                 printf " nfsd"
87                 start-stop-daemon --start --quiet \
88                     --nicelevel $RPCNFSDPRIORITY \
89                     --exec $PREFIX/sbin/rpc.nfsd -- $RPCNFSDCOUNT
90
91                 printf " mountd"
92
93                 # make sure 127.0.0.1 is a valid source for requests
94                 ClearAddr=
95                 if [ -f /proc/net/rpc/auth.unix.ip/channel ]
96                 then
97                     fgrep -qs 127.0.0.1 /proc/net/rpc/auth.unix.ip/content || {
98                         echo "nfsd 127.0.0.1 2147483647 localhost" >/proc/net/rpc/auth.unix.ip/channel
99                         ClearAddr=yes
100                     }
101                 fi
102
103                 $PREFIX/bin/rpcinfo -u localhost nfs 3 >/dev/null 2>&1 ||
104                     RPCMOUNTDOPTS="$RPCMOUNTDOPTS --no-nfs-version 3"
105
106                 [ -z "$ClearAddr" ] || echo "nfsd 127.0.0.1 1" >/proc/net/rpc/auth.unix.ip/channel
107
108                 start-stop-daemon --start --quiet \
109                     --exec $PREFIX/sbin/rpc.mountd -- $RPCMOUNTDOPTS
110                 echo "."
111         else
112                 echo "Not starting $DESC: No exports."
113         fi
114         ;;
115
116   stop)
117         printf "Stopping $DESC: mountd"
118         start-stop-daemon --stop --oknodo --quiet \
119             --name rpc.mountd --user 0
120         if [ "$NEED_SVCGSSD" = yes ]
121         then
122             printf " svcgssd"
123             start-stop-daemon --stop --oknodo --quiet \
124                     --name rpc.svcgssd --user 0
125             rm -f /var/run/rpc.svcgssd.pid
126         fi
127         printf " nfsd"
128         start-stop-daemon --stop --oknodo --quiet \
129             --name nfsd --user 0 --signal 2
130         echo "."
131
132         printf "Unexporting directories for $DESC..."
133         $PREFIX/sbin/exportfs -au
134         if mountpoint -q /proc/nfs/nfsd
135         then
136             $PREFIX/sbin/exportfs -f
137         fi
138         echo "done."
139         ;;
140
141   reload | force-reload)
142         printf "Re-exporting directories for $DESC..."
143         $PREFIX/sbin/exportfs -r
144         echo "done."
145         ;;
146
147   restart)
148         $0 stop
149         sleep 1
150         $0 start
151         ;;
152
153   *)
154         echo "Usage: nfs-kernel-server {start|stop|reload|force-reload|restart}"
155         exit 1
156         ;;
157 esac
158
159 exit 0