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