So, we’ve been using Vagrant a lot lately at work, and one thing that bugged me was whenever i shutdown my computer, it wouldn’t because I forgot to suspend or halt my running Vagrant boxes before shutting down.
So, I wrote a simple init script that suspends all running boxes, nice and easy. It should handle multiple users also (I have not tested this thou).
#!/bin/sh -e ### BEGIN INIT INFO # Provides: something warm and fuzzy # Required-Start: vboxdrv # Required-Stop: vboxdrv # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts suspended vagrant boxes and suspends running vagrant boxes # Description: ### END INIT INFO # presumably only users with valid login shells are running vagrant boxes validShells=$(cat /etc/shells | grep -v "#" | sed ':a;N;$!ba;s/\n/|/g') userList=$(grep -E "$validShells" /etc/passwd | awk -F ':' ' { print $1 } ' | tr "\\n" " ") case $1 in start) # loop thru every user for user in $userList; do # loop thru users suspended boxes for vm in $(su -c "vagrant global-status" $user 2>/dev/null | grep saved | awk ' { print $5 } '); do cd $vm >/dev/null su -c "vagrant up" $user su -c "vagrant status" $user > /dev/null # update global-status cache done done ;; stop) for user in $userList; do for vm in $(su -c "vagrant global-status" $user 2>/dev/null | grep running | awk ' { print $5 } '); do cd $vm > /dev/null su -c "vagrant suspend" $user su -c "vagrant status" $user > /dev/null # update global-status cache done done ;; status) for user in $userList; do echo "$user's vagrant box status" echo "------------------------------------------------------------------------" su -c "vagrant global-status 2> /dev/null" $user echo echo done ;; *) echo "Usage: $0 {start|stop|status}" >&2 exit 1 ;; esac exit 0
Installation
Edit /etc/init.d/vagrant-boxes
and paste the above script and save (or download it from here and save it to /etc/init.d/vagrant-boxes). On debian/ubuntu etc, run
# update-rc.d vagrant-boxes defaults 99 01
Number 99 is the sequence number and should be larger than (in my case Virtualbox number 20, which by the way is the default on Debian distros). The second number is the sequence when shutting down the computer. So, it might be good to do first of all.
Thank you!
I had the same problem, forgetting to halt or suspend running virtualboxes.
I’ll try your script now on ubuntu 14.04 amd64
Hi Olle,
Would you mind if I took this and wrapped it in a complete shell script (with all acknowledgements to you and links to this site and page of course)?
Thanks in advance
John
Of course not, wrap on! =D
This is fantastic. Adding it to my Oracle Linux box. Vagrant people need to look into providing this as an in-built functionality.
Just set this up on Ubuntu 14.04 with Vagrant 1.7.2 and it reports no instances every time…
I added the vagrant-boxes file to /etc/init.d/vagrant-boxes and ran the following command:
$ sudo update-rc.d vagrant-boxes defaults 99 01
But got this message:
insserv: Service vboxdrv has to be enabled to start service something
insserv: exiting now!
update-rc.d: error: insserv rejected the script header
I’m on Xubuntu 15.10, and using vagrant via https://github.com/varying-vagrant-vagrants/vvv/ . Any help? This looks like a great script. Thanks!
To get this to work on Ubuntu 16.04 and Vagrant 1.8.5, I just removed ”vboxdrv” from the script header and left the Required-Start and Required-Stop blank.
Cheers!
You don’t need to `cd` to the `directory` of the environment. You may instead provide the `id` of the environment you get with the `vagrant global-status` listing as an argument to `vagrant suspend`, e.g. `vagrant suspend 1526a41`.
Yes sir, you are correct. But at the time when I wrote the above, I believe you had to.
At which location this file ( /etc/init.d/vagrant-boxes ) is located?
I have installed
Vagrant-1.9.7 at: D:\dev\Vagrant
VirtualBox-5.1.26 at: D:\VirtualBox
Vagrant-local-repo-2.0.0 at: D:\sites\vagrant-local
Please guide me with absolute path.
Thank you.