Ordered guest boot-up and shutdown sequences for SmartOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

91 lines
2.1 KiB

#!/sbin/sh
# implement ordered VM start-up and shutdown on SmartOS | tamas@gerczei.eu
. /lib/svc/share/smf_include.sh
if [ -z $SMF_FMRI ];
then
print "this script can only be invoked by smf(5)"
exit $SMF_EXIT_ERR_NOSMF
fi
ME=${0##*/svc-}
function log {
# helper function to log arbitrary messages via syslog
logger -p daemon.notice -t $ME $@
}
case $1 in
start)
# determine the order of VMs by boot priority
ORDER=$(vmadm list -Ho uuid,tags.priority -s tags.priority tags.priority='~[^0]' | cut -d " " -f1)
log start-up order determined as: $ORDER
# start guests
for UUID in $ORDER
do
# invoke vmadm
vmadm start $UUID 2> /dev/null
if [ $? -eq 0 ]
then
# successful start, log and wait
log $UUID managed to $1
sleep ${DELAY:-0}
else
# failed to start guest
log $UUID failed to $1
fi
done
;;
stop)
# determine the order of VMs by reverse boot priority
ORDER=$(vmadm list -Ho uuid,tags.priority -s -tags.priority state=running | cut -d " " -f1)
log shutdown order determined as: $ORDER
# stop guests
for UUID in $ORDER
do
# invoke vmadm
vmadm stop $UUID 2> /dev/null
if [ $? -eq 0 ]
then
# successful stop
log $UUID managed to $1
else
# failed to stop guest
log $UUID failed to $1
fi
done
# grant the guests some time to shut down
WAIT_UNTIL=$(($(date +%s) + ${GRACE_PERIOD:-300}))
while [ $(zoneadm list | wc -l) -gt 1 ];
do
if [ $(date +%s) -le $WAIT_UNTIL ];
then
sleep ${WAIT_INTERVAL:-10}
else
exit $SMF_EXIT_ERR_FATAL
fi
done
;;
disarm)
# set the 'autoboot' attribute to 'false' for every installed guest in order to prevent automatic start-up without this script
zoneadm list -pi | while IFS=":" read ID UUID STATE remainder; do
if [[ $STATE == "installed" ]];
then
zonecfg -z $UUID 'set autoboot=false; select attr name=vm-autoboot; set value=false; end; verify; commit; exit'
fi
done
log disarmed: set autoboot=false on all zones
;;
*)
exit $SMF_EXIT_ERR_CONFIG
;;
esac
exit $SMF_EXIT_NODAEMON