I use vagrant for development. I forget to shut down a few of the VMs. When I go to log out of my host machine, the Ubuntu shutdown process appears to hang.
Might there be a way to script a close of all vagrant boxes with a bit of commandline-fu? Something like the following, but something that, well, works.
for f in $HOME/vagrant;
do;
cd $f
vagrant halt
done;
For a scriptable control of Virtual Box machines we can make use of the VBoxManage commands:
List running machines (returns name and UUID):
Stop running VMs by "hibernating" them (reommended to avoid data loss)
Poweroff running VMs (not recommended because we may lose data in the guest)
Use ACPI in an ACPI-aware guest OS (preferable to
poweroff
for graceful shutdown of guests)Also see: How to safely shutdown Guest OS in VirtualBox using command line
Update from OP
Based on this selected correct answer below, I've added this bash script "
$HOME/bin/stop-vagrant.sh
". So now I have something that can safely begin a stop of all vagrant VMs that I might have turned on yet forgotten about in a session.Command Explained:
vboxmanage list runningvms |
-- gets a list of all running vms under VirtualBoxsed -r 's/.*\{(.*)\}/\1/' |
-- strips the string down to id numberxargs -L1 -I {} VBoxManage controlvm {} savestate
-- runs the save state command on each box that's open.On
xargs
-L1
- take one line at a time-I {}
- uses {} as a place holder for the next commandThe other answer is great for handling Virtualbox, but Vagrant features its own mechanisms for handling Virtual Machines, and as was mentioned in one of the comments, it supports more than just VirtualBox, just VMWare at the moment, but who knows later!
This seems to work for me:
Note:
This works with Vagrant versions after 1.6, for older versions, you should probably upgrade, but if you can't, one of the other options which focuses on Virtualbox may be better.
My mechanism for this:
vagrant global-status | grep virtualbox | cut -c 1-9 | while read line; do echo $line; vagrant halt $line; done;
virtualbox
(Filters out the help text, will break if you're using some other provider)vagrant halt $line
halting the vagrant for that global unique IDThis is better than the Virtualbox method above, because it'll run any vagrant-configured shutdown mechanisms too.
Combining some of the other answers, this will close down all running virtualbox vagrant boxes:
In case other people get to this question: For those using VirtualBox, it already can take care of this, only involves editing a file:
The upside is that is not necessary to edit/create any logout or
init.d
stript to run the commands posted in the other answers. The downside is this solution is specific to VirtualBox.Tested on Ubuntu 14.10 with VirtualBox 4.3.18.
All credit goes to this post.
I just use
vagrant halt
. If you run it without a further argument, it stops all the machines defined in theVagrantfile
.If you're writing scripts to parse Vagrant commands, it's advised to parse machine-friendly output (
--machine-readable
) which is more consistent.The format is:
so you can import it as CSV file, since it's comma-separated.
With shell, it's probably more difficult to parse, for example:
See: Vagrant - Machine readable output
However I find it easier to parse the standard output, e.g.
Btw. Theoretically
vagrant
command should accept a regular expression for the list of VMs to suspend as per this GH post, for example:but it doesn't work and there is a bug #7221 which is pending in order to fix it.
Related GitHub tickets:
This may or may not work for you ;-) Works for me
vagrant_halt_all.sh
Here is a compilation of working one-liners as of September 2020. Thanks to all the previous authors for inspiration.
Halt all vagrant boxes
Version 1 (shorter)
Version 2 (more robust)
macOS/BSD compatible:
GNU compaible:
Alias
You can add this to an alias for quick execution. Add this code to
~/.bashrc
,~/.zshrc
or your shell's equivalent.macOS/BSD example:
Suspend all vagrant boxes
Or if you want to
suspend
any running machines, the syntax is easy:I recommend using
halt
for web servers; it saves disk space and may be faster.Because
vagrant halt
requires VM metadata (you run it in the directory with the fileVagrantfile
& the directory.vagrant
) it cannot be used easily to halt all VMs.In some cases it is enough to halt all VBoxes: