When rebooting OS X Lion (and forgetting to shutdown or suspend the vagrant VM manually), the VM fails to auto-suspend.
This causes the VM to be in the following state after reboot:
$ vagrant status
Current VM states:
default aborted
The VM is in an aborted state. This means that it was abruptly
stopped without properly closing the session. Run `vagrant up`
to resume this virtual machine. If any problems persist, you may
have to destroy and restart the virtual machine.
I'm not sure if this is a vagrant issue or a VirtualBox one.
I've made a simple workaround for this using OS X's logout hooks. If anyone else is interested, just follow the guide below:
Create the logout script (somewhere safe since logout hooks run as root):
sudo mkdir -p /usr/local/bin
sudo touch /usr/local/bin/logout.sh
sudo chmod u+x /usr/local/bin/logout.sh
Edit the file... for example:
#!/bin/sh
if [ "$1" = "USER_NAME" ]; then
su - "$1" -c "cd /path/to/vagrant-using-project; vagrant suspend"
fi
Enable the logout hook:
sudo defaults write com.apple.loginwindow LogoutHook /usr/local/bin/logout.sh
To disable the logout hook:
sudo defaults delete com.apple.loginwindow LogoutHook
@awbush,
Unfortunately VM's don't "auto-suspend." The "aborted" state means that the VM was not properly closed, which is definitely the case if you reboot/shut down the OS because VirtualBox doesn't know the correct state you'd like it to go in (suspended, shut down, etc.) since different guest OS's respond to this in different ways.
Vagrant itself maintains no presence other than when you execute a vagrant command, and then only for the duration for the Vagrant command. The reason I say this is because Vagrant itself has no way either of knowing when your OS is going to sleep or what behavior you'd like for it to have when it does go to sleep.
Your script is the way to do things.
Best,
Mitchell
@mitchellh,
Thanks for the response!
I understand Vagrant doesn't maintain a presence, but it does create the VirtualBox VM on our behalf. You mention that VirtualBox doesn't know the correct state we'd like the VM to go in... is there a way vagrant could tell VirtualBox (perhaps implicitly through settings on the VM that is created by Vagrant)? Or, is there a way to manually edit the VirtualBox VM after-the-fact so that VirtualBox knows this setting?
The ideal shutdown process should work like this:
Unfortunately this feature simply doesn't exist within VirtualBox. You're welcome to create a feature request ticket with VirtualBox but for Vagrant to attempt to support this (as much sense as it would make) would be a complete hack and be very OS-specific.
I agree completely: VirtualBox should support this first.
Thanks for taking the time to share knowledge on the feasibility of this feature.
I had the same problem using Ubuntu and just want to share my solution:
1) create a script "suspendVMs.sh" in /usr/local/bin with the following content:
#!/bin/bash
VAGRANT_EXE=/usr/bin/vagrant
su - <username> -c "cd <vagrant directory>; $VAGRANT_EXE suspend"
Of course, you can repeat the line starting with "su" for every VM you want to suspend at shutdown/reboot.
2) Make it executable
sudo chmod a+x /usr/local/bin/suspendVMs.sh
3) To integrate the suspend-script into your shutdown and reboot process, link the script into the appropriate rc
sudo ln -s /usr/local/bin/suspendVMs.sh /etc/rc0.d/K99suspend-vms
sudo ln -s /usr/local/bin/suspendVMs.sh /etc/rc6.d/K99suspend-vms
Thank you for sharing @awbush!
Hi,
the same thing started to happen to me when I put my mac to sleep. It happens quite often but not every time :(
This began when I updated vagrant and virtualbox to the latest stable versions (Vagrant 1.0.6 and VirtualBox 4.2.6).
Is this a "normal" behavior as well ?
Thanks.
Anyone have any workaround for this issue on windows ? (8?) - without having to manually halt boxes before each shutdown.
I just came across this thread as my Virtualbox was in an aborted state when I came back in to work.
Oddly, I had manually run vagrant halt prior to shutting the host machine down. Admittedly I didn't check the box status, but the halt command had completed with no errors. So a little confused here since most people seem to suggest shutting the guests down prior to the host to stop this happening, yet that seems to have been the cause of it?
I'm running Windows 7 64-bit with Vagrant 1.6.5 and Virtualbox 4.3.12.
Steve
Well, since then, I gave up and make my own auto-shutdown tool: http://blog.ionelmc.ro/2014/01/04/virtualbox-vm-auto-shutdown/
I'm pretty sure vagrant could do something similar.
@PSeiffert
Within Linux Hosts i use this:
/etc/default/virtualbox
.sh
SHUTDOWN_USERS="<YOUR_USERNAME>"
SHUTDOWN=savestate
for osx users:
its in php, but should be installed out of the box
``` .php
// sudo defaults write com.apple.loginwindow LogoutHook /usr/local/bin/vagrant-suspend
$user = exec('stat -f \'%Su\' /dev/console');
echo sprintf('User: %s', $user);
$output = array();
exec(sprintf('sudo -u %s VBoxManage list runningvms', $user), $output);
foreach($output as $line) {
$matches = array();
if(preg_match('/{([^}]+)}/', $line, $matches) !== 1) {
throw new \Exception(sprintf('Can\'t parse the following line: %s', $line));
}
$command = sprintf('sudo -u %s VBoxManage controlvm %s savestate', $user, $matches[1]);
echo $command . "\n";
exec($command);
echo 'done' . "\n";
}
```
Most helpful comment
@awbush,
Unfortunately VM's don't "auto-suspend." The "aborted" state means that the VM was not properly closed, which is definitely the case if you reboot/shut down the OS because VirtualBox doesn't know the correct state you'd like it to go in (suspended, shut down, etc.) since different guest OS's respond to this in different ways.
Vagrant itself maintains no presence other than when you execute a
vagrantcommand, and then only for the duration for the Vagrant command. The reason I say this is because Vagrant itself has no way either of knowing when your OS is going to sleep or what behavior you'd like for it to have when it does go to sleep.Your script is the way to do things.
Best,
Mitchell