Suspend/resume all Vagrant boxes on system shutdown/startup

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.

”Wrong” vim color scheme for root

On (what I’ve noticed so far) Ubuntu based distros, one thing that bugged me was that on a fresh install when you sudo into root and edit for example a config file, all comments were in dark blue color (which on a terminal with black background is terrible) but when doing it as a normal user the color theme was more pleasing (readable).

This is how I ”fix” this.

First, add these two lines to /etc/profile (which is read by /bin/bash /bin/dash /bin/sh etc at login)

sudo echo 'export COLORFGBG="15;0"' >> /etc/profile

Then, add this line in your /etc/sudoers file to keep the setting.

Defaults        env_keep += "COLORFGBG"

Which makes sudo remember the COLORFGBG setting. You can add other stuff here as well, for instance maybe you want’t to be able to run stuff that only you have in your PATH with sudo.

Now when you login, and sudo edit a file you get bright colors. Yay! But wait, It don’t work you say? Well, I’m guessing you run KDE or some other fancy DE and it’s terminal. Most terminals for X do run bash, but they don’t run it as a login shell, so they don’t source /etc/profile and hence the settings above won’t do any good.

My solution is to add ”-l” to the command line starting bash, which makes bash behave as it’s running as a login shell.

Here’s how to do it in Konsole in KDE:

konsole_settings konsole_shell