smartos-guesthandler/svc-guesthandler

84 lines
2.0 KiB
Plaintext
Raw Normal View History

2016-04-19 05:48:57 +00:00
#!/sbin/sh
# implement ordered VM start-up and shutdown on SmartOS | tamas@gerczei.eu
2016-04-18 14:33:06 +00:00
# PREREQUISITE: 'vmadm update $UUID <<< "{\"set_tags\": {\"priority\": 0}}"' where 0 means 'OFF', the rest determines ascending and descending numerical order respectively
. /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
2016-04-18 14:33:06 +00:00
fi
ME=$(basename ${0#svc-})
2016-04-18 14:33:06 +00:00
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
2016-04-19 05:48:57 +00:00
ORDER=$(vmadm lookup -j -o uuid,tags | json -c 'this.tags.priority > 0' -a uuid tags.priority | sort -nk2 | cut -d " " -f1)
log start-up order determined as: $ORDER
2016-04-18 14:33:06 +00:00
# start guests
2016-04-19 05:48:57 +00:00
for UUID in $ORDER
2016-04-18 14:33:06 +00:00
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}
2016-04-18 14:33:06 +00:00
else
# failed to start guest
log $UUID failed to $1
fi
done
;;
stop)
# determine the order of VMs by reverse boot priority
2016-04-19 05:48:57 +00:00
ORDER=$(vmadm lookup -j -o uuid,tags state=running | json -a uuid tags.priority | sort -rnk2 | cut -d " " -f1)
log shutdown order determined as: $ORDER
2016-04-18 14:33:06 +00:00
# stop guests
2016-04-19 05:48:57 +00:00
for UUID in $ORDER
2016-04-18 14:33:06 +00:00
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
;;
disarm)
# set the 'autoboot' attribute to 'false' for every installed guest on 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
fi
done
log disarmed: set autoboot=false on all zones
;;
*)
if smf_present;
then
exit $SMF_EXIT_ERR_CONFIG;
else
print
2016-04-18 14:33:06 +00:00
esac
exit $SMF_EXIT_NODAEMON