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.

92 lines
2.1 KiB

  1. #!/sbin/sh
  2. # implement ordered VM start-up and shutdown on SmartOS | tamas@gerczei.eu
  3. . /lib/svc/share/smf_include.sh
  4. if [ -z $SMF_FMRI ];
  5. then
  6. print "this script can only be invoked by smf(5)"
  7. exit $SMF_EXIT_ERR_NOSMF
  8. fi
  9. ME=${0##*/svc-}
  10. function log {
  11. # helper function to log arbitrary messages via syslog
  12. logger -p daemon.notice -t $ME $@
  13. }
  14. case $1 in
  15. start)
  16. # determine the order of VMs by boot priority
  17. ORDER=$(vmadm list -Ho uuid,tags.priority -s tags.priority tags.priority='~[^0]' | cut -d " " -f1)
  18. log start-up order determined as: $ORDER
  19. # start guests
  20. for UUID in $ORDER
  21. do
  22. # invoke vmadm
  23. vmadm start $UUID 2> /dev/null
  24. if [ $? -eq 0 ]
  25. then
  26. # successful start, log and wait
  27. log $UUID managed to $1
  28. sleep ${DELAY:-0}
  29. else
  30. # failed to start guest
  31. log $UUID failed to $1
  32. fi
  33. done
  34. ;;
  35. stop)
  36. # determine the order of VMs by reverse boot priority
  37. ORDER=$(vmadm list -Ho uuid,tags.priority -s -tags.priority state=running | cut -d " " -f1)
  38. log shutdown order determined as: $ORDER
  39. # stop guests
  40. for UUID in $ORDER
  41. do
  42. # invoke vmadm
  43. vmadm stop $UUID 2> /dev/null
  44. if [ $? -eq 0 ]
  45. then
  46. # successful stop
  47. log $UUID managed to $1
  48. else
  49. # failed to stop guest
  50. log $UUID failed to $1
  51. fi
  52. done
  53. # grant the guests some time to shut down
  54. WAIT_UNTIL=$(($(date +%s) + ${GRACE_PERIOD:-300}))
  55. while [ $(zoneadm list | wc -l) -gt 1 ];
  56. do
  57. if [ $(date +%s) -le $WAIT_UNTIL ];
  58. then
  59. sleep ${WAIT_INTERVAL:-10}
  60. else
  61. exit $SMF_EXIT_ERR_FATAL
  62. fi
  63. done
  64. ;;
  65. disarm)
  66. # set the 'autoboot' attribute to 'false' for every installed guest in order to prevent automatic start-up without this script
  67. zoneadm list -pi | while IFS=":" read ID UUID STATE remainder; do
  68. if [[ $STATE == "installed" ]];
  69. then
  70. zonecfg -z $UUID 'set autoboot=false; select attr name=vm-autoboot; set value=false; end; verify; commit; exit'
  71. fi
  72. done
  73. log disarmed: set autoboot=false on all zones
  74. ;;
  75. *)
  76. exit $SMF_EXIT_ERR_CONFIG
  77. ;;
  78. esac
  79. exit $SMF_EXIT_NODAEMON