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