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