svc-zones revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# CDDL HEADER START
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# The contents of this file are subject to the terms of the
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# Common Development and Distribution License (the "License").
027e89d47af308db4b41761ca9f847c026b63ec8Andreas Gustafsson# You may not use this file except in compliance with the License.
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# See the License for the specific language governing permissions
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# and limitations under the License.
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# When distributing Covered Code, include this CDDL HEADER in each
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# If applicable, add the following below this CDDL HEADER, with the
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# fields enclosed by brackets "[]" replaced with your own identifying
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# information: Portions Copyright [yyyy] [name of copyright owner]
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# CDDL HEADER END
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# Copyright 2006 Sun Microsystems, Inc. All rights reserved.
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# Use is subject to license terms.
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# ident "%Z%%M% %I% %E% SMI"
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# Return a list of running, non-global zones for which a shutdown via
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# "/sbin/init 0" may work (typically only Solaris zones.)
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# At present, this means any running "lx" zones don't qualify.
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce if (($5 != "lx") && ($2 != "global")) {
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce[ ! -x /usr/sbin/zoneadm ] && exit 0 # SUNWzoneu not installed
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luceif [ -z "$SMF_FMRI" ]; then
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce echo "this script can only be invoked by smf(5)"
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce# Make sure working directory is / to prevent unmounting problems.
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce egrep -vs '^#|^global:' /etc/zones/index || exit 0 # no local zones
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce if ($3 == "installed") {
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce zonecfg -z $zone info autoboot | grep "true" >/dev/null 2>&1
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce # zoneadmd puts itself into its own contract so
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce # this service will lose sight of it. We don't
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce # support restart so it is OK for zoneadmd to
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce # to be in an orphaned contract.
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce # Wait for all zoneadm processes to finish before allowing the
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce # start method to exit.
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce [ -n "$ZONES" ] && echo .
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce egrep -vs '^#|^global:' /etc/zones/index || exit 0 # no local zones
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce [ "`zoneadm list`" = "global" ] && exit 0 # no zones running
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce SVC_TIMEOUT=`svcprop -p stop/timeout_seconds $SMF_FMRI`
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce # First, try shutting down any running zones for which an "init 0" may
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce MAXSHUT=`expr 3 \* $SVC_TIMEOUT \/ 4` # 3/4 of time to zone shutdown
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce MAXHALT=`expr $SVC_TIMEOUT \/ 4` # rest of time goes to halt
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce if [ -n "$zonelist" ]; then
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce echo "Shutting down running zones (for up to $MAXSHUT" \
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce "seconds):\c"
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce zlogin -S $zone /sbin/init 0 < /dev/null >&0 2>&0 &
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce # Allow time for zones to shutdown cleanly
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce while [ $MAXSHUT -gt 0 -a "`shutdown_zones`" != "" ]; do
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce # Second, try halting any non-global zones still running
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce echo "Zones failed to shutdown; trying to halt " \
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce "(for up to $MAXHALT seconds):\c"
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce [ ! -z "$WAITPIDS" ] && echo .
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce # Wait for the 'zoneadm halt' commands to complete. We will let this
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce # run forever, since the restart daemon will eventually kill us off
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce # anyway if the halts do not complete after a certain period of time.
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce # If the halts complete but a zone is still not shutdown, it might
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce # be in a state like 'shutting_down' or 'down'. So we give it some
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce # time to come all the way down.
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce while [ $MAXHALT -gt 0 -a "`zoneadm list`" != "global" ]; do
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce # Report on zones which failed to shutdown.
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce echo "Zone '$zone' failed to halt."
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce [ "`zoneadm list`" != "global" ] && exit 1 # zones still running
f293a69bcd1c1dd7bdac8f4102fc2398b9e475c8Eric Luce echo "Usage: $0 { start | stop }"