xref: /illumos-gate/usr/src/cmd/zoneadm/svc-zones (revision bbf21555)
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#
23eb5a5c78SSurya Prakki# Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
243c7284bdSAlexander Eremin# Copyright 2014 Nexenta Systems, Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate
26e30bbc33Sdp. /lib/svc/share/smf_include.sh
277c478bd9Sstevel@tonic-gate
289acbbeafSnn#
299acbbeafSnn# Return a list of running, non-global zones for which a shutdown via
309acbbeafSnn# "/sbin/init 0" may work (typically only Solaris zones.)
319acbbeafSnn#
329acbbeafSnnshutdown_zones()
339acbbeafSnn{
349acbbeafSnn	zoneadm list -p | nawk -F: '{
35eb5a5c78SSurya Prakki		if ($2 != "global") {
369acbbeafSnn			print $2
379acbbeafSnn		}
389acbbeafSnn	}'
399acbbeafSnn}
409acbbeafSnn
417c478bd9Sstevel@tonic-gate[ ! -x /usr/sbin/zoneadm ] && exit 0	# SUNWzoneu not installed
427c478bd9Sstevel@tonic-gate
43e30bbc33Sdpif [ -z "$SMF_FMRI" ]; then
44*bbf21555SRichard Lowe	echo "this script can only be invoked by smf(7)"
45e30bbc33Sdp	exit $SMF_EXIT_ERR_NOSMF
46e30bbc33Sdpfi
47e30bbc33Sdp
487c478bd9Sstevel@tonic-gate# Make sure working directory is / to prevent unmounting problems.
497c478bd9Sstevel@tonic-gatecd /
507c478bd9Sstevel@tonic-gatePATH=/usr/sbin:/usr/bin; export PATH
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gatecase "$1" in
537c478bd9Sstevel@tonic-gate'start')
547c478bd9Sstevel@tonic-gate	egrep -vs '^#|^global:' /etc/zones/index || exit 0  # no local zones
55fbbfbc6eSjv
56fbbfbc6eSjv	#
57fbbfbc6eSjv	# Boot the installed zones for which the "autoboot" zone property is
58fbbfbc6eSjv	# set and invoke the sysboot hook for all other installed zones.
59fbbfbc6eSjv	#
607c478bd9Sstevel@tonic-gate	ZONES=""
617c478bd9Sstevel@tonic-gate	for zone in `zoneadm list -pi | nawk -F: '{
627c478bd9Sstevel@tonic-gate			if ($3 == "installed") {
637c478bd9Sstevel@tonic-gate				print $2
647c478bd9Sstevel@tonic-gate			}
657c478bd9Sstevel@tonic-gate		}'`; do
667c478bd9Sstevel@tonic-gate		zonecfg -z $zone info autoboot | grep "true" >/dev/null 2>&1
677c478bd9Sstevel@tonic-gate		if [ $? -eq 0 ]; then
687c478bd9Sstevel@tonic-gate			[ -z "$ZONES" ] && echo "Booting zones:\c"
697c478bd9Sstevel@tonic-gate			ZONES=yes
707c478bd9Sstevel@tonic-gate			echo " $zone\c"
717c478bd9Sstevel@tonic-gate			#
725cb4571dSdp			# zoneadmd puts itself into its own contract so
735cb4571dSdp			# this service will lose sight of it.  We don't
745cb4571dSdp			# support restart so it is OK for zoneadmd to
755cb4571dSdp			# to be in an orphaned contract.
767c478bd9Sstevel@tonic-gate			#
775cb4571dSdp			zoneadm -z $zone boot &
78fbbfbc6eSjv		else
79fbbfbc6eSjv			zoneadm -z $zone sysboot &
807c478bd9Sstevel@tonic-gate		fi
817c478bd9Sstevel@tonic-gate	done
82fbbfbc6eSjv
835cb4571dSdp	#
845cb4571dSdp	# Wait for all zoneadm processes to finish before allowing the
855cb4571dSdp	# start method to exit.
865cb4571dSdp	#
875cb4571dSdp	wait
887c478bd9Sstevel@tonic-gate	[ -n "$ZONES" ] && echo .
897c478bd9Sstevel@tonic-gate	;;
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate'stop')
927c478bd9Sstevel@tonic-gate	egrep -vs '^#|^global:' /etc/zones/index || exit 0  # no local zones
937c478bd9Sstevel@tonic-gate	[ "`zoneadm list`" = "global" ] && exit 0   # no zones running
947c478bd9Sstevel@tonic-gate
959e7542f4Sdp	SVC_TIMEOUT=`svcprop -p stop/timeout_seconds $SMF_FMRI`
967c478bd9Sstevel@tonic-gate
979acbbeafSnn	#
989acbbeafSnn	# First, try shutting down any running zones for which an "init 0" may
999acbbeafSnn	# work.
1009acbbeafSnn	#
1019e7542f4Sdp	MAXSHUT=`expr 3 \* $SVC_TIMEOUT \/ 4` # 3/4 of time to zone shutdown
1029e7542f4Sdp	MAXHALT=`expr $SVC_TIMEOUT \/ 4`      # rest of time goes to halt
1039e7542f4Sdp
1049acbbeafSnn	zonelist=`shutdown_zones`
1057c478bd9Sstevel@tonic-gate
1069acbbeafSnn	if [ -n "$zonelist" ]; then
1079acbbeafSnn		SHUTDOWN=0
1089acbbeafSnn		echo "Shutting down running zones (for up to $MAXSHUT" \
1099acbbeafSnn		    "seconds):\c"
1107c478bd9Sstevel@tonic-gate
1119acbbeafSnn		for zone in $zonelist; do
1127c478bd9Sstevel@tonic-gate			echo " $zone\c"
1133c7284bdSAlexander Eremin			zoneadm -z $zone shutdown &
1147c478bd9Sstevel@tonic-gate			SHUTDOWN=1
1159acbbeafSnn		done
1167c478bd9Sstevel@tonic-gate
1179acbbeafSnn		[ $SHUTDOWN -eq 1 ] && echo "."
1187c478bd9Sstevel@tonic-gate
1199acbbeafSnn		# Allow time for zones to shutdown cleanly
1207c478bd9Sstevel@tonic-gate
1219acbbeafSnn		while [ $MAXSHUT -gt 0 -a "`shutdown_zones`" != "" ]; do
1229acbbeafSnn			MAXSHUT=`expr $MAXSHUT - 1`
1239acbbeafSnn			sleep 1	# wait a bit longer
1249acbbeafSnn		done
1259acbbeafSnn	fi
1267c478bd9Sstevel@tonic-gate
1279acbbeafSnn	#
1289acbbeafSnn	# Second, try halting any non-global zones still running
1299acbbeafSnn	#
1307c478bd9Sstevel@tonic-gate	WAITPIDS=""
1317c478bd9Sstevel@tonic-gate	for zone in `zoneadm list`; do
1327c478bd9Sstevel@tonic-gate		if [ "$zone" != "global" ]; then
1337c478bd9Sstevel@tonic-gate			[ -z "$WAITPIDS" ] &&
1349e7542f4Sdp			    echo "Zones failed to shutdown; trying to halt " \
1359e7542f4Sdp			    "(for up to $MAXHALT seconds):\c"
1367c478bd9Sstevel@tonic-gate			echo " $zone\c"
1377c478bd9Sstevel@tonic-gate			zoneadm -z $zone halt &
1387c478bd9Sstevel@tonic-gate			WAITPIDS="$WAITPIDS $!"
1397c478bd9Sstevel@tonic-gate		fi
1407c478bd9Sstevel@tonic-gate	done
1417c478bd9Sstevel@tonic-gate	[ ! -z "$WAITPIDS" ] && echo .
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate	# Wait for the 'zoneadm halt' commands to complete.  We will let this
1445cb4571dSdp	# run forever, since the restart daemon will eventually kill us off
1457c478bd9Sstevel@tonic-gate	# anyway if the halts do not complete after a certain period of time.
1467c478bd9Sstevel@tonic-gate	wait $WAITPIDS
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate	# If the halts complete but a zone is still not shutdown, it might
1497c478bd9Sstevel@tonic-gate	# be in a state like 'shutting_down' or 'down'.  So we give it some
1507c478bd9Sstevel@tonic-gate	# time to come all the way down.
1519acbbeafSnn
1527c478bd9Sstevel@tonic-gate	while [ $MAXHALT -gt 0 -a "`zoneadm list`" != "global" ]; do
1537c478bd9Sstevel@tonic-gate		MAXHALT=`expr $MAXHALT - 1`
1547c478bd9Sstevel@tonic-gate		sleep 1	# wait a bit longer
1557c478bd9Sstevel@tonic-gate	done
1567c478bd9Sstevel@tonic-gate
1575fad3adeSPavel Filipensky	# If there are any remaining file systems in zones, try to unmount them.
1585fad3adeSPavel Filipensky	umountall -Z
1595fad3adeSPavel Filipensky
1607c478bd9Sstevel@tonic-gate	#
1617c478bd9Sstevel@tonic-gate	# Report on zones which failed to shutdown.
1627c478bd9Sstevel@tonic-gate	#
1637c478bd9Sstevel@tonic-gate	for zone in `zoneadm list`; do
1647c478bd9Sstevel@tonic-gate		if [ "$zone" != "global" ]; then
1657c478bd9Sstevel@tonic-gate			echo "Zone '$zone' failed to halt."
1667c478bd9Sstevel@tonic-gate		fi
1677c478bd9Sstevel@tonic-gate	done
1687c478bd9Sstevel@tonic-gate	[ "`zoneadm list`" != "global" ] && exit 1   # zones still running
1697c478bd9Sstevel@tonic-gate	;;
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate*)
1727c478bd9Sstevel@tonic-gate	echo "Usage: $0 { start | stop }"
1737c478bd9Sstevel@tonic-gate	exit 1
1747c478bd9Sstevel@tonic-gate	;;
1757c478bd9Sstevel@tonic-gateesac
1767c478bd9Sstevel@tonic-gateexit 0
177