#!/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=$(basename $0) ME=${ME#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 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 # 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 lookup -j -o uuid,tags state=running | json -a uuid tags.priority | sort -rnk2 | 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 ;; 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