Skip to content

Instantly share code, notes, and snippets.

@rzbrk
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save rzbrk/9369691 to your computer and use it in GitHub Desktop.

Select an option

Save rzbrk/9369691 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# This init script autostarts necessary vms at boot
# and saves running vms on shutdown
# Inspired by:
# http://ubuntuforums.org/showthread.php?t=2181095&s=d46e80841d9fedc49cfbb37ba885af9d&p=12826048#post12826048
#
# Execute this script when perform boot-up and shutdown
# sudo update-rc.d startvm defaults 99 01
# Define the user name who is able manipulate the VMs
# (usually member of group "vboxusers")
VBOXUSER=vbox
# Define the VMs which are to be started by their names
# AUTOSTARTVMS="VM1 VM2"
AUTOSTARTVMS="Win7-Remote"
RUNNINGVMS=$(sudo -H -u $VBOXUSER vboxmanage list runningvms | cut -d " " -f1 | sed -e 's/^.//' -e 's/.$//')
case "$1" in
start)
for i in $AUTOSTARTVMS
do
echo "Starting" $i "VM"
sudo -H -u $VBOXUSER vboxmanage startvm $i --type headless
done
;;
stop)
for i in $RUNNINGVMS
do
echo "Saving state of" $i "VM"
sudo -H -u $VBOXUSER vboxmanage controlvm $i savestate
done
;;
*)
echo "Usage: /etc/init.d/startvm {start|stop}"
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment