xref: /titanic_44/usr/src/cmd/zoneadm/svc-zones (revision 5cb4571d1b794d3be2bb486e9f9d721bc3e7894f)
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*5cb4571dSdp# Common Development and Distribution License (the "License").
7*5cb4571dSdp# 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*5cb4571dSdp# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate# Use is subject to license terms.
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate# ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate#
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate[ ! -x /usr/sbin/zoneadm ] && exit 0	# SUNWzoneu not installed
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate# Make sure working directory is / to prevent unmounting problems.
327c478bd9Sstevel@tonic-gatecd /
337c478bd9Sstevel@tonic-gatePATH=/usr/sbin:/usr/bin; export PATH
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gatecase "$1" in
367c478bd9Sstevel@tonic-gate'start')
377c478bd9Sstevel@tonic-gate	egrep -vs '^#|^global:' /etc/zones/index || exit 0  # no local zones
387c478bd9Sstevel@tonic-gate	ZONES=""
397c478bd9Sstevel@tonic-gate	for zone in `zoneadm list -pi | nawk -F: '{
407c478bd9Sstevel@tonic-gate			if ($3 == "installed") {
417c478bd9Sstevel@tonic-gate				print $2
427c478bd9Sstevel@tonic-gate			}
437c478bd9Sstevel@tonic-gate		}'`; do
447c478bd9Sstevel@tonic-gate		zonecfg -z $zone info autoboot | grep "true" >/dev/null 2>&1
457c478bd9Sstevel@tonic-gate		if [ $? -eq 0 ]; then
467c478bd9Sstevel@tonic-gate			[ -z "$ZONES" ] && echo "Booting zones:\c"
477c478bd9Sstevel@tonic-gate			ZONES=yes
487c478bd9Sstevel@tonic-gate			echo " $zone\c"
497c478bd9Sstevel@tonic-gate			#
50*5cb4571dSdp			# zoneadmd puts itself into its own contract so
51*5cb4571dSdp			# this service will lose sight of it.  We don't
52*5cb4571dSdp			# support restart so it is OK for zoneadmd to
53*5cb4571dSdp			# to be in an orphaned contract.
547c478bd9Sstevel@tonic-gate			#
55*5cb4571dSdp			zoneadm -z $zone boot &
567c478bd9Sstevel@tonic-gate		fi
577c478bd9Sstevel@tonic-gate	done
58*5cb4571dSdp	#
59*5cb4571dSdp	# Wait for all zoneadm processes to finish before allowing the
60*5cb4571dSdp	# start method to exit.
61*5cb4571dSdp	#
62*5cb4571dSdp	wait
637c478bd9Sstevel@tonic-gate	[ -n "$ZONES" ] && echo .
647c478bd9Sstevel@tonic-gate	;;
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate'stop')
677c478bd9Sstevel@tonic-gate	egrep -vs '^#|^global:' /etc/zones/index || exit 0  # no local zones
687c478bd9Sstevel@tonic-gate	[ "`zoneadm list`" = "global" ] && exit 0   # no zones running
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate	MAXSHUT=60      # maximum time we'll wait for all zones to shutdown
717c478bd9Sstevel@tonic-gate	MAXHALT=30      # maximum time we'll wait for all zones to halt
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate	echo "Shutting down running zones:\c"
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate	# First, try letting them run their shutdown scripts.
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate	SHUTDOWN=0
787c478bd9Sstevel@tonic-gate	for zone in `zoneadm list`; do
797c478bd9Sstevel@tonic-gate		if [ "$zone" != "global" ]; then
807c478bd9Sstevel@tonic-gate			echo " $zone\c"
817c478bd9Sstevel@tonic-gate			zlogin -S $zone /sbin/init 0 < /dev/null >&0 2>&0 &
827c478bd9Sstevel@tonic-gate			SHUTDOWN=1
837c478bd9Sstevel@tonic-gate		fi
847c478bd9Sstevel@tonic-gate	done
857c478bd9Sstevel@tonic-gate	[ $SHUTDOWN -eq 1 ] && echo "."
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate	# Allow time for zones to shutdown cleanly
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate	while [ $MAXSHUT -gt 0 -a "`zoneadm list`" != "global" ]; do
907c478bd9Sstevel@tonic-gate		MAXSHUT=`expr $MAXSHUT - 1`
917c478bd9Sstevel@tonic-gate		sleep 1	# wait a bit longer
927c478bd9Sstevel@tonic-gate	done
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate	# Second, try halting them.
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate	WAITPIDS=""
977c478bd9Sstevel@tonic-gate	for zone in `zoneadm list`; do
987c478bd9Sstevel@tonic-gate		if [ "$zone" != "global" ]; then
997c478bd9Sstevel@tonic-gate			[ -z "$WAITPIDS" ] &&
1007c478bd9Sstevel@tonic-gate			    echo "Zones failed to shutdown; trying to halt:\c"
1017c478bd9Sstevel@tonic-gate			echo " $zone\c"
1027c478bd9Sstevel@tonic-gate			zoneadm -z $zone halt &
1037c478bd9Sstevel@tonic-gate			WAITPIDS="$WAITPIDS $!"
1047c478bd9Sstevel@tonic-gate		fi
1057c478bd9Sstevel@tonic-gate	done
1067c478bd9Sstevel@tonic-gate	[ ! -z "$WAITPIDS" ] && echo .
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate	# Wait for the 'zoneadm halt' commands to complete.  We will let this
1097c478bd9Sstevel@tonic-gate	# run forever, since the restart daemon will eventually kill us off
1107c478bd9Sstevel@tonic-gate	# anyway if the halts do not complete after a certain period of time.
1117c478bd9Sstevel@tonic-gate	wait $WAITPIDS
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate	# If the halts complete but a zone is still not shutdown, it might
1147c478bd9Sstevel@tonic-gate	# be in a state like 'shutting_down' or 'down'.  So we give it some
1157c478bd9Sstevel@tonic-gate	# time to come all the way down.
1167c478bd9Sstevel@tonic-gate	while [ $MAXHALT -gt 0 -a "`zoneadm list`" != "global" ]; do
1177c478bd9Sstevel@tonic-gate		MAXHALT=`expr $MAXHALT - 1`
1187c478bd9Sstevel@tonic-gate		sleep 1	# wait a bit longer
1197c478bd9Sstevel@tonic-gate	done
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate	#
1227c478bd9Sstevel@tonic-gate	# Report on zones which failed to shutdown.
1237c478bd9Sstevel@tonic-gate	#
1247c478bd9Sstevel@tonic-gate	for zone in `zoneadm list`; do
1257c478bd9Sstevel@tonic-gate		if [ "$zone" != "global" ]; then
1267c478bd9Sstevel@tonic-gate			echo "Zone '$zone' failed to halt."
1277c478bd9Sstevel@tonic-gate		fi
1287c478bd9Sstevel@tonic-gate	done
1297c478bd9Sstevel@tonic-gate	[ "`zoneadm list`" != "global" ] && exit 1   # zones still running
1307c478bd9Sstevel@tonic-gate	;;
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate*)
1337c478bd9Sstevel@tonic-gate	echo "Usage: $0 { start | stop }"
1347c478bd9Sstevel@tonic-gate	exit 1
1357c478bd9Sstevel@tonic-gate	;;
1367c478bd9Sstevel@tonic-gateesac
1377c478bd9Sstevel@tonic-gateexit 0
138