Guest User

Untitled

a guest
Dec 15th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. VBoxManage controlvm <uuid>|<name>
  2. pause|resume|reset|poweroff|savestate|
  3. acpipowerbutton|acpisleepbutton|
  4.  
  5. #!/bin/bash
  6. if [ -z "$1" ]; then
  7. echo "Usage: $0 VMNAME_or_UUID"
  8. exit 1
  9. fi
  10. set -x
  11. VBoxManage controlvm "$1" poweroff #enforce turnoff
  12. VBoxManage snapshot "$1" restorecurrent #retore state
  13. VBoxManage showvminfo "$1" | grep State #display state to ensure
  14. VBoxHeadless -s "$1" #run in headless mode in background
  15.  
  16. [Unit]
  17. Description=VBox Virtual Machine %i Service
  18. Requires=systemd-modules-load.service
  19. After=systemd-modules-load.service
  20.  
  21. [Service]
  22. User=user
  23. Group=vboxusers
  24. ExecStart=/usr/bin/VBoxHeadless -s %i
  25. ExecStop=/usr/bin/VBoxManage controlvm %i savestate
  26.  
  27. [Install]
  28. WantedBy=multi-user.target
  29.  
  30. $ sudo systemctl enable vboxvmservice@vm_name.service
  31.  
  32. ...
  33. start(){
  34. su -c "VBoxHeadless --startvm thevm" -s /bin/bash theuser &
  35. # maybe another vbox command
  36. }
  37.  
  38. stop(){
  39. su -c "VBoxManage controlvm thevm acpipowerbutton" -s /bin/bash theuser
  40. # maybe another vbox command
  41. while [ "`su -c 'VBoxManage list runningvms' -s /bin/bash theuser`" != "" ]
  42. do
  43. echo waiting for VMs to shutdown
  44. sleep 3
  45. done
  46. }
  47. ...
  48.  
  49. VBoxManage startvm VMNAME --type headless
  50.  
  51. VBoxManage controlvm VMNAME savestate
  52.  
  53. VBoxManage list runningvms
  54.  
  55. VBoxManage list runningvms | tr -s '" {' '%{' | cut -d '%' -f3 | while read uuid; do
  56. VBoxManage controlvm $uuid savestate;
  57. done
Add Comment
Please, Sign In to add comment