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