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.

vboxinit 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/bash
  2. #
  3. # vboxinit: auto start sessions when booting and save
  4. # sessions when host is stopped
  5. #
  6. # Based on vboxtool. Only tested in Debian.
  7. #
  8. # Debian install:
  9. # copy this script to /etc/init.d
  10. # run:
  11. # chmod u+rx /etc/init.d/vboxinit
  12. # update-rc.d vboxinit defaults
  13. ### BEGIN INIT INFO
  14. # Provides: vboxinit
  15. # Required-Start: vboxdrv $local_fs
  16. # Required-Stop: vboxdrv $local_fs
  17. # Default-Start: 2 3 4 5
  18. # Default-Stop: 0 1 6
  19. # Description: Controls VirtualBox sessions
  20. ### END INIT INFO
  21. . /etc/default/virtualbox
  22. # Enable/disable service
  23. if [ "${VBOXWEB_USER}" == "" ]; then
  24. exit 0
  25. fi
  26. # Check for VirtualBox binary path
  27. if [ "$VBOX_BIN_PATH" != "" ]; then
  28. PATH = "$PATH:$VBOX_BIN_PATH";
  29. fi
  30. start()
  31. {
  32. # Get all autostart machines
  33. MACHINES=$($su_command "VBoxManage list vms | awk '{ print \$NF }' | sed -e 's/[{}]//g'")
  34. for UUID in $MACHINES; do
  35. STARTUP=$($su_command "VBoxManage getextradata $UUID 'pvbx/startupMode'" | awk '{ print $NF }')
  36. if [ "${STARTUP}" == "auto" ]; then
  37. VMNAME=$($su_command "VBoxManage showvminfo $UUID | sed -n '0,/^Name:/s/^Name:[ \t]*//p'")
  38. echo "$0: starting machine ${VMNAME} ..."
  39. $su_command "VBoxManage startvm $UUID --type headless" >>/var/log/vb.log
  40. fi
  41. done
  42. }
  43. stop()
  44. {
  45. # vms are saved, instead of stopped.
  46. MACHINES=$($su_command "VBoxManage list runningvms | awk '{ print \$NF }' | sed -e 's/[{}]//g'")
  47. for UUID in $MACHINES; do
  48. VMNAME=$($su_command "VBoxManage showvminfo $UUID | sed -n '0,/^Name:/s/^Name:[ \t]*//p'")
  49. echo "$0: saving machine ${VMNAME} state ..."
  50. $su_command "VBoxManage controlvm $UUID savestate" >>/var/log/vb.log
  51. done
  52. }
  53. status()
  54. {
  55. $su_command "VBoxManage list runningvms"
  56. }
  57. restart()
  58. {
  59. stop
  60. start
  61. }
  62. # Implementation of user control, execute several commands as another (predefined) user,
  63. su_command="su - ${VBOXWEB_USER} -s /bin/bash -c"
  64. #
  65. # Check for a command line option
  66. #
  67. case "$1" in
  68. start)
  69. start
  70. ;;
  71. stop)
  72. stop
  73. ;;
  74. status)
  75. status
  76. ;;
  77. restart)
  78. restart
  79. ;;
  80. *)
  81. echo "Usage: $0 {start|stop|restart|status}"
  82. exit 1
  83. ;;
  84. esac
  85. exit 0