xref: /titanic_50/usr/src/cmd/zoneadm/svc-zones (revision 5fad3ade0b6bf1454149b5f22a06ce99e9c1e4ff)
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
65cb4571dSdp# Common Development and Distribution License (the "License").
75cb4571dSdp# 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#
23*5fad3adeSPavel Filipensky# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate# Use is subject to license terms.
257c478bd9Sstevel@tonic-gate
26e30bbc33Sdp. /lib/svc/share/smf_include.sh
277c478bd9Sstevel@tonic-gate
289acbbeafSnn35248#
299acbbeafSnn35248# Return a list of running, non-global zones for which a shutdown via
309acbbeafSnn35248# "/sbin/init 0" may work (typically only Solaris zones.)
319acbbeafSnn35248#
329acbbeafSnn35248# At present, this means any running "lx" zones don't qualify.
339acbbeafSnn35248#
349acbbeafSnn35248shutdown_zones()
359acbbeafSnn35248{
369acbbeafSnn35248	zoneadm list -p | nawk -F: '{
379acbbeafSnn35248		if (($5 != "lx") && ($2 != "global")) {
389acbbeafSnn35248			print $2
399acbbeafSnn35248		}
409acbbeafSnn35248	}'
419acbbeafSnn35248}
429acbbeafSnn35248
437c478bd9Sstevel@tonic-gate[ ! -x /usr/sbin/zoneadm ] && exit 0	# SUNWzoneu not installed
447c478bd9Sstevel@tonic-gate
45e30bbc33Sdpif [ -z "$SMF_FMRI" ]; then
46e30bbc33Sdp	echo "this script can only be invoked by smf(5)"
47e30bbc33Sdp	exit $SMF_EXIT_ERR_NOSMF
48e30bbc33Sdpfi
49e30bbc33Sdp
507c478bd9Sstevel@tonic-gate# Make sure working directory is / to prevent unmounting problems.
517c478bd9Sstevel@tonic-gatecd /
527c478bd9Sstevel@tonic-gatePATH=/usr/sbin:/usr/bin; export PATH
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gatecase "$1" in
557c478bd9Sstevel@tonic-gate'start')
567c478bd9Sstevel@tonic-gate	egrep -vs '^#|^global:' /etc/zones/index || exit 0  # no local zones
577c478bd9Sstevel@tonic-gate	ZONES=""
587c478bd9Sstevel@tonic-gate	for zone in `zoneadm list -pi | nawk -F: '{
597c478bd9Sstevel@tonic-gate			if ($3 == "installed") {
607c478bd9Sstevel@tonic-gate				print $2
617c478bd9Sstevel@tonic-gate			}
627c478bd9Sstevel@tonic-gate		}'`; do
637c478bd9Sstevel@tonic-gate		zonecfg -z $zone info autoboot | grep "true" >/dev/null 2>&1
647c478bd9Sstevel@tonic-gate		if [ $? -eq 0 ]; then
657c478bd9Sstevel@tonic-gate			[ -z "$ZONES" ] && echo "Booting zones:\c"
667c478bd9Sstevel@tonic-gate			ZONES=yes
677c478bd9Sstevel@tonic-gate			echo " $zone\c"
687c478bd9Sstevel@tonic-gate			#
695cb4571dSdp			# zoneadmd puts itself into its own contract so
705cb4571dSdp			# this service will lose sight of it.  We don't
715cb4571dSdp			# support restart so it is OK for zoneadmd to
725cb4571dSdp			# to be in an orphaned contract.
737c478bd9Sstevel@tonic-gate			#
745cb4571dSdp			zoneadm -z $zone boot &
757c478bd9Sstevel@tonic-gate		fi
767c478bd9Sstevel@tonic-gate	done
775cb4571dSdp	#
785cb4571dSdp	# Wait for all zoneadm processes to finish before allowing the
795cb4571dSdp	# start method to exit.
805cb4571dSdp	#
815cb4571dSdp	wait
827c478bd9Sstevel@tonic-gate	[ -n "$ZONES" ] && echo .
837c478bd9Sstevel@tonic-gate	;;
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate'stop')
867c478bd9Sstevel@tonic-gate	egrep -vs '^#|^global:' /etc/zones/index || exit 0  # no local zones
877c478bd9Sstevel@tonic-gate	[ "`zoneadm list`" = "global" ] && exit 0   # no zones running
887c478bd9Sstevel@tonic-gate
899e7542f4Sdp	SVC_TIMEOUT=`svcprop -p stop/timeout_seconds $SMF_FMRI`
907c478bd9Sstevel@tonic-gate
919acbbeafSnn35248	#
929acbbeafSnn35248	# First, try shutting down any running zones for which an "init 0" may
939acbbeafSnn35248	# work.
949acbbeafSnn35248	#
959e7542f4Sdp	MAXSHUT=`expr 3 \* $SVC_TIMEOUT \/ 4` # 3/4 of time to zone shutdown
969e7542f4Sdp	MAXHALT=`expr $SVC_TIMEOUT \/ 4`      # rest of time goes to halt
979e7542f4Sdp
989acbbeafSnn35248	zonelist=`shutdown_zones`
997c478bd9Sstevel@tonic-gate
1009acbbeafSnn35248	if [ -n "$zonelist" ]; then
1017c478bd9Sstevel@tonic-gate		SHUTDOWN=0
1029acbbeafSnn35248		echo "Shutting down running zones (for up to $MAXSHUT" \
1039acbbeafSnn35248		    "seconds):\c"
1049acbbeafSnn35248
1059acbbeafSnn35248		for zone in $zonelist; do
1067c478bd9Sstevel@tonic-gate			echo " $zone\c"
1077c478bd9Sstevel@tonic-gate			zlogin -S $zone /sbin/init 0 < /dev/null >&0 2>&0 &
1087c478bd9Sstevel@tonic-gate			SHUTDOWN=1
1097c478bd9Sstevel@tonic-gate		done
1109acbbeafSnn35248
1117c478bd9Sstevel@tonic-gate		[ $SHUTDOWN -eq 1 ] && echo "."
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate		# Allow time for zones to shutdown cleanly
1147c478bd9Sstevel@tonic-gate
1159acbbeafSnn35248		while [ $MAXSHUT -gt 0 -a "`shutdown_zones`" != "" ]; do
1167c478bd9Sstevel@tonic-gate			MAXSHUT=`expr $MAXSHUT - 1`
1177c478bd9Sstevel@tonic-gate			sleep 1	# wait a bit longer
1187c478bd9Sstevel@tonic-gate		done
1199acbbeafSnn35248	fi
1207c478bd9Sstevel@tonic-gate
1219acbbeafSnn35248	#
1229acbbeafSnn35248	# Second, try halting any non-global zones still running
1239acbbeafSnn35248	#
1247c478bd9Sstevel@tonic-gate	WAITPIDS=""
1257c478bd9Sstevel@tonic-gate	for zone in `zoneadm list`; do
1267c478bd9Sstevel@tonic-gate		if [ "$zone" != "global" ]; then
1277c478bd9Sstevel@tonic-gate			[ -z "$WAITPIDS" ] &&
1289e7542f4Sdp			    echo "Zones failed to shutdown; trying to halt " \
1299e7542f4Sdp			    "(for up to $MAXHALT seconds):\c"
1307c478bd9Sstevel@tonic-gate			echo " $zone\c"
1317c478bd9Sstevel@tonic-gate			zoneadm -z $zone halt &
1327c478bd9Sstevel@tonic-gate			WAITPIDS="$WAITPIDS $!"
1337c478bd9Sstevel@tonic-gate		fi
1347c478bd9Sstevel@tonic-gate	done
1357c478bd9Sstevel@tonic-gate	[ ! -z "$WAITPIDS" ] && echo .
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate	# Wait for the 'zoneadm halt' commands to complete.  We will let this
1387c478bd9Sstevel@tonic-gate	# run forever, since the restart daemon will eventually kill us off
1397c478bd9Sstevel@tonic-gate	# anyway if the halts do not complete after a certain period of time.
1407c478bd9Sstevel@tonic-gate	wait $WAITPIDS
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate	# If the halts complete but a zone is still not shutdown, it might
1437c478bd9Sstevel@tonic-gate	# be in a state like 'shutting_down' or 'down'.  So we give it some
1447c478bd9Sstevel@tonic-gate	# time to come all the way down.
1459acbbeafSnn35248
1467c478bd9Sstevel@tonic-gate	while [ $MAXHALT -gt 0 -a "`zoneadm list`" != "global" ]; do
1477c478bd9Sstevel@tonic-gate		MAXHALT=`expr $MAXHALT - 1`
1487c478bd9Sstevel@tonic-gate		sleep 1	# wait a bit longer
1497c478bd9Sstevel@tonic-gate	done
1507c478bd9Sstevel@tonic-gate
151*5fad3adeSPavel Filipensky	# If there are any remaining file systems in zones, try to unmount them.
152*5fad3adeSPavel Filipensky	umountall -Z
153*5fad3adeSPavel Filipensky
1547c478bd9Sstevel@tonic-gate	#
1557c478bd9Sstevel@tonic-gate	# Report on zones which failed to shutdown.
1567c478bd9Sstevel@tonic-gate	#
1577c478bd9Sstevel@tonic-gate	for zone in `zoneadm list`; do
1587c478bd9Sstevel@tonic-gate		if [ "$zone" != "global" ]; then
1597c478bd9Sstevel@tonic-gate			echo "Zone '$zone' failed to halt."
1607c478bd9Sstevel@tonic-gate		fi
1617c478bd9Sstevel@tonic-gate	done
1627c478bd9Sstevel@tonic-gate	[ "`zoneadm list`" != "global" ] && exit 1   # zones still running
1637c478bd9Sstevel@tonic-gate	;;
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate*)
1667c478bd9Sstevel@tonic-gate	echo "Usage: $0 { start | stop }"
1677c478bd9Sstevel@tonic-gate	exit 1
1687c478bd9Sstevel@tonic-gate	;;
1697c478bd9Sstevel@tonic-gateesac
1707c478bd9Sstevel@tonic-gateexit 0
171