Virtualbox Windows graceful shutdown of guests on host shutdown

39,478

Solution 1

Sorry I'm late to the party. There is an exact answer to this, though it requires some commandline-foo. See this thread post for more information: https://forums.virtualbox.org/viewtopic.php?f=6&t=53684#p285540

The command you are looking for is:

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" setextradata "VM NAME" GUI/DefaultCloseAction Shutdown

This is what I use on multiple VMs, close the window and it starts a safe shutdown automatically. Shutdown Windows, and in its attempt to close everything, it will wait for these processes to complete.

Solution 2

I had a similar problem and solved it by running VirtualBox as a service:

https://github.com/onlyfang/VBoxVmService

With VBoxVMService you can chose how you want the machine to shutdown (Save state, power off) and startup. Since it's running as a service, Windows will automatically wait for it to shutdown, during the system shutdown process.

Solution 3

Unfortunately that doesn't seem to be possible for VMs started via VirtualBox GUI. Even though the GUI might catch the host shutdown event and react, the VirtualBox service gets terminated: https://forums.virtualbox.org/viewtopic.php?p=278668#p278668

If you don't need a graphical console, VBoxHeadlessTray or VBoxVMService might be the way to go. Both support automatic save and resume on Windows host shutdown and restart.

VirtualBox 5.0 introduces a "detachable UI" start mode. This mode starts a headless VM with a separate UI process. Graphical performance suffers though and 3D acceleration isn't supported yet. But maybe this can be combined with VBoxHeadlessTray in the future (VBoxHeadlessTray doesn't support 5.0 yet.) Links to VBoxHeadlessTray GitHub repository and to the corresponding GitHub pull request for adding VirtualBox 5 support.

Edit: VBoxVmService also doesn't support the new detachable mode as of version 5.0. Only headless so far. I added a feature request for that.

Solution 4

I have 3 batch scripts which I use instead of the startmenu power buttons.

do_shutdown.bat (shutdown pc with a 10 sec waiting period, not to give the vm's 10 sec time to savestate, but to allow me to cancel the shutdown within 10 seconds. The countdown starts after the vm's have been shut down)

"C:\VirtualBox\VBoxManage.exe" controlvm "Ubuntu Server" savestate
"C:\VirtualBox\VBoxManage.exe" controlvm "Ubuntu Minimal" savestate
shutdown /s /t 10

do_reboot.bat (reboots immediately after the vm's got shut down)

"C:\VirtualBox\VBoxManage.exe" controlvm "Ubuntu Server" savestate
"C:\VirtualBox\VBoxManage.exe" controlvm "Ubuntu Minimal" savestate
shutdown /r /t 0

do_cancel.bat (allows me to cancel the pc-shutdown within the 10 second waiting period. It then restarts the vm's again, since they got shut down with the do_shutdown.bat)

shutdown /a
C:\VirtualBox\VBoxManage.exe startvm "Ubuntu Minimal" --type headless
C:\VirtualBox\VBoxManage.exe startvm "Ubuntu Server" --type headless

Instead of savestate you can also use one of the following

poweroff        - pulls the plug
                  (probably not a good idea...)

acpipowerbutton - presses the power off button for a clean shutdown
                  ("The system is going down for power off NOW!" to all consoles)

acpisleepbutton - tells the os to go to sleep
                  (probably just as bad as poweroff)

Solution 5

I had a similar question and found this page. I don't want to run VirtualBox as a service, because I have a lot of VMs for testing, and usually pick different ones to run in the VirtualBox UI. When I shutdown my computer, it is annoying to manually save the state of each VM. Using scripts to save all running VMs seems to be a practical solution in this case. To make the answer of Daniel F more general, I wrote these scripts that automatically save the state of all running VMs without naming them explicitly.

saveRunningVMs.bat for Windows:

set VBoxManageEXE="%ProgramFiles%\Oracle\VirtualBox\VBoxManage.exe"
set ListRunningVMS=%VboxManageEXE% list runningvms
for /f tokens^=2^,4^ delims^=^" %%p in ('%ListRunningVMS%') do %VBoxManageEXE% controlvm %%p savestate

echo all vms saved, you can shutdown now.

rem shutdown /s /t 10

saveRunningVMs.sh for Linux:

#!/bin/bash
vboxmanage list runningvms | while read line; do
  #echo "VBoxManage controlvm $uuid savestate;"
  echo $line
  if [[ $line =~ \{(.*)\} ]]
  then
    vboxmanage controlvm ${BASH_REMATCH[1]} savestate
  fi
done
Share:
39,478

Related videos on Youtube

Matt Jenkins
Author by

Matt Jenkins

Updated on September 18, 2022

Comments

  • Matt Jenkins
    Matt Jenkins over 1 year

    I am trying to find a solution to gracefully shutdown any guest VMs running under VirtualBox Windows when the host computer is shut down or restarted.

    It seems that the safest option would be to trigger the "save state" command when the host starts shutting down, but it is not clear whether the host will wait long enough for the VMs to finish saving state and power off.

    Does anyone have a robust solution for this (seemingly basic) issue?

    • Canadian Luke
      Canadian Luke over 8 years
      Install the guest additions? Does that not provide the functionality required?
    • Matt Jenkins
      Matt Jenkins over 8 years
      @CanadianLuke It does not. :)
  • Leo B
    Leo B over 8 years
    As I am not allowed to port more than two links per post, here are the links to VBoxHeadlessTray and to the corresponding GitHub pull request for adding VirtualBox 5 support.
  • Matt Jenkins
    Matt Jenkins over 8 years
    Thanks, this is interesting. Unfortunately, there are other non-manual shutdown/reboot scenarios that I need to handle as well. For example, restarts scheduled by Windows Update, or low battery power off/shutdown events.
  • Daniel F
    Daniel F over 8 years
    Oh, ok. There's also the Group Policy Editor with the "Scripts (Startup/Shutdown)" section lifehacker.com/… I'm using that one for a very short command on shutdown (one curl call), so I don't know how it behaves on scripts that take a while to complete.
  • iuradz
    iuradz about 7 years
    The setting up process is not so automatic on Windows 10. I have to refer to the trouble shooting to see what's wrong. However, after configured correctely, this software does exactly what I need. Thank you for you great work.
  • Kris Bahnsen
    Kris Bahnsen about 6 years
    I do not experience the issues outlined in the thread. See my answer on this topic. I can run Windows host shutdown and walk away. Shutdown blocks until all of my VMs are closed, and I've changed the default action to do a clean shutdown or a save state.
  • rakslice
    rakslice almost 6 years
    Others' suggestion of using a batch script you can run manually that does the savestates and then does the shut down is great if that fits your use case. But what I really want this for is Windows Update automatic reboots, after a VM I've been working with has been hard powered off overnight by Windows Update restarts for the past two days in a row...
  • Cammy Boy
    Cammy Boy about 4 years
    Thanks! Since OP was looking for save state, the answer is SaveState instead of Shutdown.
  • Vomit IT - Chunky Mess Style
    Vomit IT - Chunky Mess Style almost 4 years
    You should post full solution content, not just a link to the 'more detailed' content.
  • Juan Ignacio Barisich
    Juan Ignacio Barisich over 3 years
    Thank you! Works for me
  • Marc Novakowski
    Marc Novakowski over 2 years
    Case in point - the website link doesn't work anymore. :(