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