Created
December 4, 2013 19:36
-
-
Save werty1st/7794082 to your computer and use it in GitHub Desktop.
vbox autostart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
#This init script autostarts necessary vms at boot | |
#and saves running vms on shutdown | |
# Sed explanation: sed -e 's/^.//' -e 's/.$//' | |
# 1. -e means to allow multiple arguments in a single sed command | |
# 2. 's/^.//' means to substitute (s) / at the beginning of the line (^), any character (.) / [substitute with nothing] / | |
# 3. 's/.$//' means to substitute (s) / any character (.), at the end of the line / [substitute with nothing] / | |
VBOXUSER=vbox | |
RUNNINGVMS=$(sudo -H -u $VBOXUSER vboxmanage list runningvms | cut -d " " -f1 | sed -e 's/^.//' -e 's/.$//') | |
STOPPEDVMS=$(sudo -H -u $VBOXUSER vboxmanage list vms | cut -d " " -f1 | sed -e 's/^.//' -e 's/.$//') | |
case "$1" in | |
start) | |
for i in $STOPPEDVMS | |
do | |
echo "Starting" $i "VM" | |
sudo -H -u $VBOXUSER vboxmanage startvm $i --type headless | |
sleep 5 | |
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