81bd3f230a810fef8d9c001b15b014a93b0d438d
[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=no
30 RPCSVCGSSDOPTS=
31 PROCNFSD_MOUNTPOINT=/proc/fs/nfsd
32 if [ -f $DEFAULTFILE ]; then
33     . $DEFAULTFILE
34 fi
35
36 . /lib/lsb/init-functions
37
38 do_modprobe() {
39     if [ -x /sbin/modprobe -a -f /proc/modules ]
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         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                         log_warning_msg "Not starting $DESC: no support in current kernel."
68                         exit 0
69                 fi
70                 
71                 do_mount nfsd $PROCNFSD_MOUNTPOINT || NEED_SVCGSSD=no
72                 log_begin_msg "Exporting directories for $DESC..."
73                 $PREFIX/sbin/exportfs -r
74                 if [ $? != 0 ]; then
75                         log_end_msg $?
76                         exit $?
77                 fi
78                 log_end_msg 0
79
80                 log_daemon_msg "Starting $DESC"
81                 log_progress_msg "nfsd"
82                 start-stop-daemon --start --oknodo --quiet \
83                     --nicelevel $RPCNFSDPRIORITY \
84                     --exec $PREFIX/sbin/rpc.nfsd -- $RPCNFSDCOUNT
85                 if [ $? != 0 ]; then
86                         log_end_msg $?
87                         exit $?
88                 fi
89
90                 # make sure 127.0.0.1 is a valid source for requests
91                 ClearAddr=
92                 if [ -f /proc/net/rpc/auth.unix.ip/channel ]
93                 then
94                     fgrep -qs 127.0.0.1 /proc/net/rpc/auth.unix.ip/content || {
95                         echo "nfsd 127.0.0.1 2147483647 localhost" >/proc/net/rpc/auth.unix.ip/channel
96                         ClearAddr=yes
97                     }
98                 fi
99
100                 $PREFIX/bin/rpcinfo -u localhost nfs 3 >/dev/null 2>&1 ||
101                     RPCMOUNTDOPTS="$RPCMOUNTDOPTS --no-nfs-version 3"
102
103                 [ -z "$ClearAddr" ] || echo "nfsd 127.0.0.1 1" >/proc/net/rpc/auth.unix.ip/channel
104         
105                 if [ "$NEED_SVCGSSD" = "yes" ]; then
106                         log_progress_msg "svcgssd"
107                         start-stop-daemon --start --oknodo --quiet \
108                             --exec $PREFIX/sbin/rpc.svcgssd -- $RPCSVCGSSDOPTS
109                         if [ $? != 0 ]; then
110                                 log_end_msg $?
111                                 exit $?
112                         fi
113                 fi
114
115                 log_progress_msg "mountd"
116                 start-stop-daemon --start --oknodo --quiet \
117                     --exec $PREFIX/sbin/rpc.mountd -- $RPCMOUNTDOPTS
118                 if [ $? != 0 ]; then
119                         log_end_msg $?
120                         exit $?
121                 fi
122
123                 log_end_msg 0
124         else
125                 log_warning_msg "Not starting $DESC: no exports."
126         fi
127         ;;
128
129   stop)
130         log_daemon_msg "Stopping $DESC"
131         
132         log_progress_msg "mountd"
133         start-stop-daemon --stop --oknodo --quiet \
134             --name rpc.mountd --user 0
135         if [ $? != 0 ]; then
136                 log_end_msg $?
137                 exit $?
138         fi
139                 
140         if [ "$NEED_SVCGSSD" = "yes" ]; then
141                 log_progress_msg "svcgssd"
142                 start-stop-daemon --stop --oknodo --quiet \
143                     --name rpc.svcgssd --user 0
144                 if [ $? != 0 ]; then
145                         log_end_msg $?
146                         exit $?
147                 fi
148         fi
149
150         log_progress_msg "nfsd"
151         start-stop-daemon --stop --oknodo --quiet \
152             --name nfsd --user 0 --signal 2
153         if [ $? != 0 ]; then
154                 log_end_msg $?
155                 exit $?
156         fi
157
158         log_end_msg 0
159
160         log_begin_msg "Unexporting directories for $DESC..."
161         $PREFIX/sbin/exportfs -au
162         if [ $? != 0 ]; then
163                 log_end_msg $?
164                 exit $?
165         fi
166         log_end_msg 0
167
168         if mountpoint -q /proc/nfs/nfsd
169         then
170             $PREFIX/sbin/exportfs -f
171         fi
172         ;;
173
174   status)
175         if pidof nfsd >/dev/null
176         then
177             echo "nfsd running"
178             exit 0
179         else
180             echo "nfsd not running"
181             exit 3
182         fi
183         ;;
184
185   reload | force-reload)
186         log_begin_msg "Re-exporting directories for $DESC..."
187         $PREFIX/sbin/exportfs -r
188         log_end_msg $?
189         exit $?
190         ;;
191
192   restart)
193         $0 stop
194         sleep 1
195         $0 start
196         ;;
197
198   *)
199         log_success_msg "Usage: nfs-kernel-server {start|stop|status|reload|force-reload|restart}"
200         exit 1
201         ;;
202 esac
203
204 exit 0