Imported Debian patch 1.0.8+1.0.9pre1-1
[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 # What is this?
14 DESC="NFS kernel daemon"
15 PREFIX=/usr
16
17 # Exit if required binaries are missing.
18 [ -x $PREFIX/sbin/rpc.nfsd    ] || exit 0
19 [ -x $PREFIX/sbin/rpc.mountd  ] || exit 0
20 [ -x $PREFIX/sbin/exportfs    ] || exit 0
21
22 # Read config
23 DEFAULTFILE=/etc/default/nfs-kernel-server
24 RPCNFSDCOUNT=8
25 RPCNFSDPRIORITY=0
26 RPCMOUNTDOPTS=
27 NEED_SVCGSSD=yes
28 RPCGSSDOPTS=
29 RPCSVCGSSDOPTS=
30 PROCNFSD_MOUNTPOINT=/proc/fs/nfsd
31 if [ -f $DEFAULTFILE ]; then
32     . $DEFAULTFILE
33 fi
34
35 . /lib/lsb/init-functions
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         if [ -f /etc/exports ]
61         then
62                 do_modprobe nfsd
63
64                 # See if our running kernel supports the NFS kernel server
65                 if [ -f /proc/kallsyms ] && ! grep -qE 'init_nf(sd|     )' /proc/kallsyms; then
66                         log_warning_msg "Not starting $DESC: no support in current kernel."
67                         exit 0
68                 fi
69                 
70                 do_mount nfsd $PROCNFSD_MOUNTPOINT || NEED_SVCGSSD=no
71                 log_begin_msg "Exporting directories for $DESC..."
72                 $PREFIX/sbin/exportfs -r
73                 if [ $? != 0 ]; then
74                         log_end_msg $?
75                         exit $?
76                 fi
77                 log_end_msg 0
78
79                 log_daemon_msg "Starting $DESC"
80                 log_progress_msg "nfsd"
81                 start-stop-daemon --start --oknodo --quiet \
82                     --nicelevel $RPCNFSDPRIORITY \
83                     --exec $PREFIX/sbin/rpc.nfsd -- $RPCNFSDCOUNT
84                 if [ $? != 0 ]; then
85                         log_end_msg $?
86                         exit $?
87                 fi
88
89                 # make sure 127.0.0.1 is a valid source for requests
90                 ClearAddr=
91                 if [ -f /proc/net/rpc/auth.unix.ip/channel ]
92                 then
93                     fgrep -qs 127.0.0.1 /proc/net/rpc/auth.unix.ip/content || {
94                         echo "nfsd 127.0.0.1 2147483647 localhost" >/proc/net/rpc/auth.unix.ip/channel
95                         ClearAddr=yes
96                     }
97                 fi
98
99                 $PREFIX/bin/rpcinfo -u localhost nfs 3 >/dev/null 2>&1 ||
100                     RPCMOUNTDOPTS="$RPCMOUNTDOPTS --no-nfs-version 3"
101
102                 [ -z "$ClearAddr" ] || echo "nfsd 127.0.0.1 1" >/proc/net/rpc/auth.unix.ip/channel
103
104                 log_progress_msg "mountd"
105                 start-stop-daemon --start --oknodo --quiet \
106                     --exec $PREFIX/sbin/rpc.mountd -- $RPCMOUNTDOPTS
107                 if [ $? != 0 ]; then
108                         log_end_msg $?
109                         exit $?
110                 fi
111
112                 log_end_msg 0
113         else
114                 log_warning_msg "Not starting $DESC: no exports."
115         fi
116         ;;
117
118   stop)
119         log_daemon_msg "Stopping $DESC"
120         
121         log_progress_msg "mountd"
122         start-stop-daemon --stop --oknodo --quiet \
123             --name rpc.mountd --user 0
124         if [ $? != 0 ]; then
125                 log_end_msg $?
126                 exit $?
127         fi
128
129         log_progress_msg "nfsd"
130         start-stop-daemon --stop --oknodo --quiet \
131             --name nfsd --user 0 --signal 2
132         if [ $? != 0 ]; then
133                 log_end_msg $?
134                 exit $?
135         fi
136
137         log_end_msg 0
138
139         log_begin_msg "Unexporting directories for $DESC..."
140         $PREFIX/sbin/exportfs -au
141         if [ $? != 0 ]; then
142                 log_end_msg $?
143                 exit $?
144         fi
145         log_end_msg 0
146
147         if mountpoint -q /proc/nfs/nfsd
148         then
149             $PREFIX/sbin/exportfs -f
150         fi
151         ;;
152
153   status)
154         if pidof nfsd >/dev/null
155         then
156             echo "nfsd running"
157             exit 0
158         else
159             echo "nfsd not running"
160             exit 3
161         fi
162         ;;
163
164   reload | force-reload)
165         log_begin_msg "Re-exporting directories for $DESC..."
166         $PREFIX/sbin/exportfs -r
167         log_end_msg $?
168         exit $?
169         ;;
170
171   restart)
172         $0 stop
173         sleep 1
174         $0 start
175         ;;
176
177   *)
178         log_success_msg "Usage: nfs-kernel-server {start|stop|status|reload|force-reload|restart}"
179         exit 1
180         ;;
181 esac
182
183 exit 0