xref: /illumos-gate/usr/src/cmd/fs.d/nfs/svc/nfs-server (revision 65a89a64c60f3061bbe2381edaacc81660af9a95)
1#!/sbin/sh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License, Version 1.0 only
7# (the "License").  You may not use this file except in compliance
8# with the License.
9#
10# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11# or http://www.opensolaris.org/os/licensing.
12# See the License for the specific language governing permissions
13# and limitations under the License.
14#
15# When distributing Covered Code, include this CDDL HEADER in each
16# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17# If applicable, add the following below this CDDL HEADER, with the
18# fields enclosed by brackets "[]" replaced with your own identifying
19# information: Portions Copyright [yyyy] [name of copyright owner]
20#
21# CDDL HEADER END
22#
23#
24# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29# Start/stop processes required for server NFS
30
31. /lib/svc/share/smf_include.sh
32zone=${_INIT_ZONENAME:=`/sbin/zonename`}
33
34case "$1" in
35'start')
36	# The NFS server is not supported in a local zone
37	if [ $zone != "global" ] ; then
38		/usr/sbin/svcadm disable -t svc:/network/nfs/server
39		echo "The NFS server is not supported in a local zone"
40		sleep 5 &
41		exit $SMF_EXIT_OK
42	fi
43
44	# Share any ZFS filesystems marked for sharing.  The environment
45	# variable prevents share(1M) from checking whether each filesystem
46	# is in use, an O(n^2) operation with no purpose at this time
47
48	if [ -x /usr/sbin/zfs ]; then
49		SHARE_NOINUSE_CHECK=1; export SHARE_NOINUSE_CHECK
50		/usr/sbin/zfs share -a
51		unset SHARE_NOINUSE_CHECK
52	fi
53
54	# If /etc/dfs/dfstab exists and has non-blank or non-commented-out
55	# lines, then run shareall to export them.
56
57	startnfsd=0
58	if [ -f /etc/dfs/dfstab ] && /usr/bin/egrep -v '^[	 ]*(#|$)' \
59	    /etc/dfs/dfstab >/dev/null 2>&1; then
60
61		/usr/sbin/shareall -F nfs
62	fi
63
64
65	# Start up mountd and nfsd if anything is exported.
66
67	if /usr/bin/grep -s nfs /etc/dfs/sharetab >/dev/null; then
68		startnfsd=1
69	fi
70
71	# When the system comes up umask is not set; so set the mode now
72
73	[ -f /etc/dfs/sharetab ] && /usr/bin/chmod 644 /etc/dfs/sharetab
74
75	# Start nfslogd if /etc/nfs/nfslogtab exists and is not empty
76	# This means that either we just shared new filesystems with
77	# logging enabled, or they were shared in the previous session
78	# and their working buffers haven't been processed yet.
79
80	if [ -s /etc/nfs/nfslogtab ]; then
81		/usr/lib/nfs/nfslogd &
82	fi
83
84	# Options for nfsd are now set in /etc/default/nfs
85	if [ $startnfsd -ne 0 ]; then
86		/usr/lib/nfs/mountd
87		/usr/lib/nfs/nfsd
88	else
89		/usr/sbin/svcadm disable -t svc:/network/nfs/server
90		echo "No NFS filesystems are shared"
91		sleep 5 &
92	fi
93
94	;;
95
96'refresh')
97	/usr/sbin/shareall -F nfs
98	;;
99
100'stop')
101	/usr/bin/pkill -x -u 0,1 -z $zone '(nfsd|mountd)'
102
103	# Unshare shared ZFS filesystems.
104
105	if [ -x /usr/sbin/zfs ]; then
106		/usr/sbin/zfs unshare -a
107	fi
108
109	# Unshare remaining shared filesystems.
110
111	if /usr/bin/grep -s nfs /etc/dfs/sharetab >/dev/null; then
112		/usr/sbin/unshareall -F nfs
113	fi
114
115	#
116	# Wait up to 10 seconds for nfslogd to gracefully handle SIGHUP
117	#
118	/usr/bin/pkill -HUP -x -u 0 -z $zone nfslogd
119	wtime=10
120
121	while [ $wtime -gt 0 ]; do
122		/usr/bin/pgrep -x -u 0 -z $zone nfslogd >/dev/null || break
123		wtime=`expr $wtime - 1`
124		sleep 1
125	done
126
127	#
128	# Kill nfslogd more forcefully if it did not shutdown during
129	# the grace period
130	#
131	if [ $wtime -eq 0 ]; then
132		/usr/bin/pkill -TERM -x -u 0 -z $zone nfslogd
133	fi
134
135	# Kill any processes left in service contract
136	smf_kill_contract $2 TERM 1
137	[ $? -ne 0 ] && exit 1
138	;;
139
140*)
141	echo "Usage: $0 { start | stop | refresh }"
142	exit 1
143	;;
144esac
145exit $SMF_EXIT_OK
146