76

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;
Rick
  • 2,847

11 Answers11

102

For a scriptable control of Virtual Box machines we can make use of the VBoxManage commands:

  • List running machines (returns name and UUID):

    VBoxManage list runningvms
    
  • Stop running VMs by "hibernating" them (reommended to avoid data loss)

    VBoxManage controlvm <name|uuid> savestate
    
  • Poweroff running VMs (not recommended because we may lose data in the guest)

    VBoxManage controlvm <name|uuid> poweroff
    
  • Use ACPI in an ACPI-aware guest OS (preferable to poweroff for graceful shutdown of guests)

    VBoxManage controlvm <name|uuid> acpipowerbutton
    

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.

vboxmanage list runningvms | sed -r 's/.*\{(.*)\}/\1/' | xargs -L1 -I {} VBoxManage controlvm {} savestate

Command Explained:

vboxmanage list runningvms | -- gets a list of all running vms under VirtualBox

sed -r 's/.*\{(.*)\}/\1/' | -- strips the string down to id number

xargs -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 command
Takkat
  • 142,284
26

The 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:

vagrant global-status | awk '/running/{print $1}' | xargs -r -d '\n' -n 1 -- vagrant suspend

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.

ThomasRedstone
  • 361
  • 3
  • 3
13

My mechanism for this:

vagrant global-status | grep virtualbox | cut -c 1-9 | while read line; do echo $line; vagrant halt $line; done;

  • global-status lists all boxes
  • filter that for lines containing virtualbox (Filters out the help text, will break if you're using some other provider)
  • Filter that to display only the first 9 characters (the global unique ID)
  • While we can still read a line from that input, read it as the variable $line then:
    • Print out that $line
    • run vagrant halt $line halting the vagrant for that global unique ID

This is better than the Virtualbox method above, because it'll run any vagrant-configured shutdown mechanisms too.

Aquarion
  • 231
  • 3
    This command works for OS X hosts as well. Others fail due to differences in command-line arguments handling between OSX and Linux versions of sed and awk. Thanks! – Andrei Андрей Листочкин Jan 19 '16 at 18:25
  • @AndrewАндрейЛисточкин Confirm - works on OS X 10 well. Doesn't tested on other OS although. – setevoy Jan 20 '16 at 09:46
  • 2
    The above command tries to halt all boxes, no matter if there are running or not, thus taking a long time to execute. For me, as i only use virtualbox, i changed the "virtualbox" grep to "running", to get only the running ones. – Alex2php May 25 '16 at 10:26
  • This is a good answer, but you may like to use vagrant suspend instead of vagrant halt to save the state and suspend for a quicker down and up. – David Thomas Oct 16 '16 at 09:06
  • True, and if it works for you that's great. I've had massive timekeeping problems with suspended VMs under Virtualbox, so I wouldn't recommend it. – Aquarion Oct 16 '16 at 18:23
  • I know it's an old post, but I'm wondering if the option --prune is needed ? BTW this should be the accepted answer IMHO. Thanks. – youssman Feb 12 '18 at 16:34
  • @youssman: Prune didn't exist when I wrote this, I think, but I'd recommend running --prune on its own before the above to get rid of any that do not exist anymore before running the loop. – Aquarion Feb 12 '18 at 21:42
  • @Aquarion didn't know that (I'm new to vagrant). I think we can run it with --prune because it removes the invalid entries before printing the list. I'm using it now with --prune and seems to work (I'll post a comment if I encounter any errors/problem). Thanks for your answer. – youssman Feb 12 '18 at 22:34
6

In case other people get to this question: For those using VirtualBox, it already can take care of this, only involves editing a file:

# Contents of /etc/default/virtualbox
# ...
# ...
# SHUTDOWN_USERS="foo bar"  
#   check for running VMs of user 'foo' and user 'bar'
#   'all' checks for all active users
# SHUTDOWN=poweroff
# SHUTDOWN=acpibutton
# SHUTDOWN=savestate
#   select one of these shutdown methods for running VMs
#   acpibutton and savestate causes the init script to wait
#   30 seconds for the VMs to shutdown

## My original values
# SHUTDOWN_USERS=""
# SHUTDOWN=poweroff

## My current values
SHUTDOWN_USERS="all"
SHUTDOWN=savestate

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.

6

Combining some of the other answers, this will close down all running virtualbox vagrant boxes:

vagrant global-status | awk '/virtualbox running/{ print $1 }' | xargs vagrant halt
Ryan
  • 201
4

I just use vagrant halt. If you run it without a further argument, it stops all the machines defined in the Vagrantfile.

nomen
  • 168
1

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:

timestamp,target,type,data...

so you can import it as CSV file, since it's comma-separated.

With shell, it's probably more difficult to parse, for example:

for id in $(vagrant global-status --machine-readable | cut -d, -f5 | grep -B3 running | egrep -o "[0-9a-f]{7}"); do
    vagrant suspend $id;
done

See: Vagrant - Machine readable output


However I find it easier to parse the standard output, e.g.

while read id name provider state path; do
  [ "$state" = "running" ] && vagrant suspend $id;
done < <(vagrant global-status)

Btw. Theoretically vagrant command should accept a regular expression for the list of VMs to suspend as per this GH post, for example:

vagrant suspend '*'

but it doesn't work and there is a bug #7221 which is pending in order to fix it.


Related GitHub tickets:

kenorb
  • 10,347
0

This may or may not work for you ;-) Works for me

vagrant_halt_all.sh

#!/usr/bin/env bash
if [ -z "$1" ]
then
  OPTS=""
else
  # force close if any arg passed
  OPTS="-f"
fi

for i in $(vagrant global-status | grep running | awk '{print $1}'); do DIR=$(vagrant global-status | grep running | awk '{print $5}') cd "$DIR"; OUT=$(vagrant halt); echo "attempted to halt $i: $OUT" done

brad parks
  • 2,427
0

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)

vagrant global-status | awk '/virtualbox running/{ print $1 }' | xargs vagrant halt

Version 2 (more robust)

macOS/BSD compatible:

vagrant global-status --machine-readable | cut -d, -f5 | grep -B3 running | egrep -o '[0-9a-f]{7}' | xargs -n 1 -- vagrant halt

GNU compaible:

vagrant global-status --machine-readable | cut -d, -f5 | grep -B3 running | egrep -o '[0-9a-f]{7}' | xargs -r -d '\n' -n 1 -- vagrant halt

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:

alias vagrantHaltAll="vagrant global-status --machine-readable | cut -d, -f5 | grep -B3 running | egrep -o '[0-9a-f]{7}' | xargs -n 1 -- vagrant halt"

Usage:

vagrantHaltAll

Suspend all vagrant boxes

Or if you want to suspend any running machines, the syntax is easy:

vagrant suspend --all

I recommend using halt for web servers; it saves disk space and may be faster.

trebor
  • 121
0

Because vagrant halt requires VM metadata (you run it in the directory with the file Vagrantfile & the directory .vagrant) it cannot be used easily to halt all VMs.

In some cases it is enough to halt all VBoxes:

myvbox-halt-all() {
  VBoxManage list runningvms |& tr -d '\r' | while read name uuid; do
    VBoxManage controlvm "$uuid" acpipowerbutton
  done
}
gavenkoa
  • 1,093
0

vagrant global-status | grep virtualbox | cut -c 1-9 | xargs vagrant suspend will suspend all the machines created by Vagrant.

enter image description here

karel
  • 114,770
Manish
  • 1