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