xref: /titanic_52/usr/src/cmd/zoneadm/svc-zones (revision e12a8a13c5492eeed938960ff7c68a46f982288b)
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. /lib/svc/share/smf_include.sh
29
30[ ! -x /usr/sbin/zoneadm ] && exit 0	# SUNWzoneu not installed
31
32if [ -z "$SMF_FMRI" ]; then
33	echo "this script can only be invoked by smf(5)"
34	exit $SMF_EXIT_ERR_NOSMF
35fi
36
37# Make sure working directory is / to prevent unmounting problems.
38cd /
39PATH=/usr/sbin:/usr/bin; export PATH
40
41case "$1" in
42'start')
43	egrep -vs '^#|^global:' /etc/zones/index || exit 0  # no local zones
44	ZONES=""
45	for zone in `zoneadm list -pi | nawk -F: '{
46			if ($3 == "installed") {
47				print $2
48			}
49		}'`; do
50		zonecfg -z $zone info autoboot | grep "true" >/dev/null 2>&1
51		if [ $? -eq 0 ]; then
52			[ -z "$ZONES" ] && echo "Booting zones:\c"
53			ZONES=yes
54			echo " $zone\c"
55			#
56			# zoneadmd puts itself into its own contract so
57			# this service will lose sight of it.  We don't
58			# support restart so it is OK for zoneadmd to
59			# to be in an orphaned contract.
60			#
61			zoneadm -z $zone boot &
62		fi
63	done
64	#
65	# Wait for all zoneadm processes to finish before allowing the
66	# start method to exit.
67	#
68	wait
69	[ -n "$ZONES" ] && echo .
70	;;
71
72'stop')
73	egrep -vs '^#|^global:' /etc/zones/index || exit 0  # no local zones
74	[ "`zoneadm list`" = "global" ] && exit 0   # no zones running
75
76	SVC_TIMEOUT=`svcprop -p stop/timeout_seconds $SMF_FMRI`
77
78	MAXSHUT=`expr 3 \* $SVC_TIMEOUT \/ 4` # 3/4 of time to zone shutdown
79	MAXHALT=`expr $SVC_TIMEOUT \/ 4`      # rest of time goes to halt
80
81	echo "Shutting down running zones (for up to $MAXSHUT seconds):\c"
82
83	# First, try letting them run their shutdown scripts.
84
85	SHUTDOWN=0
86	for zone in `zoneadm list`; do
87		if [ "$zone" != "global" ]; then
88			echo " $zone\c"
89			zlogin -S $zone /sbin/init 0 < /dev/null >&0 2>&0 &
90			SHUTDOWN=1
91		fi
92	done
93	[ $SHUTDOWN -eq 1 ] && echo "."
94
95	# Allow time for zones to shutdown cleanly
96
97	while [ $MAXSHUT -gt 0 -a "`zoneadm list`" != "global" ]; do
98		MAXSHUT=`expr $MAXSHUT - 1`
99		sleep 1	# wait a bit longer
100	done
101
102	# Second, try halting them.
103
104	WAITPIDS=""
105	for zone in `zoneadm list`; do
106		if [ "$zone" != "global" ]; then
107			[ -z "$WAITPIDS" ] &&
108			    echo "Zones failed to shutdown; trying to halt " \
109			    "(for up to $MAXHALT seconds):\c"
110			echo " $zone\c"
111			zoneadm -z $zone halt &
112			WAITPIDS="$WAITPIDS $!"
113		fi
114	done
115	[ ! -z "$WAITPIDS" ] && echo .
116
117	# Wait for the 'zoneadm halt' commands to complete.  We will let this
118	# run forever, since the restart daemon will eventually kill us off
119	# anyway if the halts do not complete after a certain period of time.
120	wait $WAITPIDS
121
122	# If the halts complete but a zone is still not shutdown, it might
123	# be in a state like 'shutting_down' or 'down'.  So we give it some
124	# time to come all the way down.
125	while [ $MAXHALT -gt 0 -a "`zoneadm list`" != "global" ]; do
126		MAXHALT=`expr $MAXHALT - 1`
127		sleep 1	# wait a bit longer
128	done
129
130	#
131	# Report on zones which failed to shutdown.
132	#
133	for zone in `zoneadm list`; do
134		if [ "$zone" != "global" ]; then
135			echo "Zone '$zone' failed to halt."
136		fi
137	done
138	[ "`zoneadm list`" != "global" ] && exit 1   # zones still running
139	;;
140
141*)
142	echo "Usage: $0 { start | stop }"
143	exit 1
144	;;
145esac
146exit 0
147