xref: /illumos-gate/usr/src/cmd/zoneadm/svc-zones (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate#!/sbin/sh
2*7c478bd9Sstevel@tonic-gate#
3*7c478bd9Sstevel@tonic-gate# CDDL HEADER START
4*7c478bd9Sstevel@tonic-gate#
5*7c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*7c478bd9Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only
7*7c478bd9Sstevel@tonic-gate# (the "License").  You may not use this file except in compliance
8*7c478bd9Sstevel@tonic-gate# with the License.
9*7c478bd9Sstevel@tonic-gate#
10*7c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11*7c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
12*7c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
13*7c478bd9Sstevel@tonic-gate# and limitations under the License.
14*7c478bd9Sstevel@tonic-gate#
15*7c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
16*7c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17*7c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
18*7c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
19*7c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
20*7c478bd9Sstevel@tonic-gate#
21*7c478bd9Sstevel@tonic-gate# CDDL HEADER END
22*7c478bd9Sstevel@tonic-gate#
23*7c478bd9Sstevel@tonic-gate#
24*7c478bd9Sstevel@tonic-gate# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
25*7c478bd9Sstevel@tonic-gate# Use is subject to license terms.
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate# ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate#
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate[ ! -x /usr/sbin/zoneadm ] && exit 0	# SUNWzoneu not installed
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate# Make sure working directory is / to prevent unmounting problems.
33*7c478bd9Sstevel@tonic-gatecd /
34*7c478bd9Sstevel@tonic-gatePATH=/usr/sbin:/usr/bin; export PATH
35*7c478bd9Sstevel@tonic-gate
36*7c478bd9Sstevel@tonic-gatecase "$1" in
37*7c478bd9Sstevel@tonic-gate'start')
38*7c478bd9Sstevel@tonic-gate	egrep -vs '^#|^global:' /etc/zones/index || exit 0  # no local zones
39*7c478bd9Sstevel@tonic-gate	ZONES=""
40*7c478bd9Sstevel@tonic-gate	for zone in `zoneadm list -pi | nawk -F: '{
41*7c478bd9Sstevel@tonic-gate			if ($3 == "installed") {
42*7c478bd9Sstevel@tonic-gate				print $2
43*7c478bd9Sstevel@tonic-gate			}
44*7c478bd9Sstevel@tonic-gate		}'`; do
45*7c478bd9Sstevel@tonic-gate		zonecfg -z $zone info autoboot | grep "true" >/dev/null 2>&1
46*7c478bd9Sstevel@tonic-gate		if [ $? -eq 0 ]; then
47*7c478bd9Sstevel@tonic-gate			[ -z "$ZONES" ] && echo "Booting zones:\c"
48*7c478bd9Sstevel@tonic-gate			ZONES=yes
49*7c478bd9Sstevel@tonic-gate			echo " $zone\c"
50*7c478bd9Sstevel@tonic-gate			#
51*7c478bd9Sstevel@tonic-gate			# We don't (yet) support restart for zones, so
52*7c478bd9Sstevel@tonic-gate			# we need to get all of the zones stuff off into
53*7c478bd9Sstevel@tonic-gate			# a different contract.
54*7c478bd9Sstevel@tonic-gate			#
55*7c478bd9Sstevel@tonic-gate			ctrun -l none -i none zoneadm -z $zone boot &
56*7c478bd9Sstevel@tonic-gate		fi
57*7c478bd9Sstevel@tonic-gate	done
58*7c478bd9Sstevel@tonic-gate	[ -n "$ZONES" ] && echo .
59*7c478bd9Sstevel@tonic-gate	;;
60*7c478bd9Sstevel@tonic-gate
61*7c478bd9Sstevel@tonic-gate'stop')
62*7c478bd9Sstevel@tonic-gate	egrep -vs '^#|^global:' /etc/zones/index || exit 0  # no local zones
63*7c478bd9Sstevel@tonic-gate	[ "`zoneadm list`" = "global" ] && exit 0   # no zones running
64*7c478bd9Sstevel@tonic-gate
65*7c478bd9Sstevel@tonic-gate	MAXSHUT=60      # maximum time we'll wait for all zones to shutdown
66*7c478bd9Sstevel@tonic-gate	MAXHALT=30      # maximum time we'll wait for all zones to halt
67*7c478bd9Sstevel@tonic-gate
68*7c478bd9Sstevel@tonic-gate	echo "Shutting down running zones:\c"
69*7c478bd9Sstevel@tonic-gate
70*7c478bd9Sstevel@tonic-gate	# First, try letting them run their shutdown scripts.
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gate	SHUTDOWN=0
73*7c478bd9Sstevel@tonic-gate	for zone in `zoneadm list`; do
74*7c478bd9Sstevel@tonic-gate		if [ "$zone" != "global" ]; then
75*7c478bd9Sstevel@tonic-gate			echo " $zone\c"
76*7c478bd9Sstevel@tonic-gate			zlogin -S $zone /sbin/init 0 < /dev/null >&0 2>&0 &
77*7c478bd9Sstevel@tonic-gate			SHUTDOWN=1
78*7c478bd9Sstevel@tonic-gate		fi
79*7c478bd9Sstevel@tonic-gate	done
80*7c478bd9Sstevel@tonic-gate	[ $SHUTDOWN -eq 1 ] && echo "."
81*7c478bd9Sstevel@tonic-gate
82*7c478bd9Sstevel@tonic-gate	# Allow time for zones to shutdown cleanly
83*7c478bd9Sstevel@tonic-gate
84*7c478bd9Sstevel@tonic-gate	while [ $MAXSHUT -gt 0 -a "`zoneadm list`" != "global" ]; do
85*7c478bd9Sstevel@tonic-gate		MAXSHUT=`expr $MAXSHUT - 1`
86*7c478bd9Sstevel@tonic-gate		sleep 1	# wait a bit longer
87*7c478bd9Sstevel@tonic-gate	done
88*7c478bd9Sstevel@tonic-gate
89*7c478bd9Sstevel@tonic-gate	# Second, try halting them.
90*7c478bd9Sstevel@tonic-gate
91*7c478bd9Sstevel@tonic-gate	WAITPIDS=""
92*7c478bd9Sstevel@tonic-gate	for zone in `zoneadm list`; do
93*7c478bd9Sstevel@tonic-gate		if [ "$zone" != "global" ]; then
94*7c478bd9Sstevel@tonic-gate			[ -z "$WAITPIDS" ] &&
95*7c478bd9Sstevel@tonic-gate			    echo "Zones failed to shutdown; trying to halt:\c"
96*7c478bd9Sstevel@tonic-gate			echo " $zone\c"
97*7c478bd9Sstevel@tonic-gate			zoneadm -z $zone halt &
98*7c478bd9Sstevel@tonic-gate			WAITPIDS="$WAITPIDS $!"
99*7c478bd9Sstevel@tonic-gate		fi
100*7c478bd9Sstevel@tonic-gate	done
101*7c478bd9Sstevel@tonic-gate	[ ! -z "$WAITPIDS" ] && echo .
102*7c478bd9Sstevel@tonic-gate
103*7c478bd9Sstevel@tonic-gate	# Wait for the 'zoneadm halt' commands to complete.  We will let this
104*7c478bd9Sstevel@tonic-gate	# run forever, since the restart daemon will eventually kill us off
105*7c478bd9Sstevel@tonic-gate	# anyway if the halts do not complete after a certain period of time.
106*7c478bd9Sstevel@tonic-gate	wait $WAITPIDS
107*7c478bd9Sstevel@tonic-gate
108*7c478bd9Sstevel@tonic-gate	# If the halts complete but a zone is still not shutdown, it might
109*7c478bd9Sstevel@tonic-gate	# be in a state like 'shutting_down' or 'down'.  So we give it some
110*7c478bd9Sstevel@tonic-gate	# time to come all the way down.
111*7c478bd9Sstevel@tonic-gate	while [ $MAXHALT -gt 0 -a "`zoneadm list`" != "global" ]; do
112*7c478bd9Sstevel@tonic-gate		MAXHALT=`expr $MAXHALT - 1`
113*7c478bd9Sstevel@tonic-gate		sleep 1	# wait a bit longer
114*7c478bd9Sstevel@tonic-gate	done
115*7c478bd9Sstevel@tonic-gate
116*7c478bd9Sstevel@tonic-gate	#
117*7c478bd9Sstevel@tonic-gate	# Report on zones which failed to shutdown.
118*7c478bd9Sstevel@tonic-gate	#
119*7c478bd9Sstevel@tonic-gate	for zone in `zoneadm list`; do
120*7c478bd9Sstevel@tonic-gate		if [ "$zone" != "global" ]; then
121*7c478bd9Sstevel@tonic-gate			echo "Zone '$zone' failed to halt."
122*7c478bd9Sstevel@tonic-gate		fi
123*7c478bd9Sstevel@tonic-gate	done
124*7c478bd9Sstevel@tonic-gate	[ "`zoneadm list`" != "global" ] && exit 1   # zones still running
125*7c478bd9Sstevel@tonic-gate	;;
126*7c478bd9Sstevel@tonic-gate
127*7c478bd9Sstevel@tonic-gate*)
128*7c478bd9Sstevel@tonic-gate	echo "Usage: $0 { start | stop }"
129*7c478bd9Sstevel@tonic-gate	exit 1
130*7c478bd9Sstevel@tonic-gate	;;
131*7c478bd9Sstevel@tonic-gateesac
132*7c478bd9Sstevel@tonic-gateexit 0
133