52913ae553f909d0a216115be74b0b624e6a2c48
[nfs-utils.git] / debian / nfs-kernel-server.init
1 #!/bin/sh
2
3 ### BEGIN INIT INFO
4 # Provides:          nfs-kernel-server
5 # Required-Start:    $portmap $time
6 # Default-Start:     2 3 4 5
7 # Default-Stop:      0 1 6
8 # Short-Description: Kernel NFS server support
9 # Description:       NFS is a popular protocol for file sharing across
10 #                    TCP/IP networks. This service provides NFS server
11 #                    functionality, which is configured via the
12 #                    /etc/exports file.
13 ### END INIT INFO
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 . /lib/lsb/init-functions
38
39 do_modprobe() {
40     if [ -x /sbin/modprobe -a -f /proc/modules ]
41     then
42         modprobe -q "$1" || true
43     fi
44 }
45
46 do_mount() {
47     if ! grep -E -qs "$1\$" /proc/filesystems
48     then
49         return 1
50     fi
51     if ! mountpoint -q "$2"
52     then
53         mount -t "$1" "$1" "$2"
54         return
55     fi
56     return 0
57 }
58
59 # See how we were called.
60 case "$1" in
61   start)
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 -qE 'init_nf(sd|     )' /proc/kallsyms; then
68                         log_warning_msg "Not starting $DESC: no support in current kernel."
69                         exit 0
70                 fi
71                 
72                 do_mount nfsd $PROCNFSD_MOUNTPOINT || NEED_SVCGSSD=no
73                 log_begin_msg "Exporting directories for $DESC..."
74                 $PREFIX/sbin/exportfs -r
75                 if [ $? != 0 ]; then
76                         log_end_msg $?
77                         exit $?
78                 fi
79                 log_end_msg 0
80
81                 log_daemon_msg "Starting $DESC"
82                 log_progress_msg "nfsd"
83                 start-stop-daemon --start --oknodo --quiet \
84                     --nicelevel $RPCNFSDPRIORITY \
85                     --exec $PREFIX/sbin/rpc.nfsd -- $RPCNFSDCOUNT
86                 if [ $? != 0 ]; then
87                         log_end_msg $?
88                         exit $?
89                 fi
90
91                 # make sure 127.0.0.1 is a valid source for requests
92                 ClearAddr=
93                 if [ -f /proc/net/rpc/auth.unix.ip/channel ]
94                 then
95                     fgrep -qs 127.0.0.1 /proc/net/rpc/auth.unix.ip/content || {
96                         echo "nfsd 127.0.0.1 2147483647 localhost" >/proc/net/rpc/auth.unix.ip/channel
97                         ClearAddr=yes
98                     }
99                 fi
100
101                 $PREFIX/bin/rpcinfo -u localhost nfs 3 >/dev/null 2>&1 ||
102                     RPCMOUNTDOPTS="$RPCMOUNTDOPTS --no-nfs-version 3"
103
104                 [ -z "$ClearAddr" ] || echo "nfsd 127.0.0.1 1" >/proc/net/rpc/auth.unix.ip/channel
105
106                 log_progress_msg "mountd"
107                 start-stop-daemon --start --oknodo --quiet \
108                     --exec $PREFIX/sbin/rpc.mountd -- $RPCMOUNTDOPTS
109                 if [ $? != 0 ]; then
110                         log_end_msg $?
111                         exit $?
112                 fi
113
114                 log_end_msg 0
115         else
116                 log_warning_msg "Not starting $DESC: no exports."
117         fi
118         ;;
119
120   stop)
121         log_daemon_msg "Stopping $DESC"
122         
123         log_progress_msg "mountd"
124         start-stop-daemon --stop --oknodo --quiet \
125             --name rpc.mountd --user 0
126         if [ $? != 0 ]; then
127                 log_end_msg $?
128                 exit $?
129         fi
130
131         log_progress_msg "nfsd"
132         start-stop-daemon --stop --oknodo --quiet \
133             --name nfsd --user 0 --signal 2
134         if [ $? != 0 ]; then
135                 log_end_msg $?
136                 exit $?
137         fi
138
139         log_end_msg 0
140
141         log_begin_msg "Unexporting directories for $DESC..."
142         $PREFIX/sbin/exportfs -au
143         if [ $? != 0 ]; then
144                 log_end_msg $?
145                 exit $?
146         fi
147         log_end_msg 0
148
149         if mountpoint -q /proc/nfs/nfsd
150         then
151             $PREFIX/sbin/exportfs -f
152         fi
153         ;;
154
155   status)
156         if pidof nfsd >/dev/null
157         then
158             echo "nfsd running"
159             exit 0
160         else
161             echo "nfsd not running"
162             exit 3
163         fi
164         ;;
165
166   reload | force-reload)
167         log_begin_msg "Re-exporting directories for $DESC..."
168         $PREFIX/sbin/exportfs -r
169         log_end_msg $?
170         exit $?
171         ;;
172
173   restart)
174         $0 stop
175         sleep 1
176         $0 start
177         ;;
178
179   *)
180         log_success_msg "Usage: nfs-kernel-server {start|stop|status|reload|force-reload|restart}"
181         exit 1
182         ;;
183 esac
184
185 exit 0