#!/bin/ksh -p # # Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved. . /lib/svc/share/smf_include.sh function error { echo "$@" >&2 echo "$@" >/dev/console } function running_zones { zoneadm list -p | nawk -F: '$2 != "global" && $3 == "running" { print $2 }' } function wait_for_zones { typeset -i timeout=$1 while [ $timeout -gt 0 -a -n "$(running_zones)" ]; do ((timeout--)) sleep 1 done if [ -z "$(running_zones)" ]; then # # If we got here, there are no more running zones but we may # still be running poststate brand callbacks. So as to not race # with a subsequent start, wait for the backgrounded zoneadm # processes to complete. # wait exit $SMF_EXIT_OK fi } if [[ -z "$SMF_FMRI" ]]; then echo "this script can only be invoked by smf(5)" >&2 exit $SMF_EXIT_ERR_NOSMF fi # Make sure working directory is / to prevent unmounting problems. cd / PATH=/usr/sbin:/usr/bin; export PATH case "$1" in 'start') egrep -vs '^#|^global:' /etc/zones/index || exit 0 # no local zones # # Boot the installed zones for which the "autoboot" zone property is # set and invoke the sysboot hook for all other installed zones. # zones="" for zone in $(zoneadm list -pi | nawk -F: '{ if ($3 == "installed") { print $2 } }'); do zone_log="/var/log/zones/${zone}.messages" zone_sysboot="zoneadm -z $zone sysboot >> $zone_log 2>&1" zone_boot="zoneadm -z $zone boot >> $zone_log 2>&1" zonecfg -z $zone info autoboot | grep "true" >/dev/null 2>&1 if [ $? -eq 0 ]; then [[ -z "$zones" ]] && echo "Booting zones:\c" zones=yes echo " $zone\c" # # We create the processes doing sysboot and autoboot in # an abandoned contract so that the start method can # finish without waiting for these processes. # ctrun -l none sh -c "$zone_sysboot && $zone_boot" else ctrun -l none sh -c "$zone_sysboot" fi done [[ -n "$zones" ]] && echo . ;; 'stop') egrep -vs '^#|^global:' /etc/zones/index || exit 0 # no local zones [[ -z $(running_zones) ]] && exit 0 # no zones running svc_timeout=$2 shutdown_timeout=$((3 * $svc_timeout / 4)) halt_timeout=$((1 * $svc_timeout / 4)) echo "Shutting down zones (for up to ${shutdown_timeout}s): \c" echo "$(running_zones)" for zone in $(running_zones); do cmd=$(zonecfg -z $zone info autoshutdown 2>/dev/null | \ awk '{print $2}') if [[ -z "$cmd" ]]; then cmd="shutdown" fi zoneadm -z $zone $cmd & done wait_for_zones $((3 * $svc_timeout / 4)) error "One or more zones failed to shut down cleanly: $(running_zones)" echo "Halting zones (for up to ${halt_timeout}s): \c" echo "$(running_zones)" for zone in $(running_zones); do zoneadm -z $zone halt & done wait_for_zones $((1 * $svc_timeout / 4)) error "One or more zones failed to halt: $(running_zones)" umountall -Z exit $SMF_EXIT_ERR_FATAL ;; *) echo "Usage: $0 { start | stop }" exit $SMF_EXIT_ERR_CONFIG ;; esac exit $SMF_EXIT_OK