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