xref: /onnv-gate/usr/src/cmd/zoneadm/svc-zones (revision 12613:4c5722bc28dc)
10Sstevel@tonic-gate#!/sbin/sh
20Sstevel@tonic-gate#
30Sstevel@tonic-gate# CDDL HEADER START
40Sstevel@tonic-gate#
50Sstevel@tonic-gate# The contents of this file are subject to the terms of the
62382Sdp# Common Development and Distribution License (the "License").
72382Sdp# You may not use this file except in compliance with the License.
80Sstevel@tonic-gate#
90Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate# See the License for the specific language governing permissions
120Sstevel@tonic-gate# and limitations under the License.
130Sstevel@tonic-gate#
140Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate#
200Sstevel@tonic-gate# CDDL HEADER END
210Sstevel@tonic-gate#
220Sstevel@tonic-gate#
23*12613SSurya.Prakki@Sun.COM# Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate
252622Sdp. /lib/svc/share/smf_include.sh
260Sstevel@tonic-gate
272712Snn35248#
282712Snn35248# Return a list of running, non-global zones for which a shutdown via
292712Snn35248# "/sbin/init 0" may work (typically only Solaris zones.)
302712Snn35248#
312712Snn35248shutdown_zones()
322712Snn35248{
332712Snn35248	zoneadm list -p | nawk -F: '{
34*12613SSurya.Prakki@Sun.COM		if ($2 != "global") {
352712Snn35248			print $2
362712Snn35248		}
372712Snn35248	}'
382712Snn35248}
392712Snn35248
400Sstevel@tonic-gate[ ! -x /usr/sbin/zoneadm ] && exit 0	# SUNWzoneu not installed
410Sstevel@tonic-gate
422622Sdpif [ -z "$SMF_FMRI" ]; then
432622Sdp	echo "this script can only be invoked by smf(5)"
442622Sdp	exit $SMF_EXIT_ERR_NOSMF
452622Sdpfi
462622Sdp
470Sstevel@tonic-gate# Make sure working directory is / to prevent unmounting problems.
480Sstevel@tonic-gatecd /
490Sstevel@tonic-gatePATH=/usr/sbin:/usr/bin; export PATH
500Sstevel@tonic-gate
510Sstevel@tonic-gatecase "$1" in
520Sstevel@tonic-gate'start')
530Sstevel@tonic-gate	egrep -vs '^#|^global:' /etc/zones/index || exit 0  # no local zones
5410718SJordan.Vaughan@Sun.com
5510718SJordan.Vaughan@Sun.com	#
5610718SJordan.Vaughan@Sun.com	# Boot the installed zones for which the "autoboot" zone property is
5710718SJordan.Vaughan@Sun.com	# set and invoke the sysboot hook for all other installed zones.
5810718SJordan.Vaughan@Sun.com	#
590Sstevel@tonic-gate	ZONES=""
600Sstevel@tonic-gate	for zone in `zoneadm list -pi | nawk -F: '{
610Sstevel@tonic-gate			if ($3 == "installed") {
620Sstevel@tonic-gate				print $2
630Sstevel@tonic-gate			}
640Sstevel@tonic-gate		}'`; do
650Sstevel@tonic-gate		zonecfg -z $zone info autoboot | grep "true" >/dev/null 2>&1
660Sstevel@tonic-gate		if [ $? -eq 0 ]; then
670Sstevel@tonic-gate			[ -z "$ZONES" ] && echo "Booting zones:\c"
680Sstevel@tonic-gate			ZONES=yes
690Sstevel@tonic-gate			echo " $zone\c"
700Sstevel@tonic-gate			#
712382Sdp			# zoneadmd puts itself into its own contract so
722382Sdp			# this service will lose sight of it.  We don't
732382Sdp			# support restart so it is OK for zoneadmd to
742382Sdp			# to be in an orphaned contract.
750Sstevel@tonic-gate			#
762382Sdp			zoneadm -z $zone boot &
7710718SJordan.Vaughan@Sun.com		else
7810718SJordan.Vaughan@Sun.com			zoneadm -z $zone sysboot &
790Sstevel@tonic-gate		fi
800Sstevel@tonic-gate	done
8110718SJordan.Vaughan@Sun.com
822382Sdp	#
832382Sdp	# Wait for all zoneadm processes to finish before allowing the
842382Sdp	# start method to exit.
852382Sdp	#
862382Sdp	wait
870Sstevel@tonic-gate	[ -n "$ZONES" ] && echo .
880Sstevel@tonic-gate	;;
890Sstevel@tonic-gate
900Sstevel@tonic-gate'stop')
910Sstevel@tonic-gate	egrep -vs '^#|^global:' /etc/zones/index || exit 0  # no local zones
920Sstevel@tonic-gate	[ "`zoneadm list`" = "global" ] && exit 0   # no zones running
930Sstevel@tonic-gate
942607Sdp	SVC_TIMEOUT=`svcprop -p stop/timeout_seconds $SMF_FMRI`
950Sstevel@tonic-gate
962712Snn35248	#
972712Snn35248	# First, try shutting down any running zones for which an "init 0" may
982712Snn35248	# work.
992712Snn35248	#
1002607Sdp	MAXSHUT=`expr 3 \* $SVC_TIMEOUT \/ 4` # 3/4 of time to zone shutdown
1012607Sdp	MAXHALT=`expr $SVC_TIMEOUT \/ 4`      # rest of time goes to halt
1022607Sdp
1032712Snn35248	zonelist=`shutdown_zones`
1040Sstevel@tonic-gate
1052712Snn35248	if [ -n "$zonelist" ]; then
1062712Snn35248		SHUTDOWN=0
1072712Snn35248		echo "Shutting down running zones (for up to $MAXSHUT" \
1082712Snn35248		    "seconds):\c"
1092712Snn35248
1102712Snn35248		for zone in $zonelist; do
1110Sstevel@tonic-gate			echo " $zone\c"
1120Sstevel@tonic-gate			zlogin -S $zone /sbin/init 0 < /dev/null >&0 2>&0 &
1130Sstevel@tonic-gate			SHUTDOWN=1
1142712Snn35248		done
1150Sstevel@tonic-gate
1162712Snn35248		[ $SHUTDOWN -eq 1 ] && echo "."
1172712Snn35248
1182712Snn35248		# Allow time for zones to shutdown cleanly
1190Sstevel@tonic-gate
1202712Snn35248		while [ $MAXSHUT -gt 0 -a "`shutdown_zones`" != "" ]; do
1212712Snn35248			MAXSHUT=`expr $MAXSHUT - 1`
1222712Snn35248			sleep 1	# wait a bit longer
1232712Snn35248		done
1242712Snn35248	fi
1250Sstevel@tonic-gate
1262712Snn35248	#
1272712Snn35248	# Second, try halting any non-global zones still running
1282712Snn35248	#
1290Sstevel@tonic-gate	WAITPIDS=""
1300Sstevel@tonic-gate	for zone in `zoneadm list`; do
1310Sstevel@tonic-gate		if [ "$zone" != "global" ]; then
1320Sstevel@tonic-gate			[ -z "$WAITPIDS" ] &&
1332607Sdp			    echo "Zones failed to shutdown; trying to halt " \
1342607Sdp			    "(for up to $MAXHALT seconds):\c"
1350Sstevel@tonic-gate			echo " $zone\c"
1360Sstevel@tonic-gate			zoneadm -z $zone halt &
1370Sstevel@tonic-gate			WAITPIDS="$WAITPIDS $!"
1380Sstevel@tonic-gate		fi
1390Sstevel@tonic-gate	done
1400Sstevel@tonic-gate	[ ! -z "$WAITPIDS" ] && echo .
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate	# Wait for the 'zoneadm halt' commands to complete.  We will let this
1432382Sdp	# run forever, since the restart daemon will eventually kill us off
1440Sstevel@tonic-gate	# anyway if the halts do not complete after a certain period of time.
1450Sstevel@tonic-gate	wait $WAITPIDS
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate	# If the halts complete but a zone is still not shutdown, it might
1480Sstevel@tonic-gate	# be in a state like 'shutting_down' or 'down'.  So we give it some
1490Sstevel@tonic-gate	# time to come all the way down.
1502712Snn35248
1510Sstevel@tonic-gate	while [ $MAXHALT -gt 0 -a "`zoneadm list`" != "global" ]; do
1520Sstevel@tonic-gate		MAXHALT=`expr $MAXHALT - 1`
1530Sstevel@tonic-gate		sleep 1	# wait a bit longer
1540Sstevel@tonic-gate	done
1550Sstevel@tonic-gate
1568412SPavel.Filipensky@Sun.COM	# If there are any remaining file systems in zones, try to unmount them.
1578412SPavel.Filipensky@Sun.COM	umountall -Z
1588412SPavel.Filipensky@Sun.COM
1590Sstevel@tonic-gate	#
1600Sstevel@tonic-gate	# Report on zones which failed to shutdown.
1610Sstevel@tonic-gate	#
1620Sstevel@tonic-gate	for zone in `zoneadm list`; do
1630Sstevel@tonic-gate		if [ "$zone" != "global" ]; then
1640Sstevel@tonic-gate			echo "Zone '$zone' failed to halt."
1650Sstevel@tonic-gate		fi
1660Sstevel@tonic-gate	done
1670Sstevel@tonic-gate	[ "`zoneadm list`" != "global" ] && exit 1   # zones still running
1680Sstevel@tonic-gate	;;
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate*)
1710Sstevel@tonic-gate	echo "Usage: $0 { start | stop }"
1720Sstevel@tonic-gate	exit 1
1730Sstevel@tonic-gate	;;
1740Sstevel@tonic-gateesac
1750Sstevel@tonic-gateexit 0
176