So I've decided to really try Vagrant again - there's just this annoyance that makes it very inconvenient: on Windows, VirtualBox doesn't graciously shutdown your VMs. Logged off ? Aborted VM. Shutdown in rush ? You probably forgot to take care of those VMs. Want to worder every time about why some service doesn't start at boot ? Want to recover that mongo database every time you forgot to sudo shutdown or vagrant halt ? I certainly do not.
So the plan is to create some tool to make those VMs get a nice, gracious ACPI shutdown. And if ACPI shutdown not possible then suspend them.
I've tried to make logoff scripts to run vagrant halt but it appears that by the time the script is run the VMs are already killed. Bummer.
Because the VMs are started by Vagrant under the user's account we cannot use any Windows Service based solution .
So, the plan*
Is going to be like this:
- Have some sort of process that runs in the background.
- Have a notification area icon (to see that it's running and maybe disable it).
- When process is sent WM_ENDSESSION, WM_QUERYENDSESSION, WM_QUIT, WM_CLOSE or anything that might look like a logoff/shutdown event then shamelessly block and patiently wait for all the VMs to shutdown.
- Make the process get the close messages first (set priority using SetProcessShutdownParameters).
- For each VM the shutdown process is going to be like this:
- Unpause if VM paused.
- Send ACPI shutdown signal, wait for a state change for 10 seconds
- If machine still running then suspend it, wait again till it changes state.
I've implemented it here - give it shot !
Here's what you need to run it:
- Download vbox-shutdown.py somewhere there are write permissions (it will create a logfile unless you specify it a different path).
- Python 2.7 with same architecture as VirtualBox - if you have 64bit system most probably you'll need a 64-bit Python (I've used the COM api - it doesn't allow cross arch interop for now ). This could change by using the command line API if anyone really wanted this. Anyone can fix it - it's just some Python code.
- Install pywin32 - same architecture/reason as above. 
- Install the vboxapi package. Running python vboxapisetup.py install in c:\Program Files\Oracle\VirtualBox\sdk\install\ did the trick for me. I wonder why they have this useless pypi package with no distributions ...
Now just run shell:startup and put the file in there.
I've tested this on Windows 8.1 but there's no reason it wouldn't run on older versions (or be something that cannot be fixed easily).