xref: /titanic_51/usr/src/cmd/zoneadm/svc-zones (revision 8ba25627aaba4ea5318b8a7588142fdcdc1c765a)
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	SVC_TIMEOUT=`svcprop -p stop/timeout_seconds $SMF_FMRI`
71
72	MAXSHUT=`expr 3 \* $SVC_TIMEOUT \/ 4` # 3/4 of time to zone shutdown
73	MAXHALT=`expr $SVC_TIMEOUT \/ 4`      # rest of time goes to halt
74
75	echo "Shutting down running zones (for up to $MAXSHUT seconds):\c"
76
77	# First, try letting them run their shutdown scripts.
78
79	SHUTDOWN=0
80	for zone in `zoneadm list`; do
81		if [ "$zone" != "global" ]; then
82			echo " $zone\c"
83			zlogin -S $zone /sbin/init 0 < /dev/null >&0 2>&0 &
84			SHUTDOWN=1
85		fi
86	done
87	[ $SHUTDOWN -eq 1 ] && echo "."
88
89	# Allow time for zones to shutdown cleanly
90
91	while [ $MAXSHUT -gt 0 -a "`zoneadm list`" != "global" ]; do
92		MAXSHUT=`expr $MAXSHUT - 1`
93		sleep 1	# wait a bit longer
94	done
95
96	# Second, try halting them.
97
98	WAITPIDS=""
99	for zone in `zoneadm list`; do
100		if [ "$zone" != "global" ]; then
101			[ -z "$WAITPIDS" ] &&
102			    echo "Zones failed to shutdown; trying to halt " \
103			    "(for up to $MAXHALT seconds):\c"
104			echo " $zone\c"
105			zoneadm -z $zone halt &
106			WAITPIDS="$WAITPIDS $!"
107		fi
108	done
109	[ ! -z "$WAITPIDS" ] && echo .
110
111	# Wait for the 'zoneadm halt' commands to complete.  We will let this
112	# run forever, since the restart daemon will eventually kill us off
113	# anyway if the halts do not complete after a certain period of time.
114	wait $WAITPIDS
115
116	# If the halts complete but a zone is still not shutdown, it might
117	# be in a state like 'shutting_down' or 'down'.  So we give it some
118	# time to come all the way down.
119	while [ $MAXHALT -gt 0 -a "`zoneadm list`" != "global" ]; do
120		MAXHALT=`expr $MAXHALT - 1`
121		sleep 1	# wait a bit longer
122	done
123
124	#
125	# Report on zones which failed to shutdown.
126	#
127	for zone in `zoneadm list`; do
128		if [ "$zone" != "global" ]; then
129			echo "Zone '$zone' failed to halt."
130		fi
131	done
132	[ "`zoneadm list`" != "global" ] && exit 1   # zones still running
133	;;
134
135*)
136	echo "Usage: $0 { start | stop }"
137	exit 1
138	;;
139esac
140exit 0
141