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