xref: /illumos-gate/usr/src/cmd/fs.d/nfs/svc/nfs-server (revision 6927f468b0af7710df000f6b16f6ee413e1e3007)
17c478bd9Sstevel@tonic-gate#!/sbin/sh
27c478bd9Sstevel@tonic-gate#
37c478bd9Sstevel@tonic-gate# CDDL HEADER START
47c478bd9Sstevel@tonic-gate#
57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*6927f468Sdp# Common Development and Distribution License (the "License").
7*6927f468Sdp# You may not use this file except in compliance with the License.
87c478bd9Sstevel@tonic-gate#
97c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate# and limitations under the License.
137c478bd9Sstevel@tonic-gate#
147c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate#
207c478bd9Sstevel@tonic-gate# CDDL HEADER END
217c478bd9Sstevel@tonic-gate#
227c478bd9Sstevel@tonic-gate#
237f7322feSeschrock# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate# Use is subject to license terms.
257c478bd9Sstevel@tonic-gate#
267c478bd9Sstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate# Start/stop processes required for server NFS
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate. /lib/svc/share/smf_include.sh
31*6927f468Sdpzone=`smf_zonename`
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gatecase "$1" in
347c478bd9Sstevel@tonic-gate'start')
357c478bd9Sstevel@tonic-gate	# The NFS server is not supported in a local zone
36*6927f468Sdp	if smf_is_nonglobalzone; then
373fd3a04aSthurlow		/usr/sbin/svcadm disable -t svc:/network/nfs/server
387c478bd9Sstevel@tonic-gate		echo "The NFS server is not supported in a local zone"
397c478bd9Sstevel@tonic-gate		sleep 5 &
407c478bd9Sstevel@tonic-gate		exit $SMF_EXIT_OK
417c478bd9Sstevel@tonic-gate	fi
427c478bd9Sstevel@tonic-gate
437f7322feSeschrock	# Share any ZFS filesystems marked for sharing.  The environment
447f7322feSeschrock	# variable prevents share(1M) from checking whether each filesystem
457f7322feSeschrock	# is in use, an O(n^2) operation with no purpose at this time
467f7322feSeschrock
477f7322feSeschrock	if [ -x /usr/sbin/zfs ]; then
487f7322feSeschrock		SHARE_NOINUSE_CHECK=1; export SHARE_NOINUSE_CHECK
497f7322feSeschrock		/usr/sbin/zfs share -a
507f7322feSeschrock		unset SHARE_NOINUSE_CHECK
517f7322feSeschrock	fi
527f7322feSeschrock
537c478bd9Sstevel@tonic-gate	# If /etc/dfs/dfstab exists and has non-blank or non-commented-out
54fa9e4066Sahrens	# lines, then run shareall to export them.
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate	startnfsd=0
577c478bd9Sstevel@tonic-gate	if [ -f /etc/dfs/dfstab ] && /usr/bin/egrep -v '^[	 ]*(#|$)' \
587c478bd9Sstevel@tonic-gate	    /etc/dfs/dfstab >/dev/null 2>&1; then
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate		/usr/sbin/shareall -F nfs
617c478bd9Sstevel@tonic-gate	fi
627c478bd9Sstevel@tonic-gate
63fa9e4066Sahrens
64fa9e4066Sahrens	# Start up mountd and nfsd if anything is exported.
65fa9e4066Sahrens
667c478bd9Sstevel@tonic-gate	if /usr/bin/grep -s nfs /etc/dfs/sharetab >/dev/null; then
677c478bd9Sstevel@tonic-gate		startnfsd=1
687c478bd9Sstevel@tonic-gate	fi
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate	# When the system comes up umask is not set; so set the mode now
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate	[ -f /etc/dfs/sharetab ] && /usr/bin/chmod 644 /etc/dfs/sharetab
737c478bd9Sstevel@tonic-gate
747c478bd9Sstevel@tonic-gate	# Start nfslogd if /etc/nfs/nfslogtab exists and is not empty
757c478bd9Sstevel@tonic-gate	# This means that either we just shared new filesystems with
767c478bd9Sstevel@tonic-gate	# logging enabled, or they were shared in the previous session
777c478bd9Sstevel@tonic-gate	# and their working buffers haven't been processed yet.
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate	if [ -s /etc/nfs/nfslogtab ]; then
807c478bd9Sstevel@tonic-gate		/usr/lib/nfs/nfslogd &
817c478bd9Sstevel@tonic-gate	fi
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate	# Options for nfsd are now set in /etc/default/nfs
847c478bd9Sstevel@tonic-gate	if [ $startnfsd -ne 0 ]; then
857c478bd9Sstevel@tonic-gate		/usr/lib/nfs/mountd
867c478bd9Sstevel@tonic-gate		/usr/lib/nfs/nfsd
877c478bd9Sstevel@tonic-gate	else
883fd3a04aSthurlow		/usr/sbin/svcadm disable -t svc:/network/nfs/server
897c478bd9Sstevel@tonic-gate		echo "No NFS filesystems are shared"
907c478bd9Sstevel@tonic-gate		sleep 5 &
917c478bd9Sstevel@tonic-gate	fi
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate	;;
947c478bd9Sstevel@tonic-gate
953fd3a04aSthurlow'refresh')
963fd3a04aSthurlow	/usr/sbin/shareall -F nfs
973fd3a04aSthurlow	;;
983fd3a04aSthurlow
997c478bd9Sstevel@tonic-gate'stop')
1007c478bd9Sstevel@tonic-gate	/usr/bin/pkill -x -u 0,1 -z $zone '(nfsd|mountd)'
1017c478bd9Sstevel@tonic-gate
102fa9e4066Sahrens	# Unshare shared ZFS filesystems.
103fa9e4066Sahrens
104fa9e4066Sahrens	if [ -x /usr/sbin/zfs ]; then
105fa9e4066Sahrens		/usr/sbin/zfs unshare -a
106fa9e4066Sahrens	fi
107fa9e4066Sahrens
108fa9e4066Sahrens	# Unshare remaining shared filesystems.
109fa9e4066Sahrens
1107c478bd9Sstevel@tonic-gate	if /usr/bin/grep -s nfs /etc/dfs/sharetab >/dev/null; then
1117c478bd9Sstevel@tonic-gate		/usr/sbin/unshareall -F nfs
1127c478bd9Sstevel@tonic-gate	fi
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate	#
1157c478bd9Sstevel@tonic-gate	# Wait up to 10 seconds for nfslogd to gracefully handle SIGHUP
1167c478bd9Sstevel@tonic-gate	#
1177c478bd9Sstevel@tonic-gate	/usr/bin/pkill -HUP -x -u 0 -z $zone nfslogd
1187c478bd9Sstevel@tonic-gate	wtime=10
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate	while [ $wtime -gt 0 ]; do
1217c478bd9Sstevel@tonic-gate		/usr/bin/pgrep -x -u 0 -z $zone nfslogd >/dev/null || break
1227c478bd9Sstevel@tonic-gate		wtime=`expr $wtime - 1`
1237c478bd9Sstevel@tonic-gate		sleep 1
1247c478bd9Sstevel@tonic-gate	done
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate	#
1277c478bd9Sstevel@tonic-gate	# Kill nfslogd more forcefully if it did not shutdown during
1287c478bd9Sstevel@tonic-gate	# the grace period
1297c478bd9Sstevel@tonic-gate	#
1307c478bd9Sstevel@tonic-gate	if [ $wtime -eq 0 ]; then
1317c478bd9Sstevel@tonic-gate		/usr/bin/pkill -TERM -x -u 0 -z $zone nfslogd
1327c478bd9Sstevel@tonic-gate	fi
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate	# Kill any processes left in service contract
1357c478bd9Sstevel@tonic-gate	smf_kill_contract $2 TERM 1
1367c478bd9Sstevel@tonic-gate	[ $? -ne 0 ] && exit 1
1377c478bd9Sstevel@tonic-gate	;;
1383fd3a04aSthurlow
1397c478bd9Sstevel@tonic-gate*)
1403fd3a04aSthurlow	echo "Usage: $0 { start | stop | refresh }"
1417c478bd9Sstevel@tonic-gate	exit 1
1427c478bd9Sstevel@tonic-gate	;;
1437c478bd9Sstevel@tonic-gateesac
1447c478bd9Sstevel@tonic-gateexit $SMF_EXIT_OK
145