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