xref: /illumos-gate/usr/src/cmd/fs.d/nfs/svc/nfs-server (revision 43d18f1c320355e93c47399bea0b2e022fe06364)
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 2005 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	# If /etc/dfs/dfstab exists and has non-blank or non-commented-out
45	# lines, then run shareall to export them.
46
47	startnfsd=0
48	if [ -f /etc/dfs/dfstab ] && /usr/bin/egrep -v '^[	 ]*(#|$)' \
49	    /etc/dfs/dfstab >/dev/null 2>&1; then
50
51		/usr/sbin/shareall -F nfs
52	fi
53
54	# Share any ZFS filesystems marked for sharing.
55
56	if [ -x /usr/sbin/zfs ]; then
57		/usr/sbin/zfs share -a
58	fi
59
60	# Start up mountd and nfsd if anything is exported.
61
62	if /usr/bin/grep -s nfs /etc/dfs/sharetab >/dev/null; then
63		startnfsd=1
64	fi
65
66	# When the system comes up umask is not set; so set the mode now
67
68	[ -f /etc/dfs/sharetab ] && /usr/bin/chmod 644 /etc/dfs/sharetab
69
70	# Start nfslogd if /etc/nfs/nfslogtab exists and is not empty
71	# This means that either we just shared new filesystems with
72	# logging enabled, or they were shared in the previous session
73	# and their working buffers haven't been processed yet.
74
75	if [ -s /etc/nfs/nfslogtab ]; then
76		/usr/lib/nfs/nfslogd &
77	fi
78
79	# Options for nfsd are now set in /etc/default/nfs
80	if [ $startnfsd -ne 0 ]; then
81		/usr/lib/nfs/mountd
82		/usr/lib/nfs/nfsd
83	else
84		/usr/sbin/svcadm disable -t svc:/network/nfs/server
85		echo "No NFS filesystems are shared"
86		sleep 5 &
87	fi
88
89	;;
90
91'refresh')
92	/usr/sbin/shareall -F nfs
93	;;
94
95'stop')
96	/usr/bin/pkill -x -u 0,1 -z $zone '(nfsd|mountd)'
97
98	# Unshare shared ZFS filesystems.
99
100	if [ -x /usr/sbin/zfs ]; then
101		/usr/sbin/zfs unshare -a
102	fi
103
104	# Unshare remaining shared filesystems.
105
106	if /usr/bin/grep -s nfs /etc/dfs/sharetab >/dev/null; then
107		/usr/sbin/unshareall -F nfs
108	fi
109
110	#
111	# Wait up to 10 seconds for nfslogd to gracefully handle SIGHUP
112	#
113	/usr/bin/pkill -HUP -x -u 0 -z $zone nfslogd
114	wtime=10
115
116	while [ $wtime -gt 0 ]; do
117		/usr/bin/pgrep -x -u 0 -z $zone nfslogd >/dev/null || break
118		wtime=`expr $wtime - 1`
119		sleep 1
120	done
121
122	#
123	# Kill nfslogd more forcefully if it did not shutdown during
124	# the grace period
125	#
126	if [ $wtime -eq 0 ]; then
127		/usr/bin/pkill -TERM -x -u 0 -z $zone nfslogd
128	fi
129
130	# Kill any processes left in service contract
131	smf_kill_contract $2 TERM 1
132	[ $? -ne 0 ] && exit 1
133	;;
134
135*)
136	echo "Usage: $0 { start | stop | refresh }"
137	exit 1
138	;;
139esac
140exit $SMF_EXIT_OK
141