Created
August 24, 2012 01:28
-
-
Save hfase01/3444494 to your computer and use it in GitHub Desktop.
vboxmanage init.d script
This file contains 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
SERVICENAME=VirtualBox | |
VIRTUALMACHINE="Linux Server" | |
#echo "VirtualBox Virtual Machine details" | |
#echo "==================================" | |
#echo "VIRTUALMACHINE=$VIRTUALMACHINE" | |
VMSTATUS=`VBoxManage list runningvms | grep "$VIRTUALMACHINE" | wc -l` | |
PIDFILE="/var/run/$SERVICENAME-$VIRTUALMACHINE.pid" | |
# Check for missing binaries | |
VIRTUALBOX_BIN=/usr/bin/VBoxHeadless | |
VIRTUALBOX_CONTROL_BIN=/usr/bin/VBoxManage | |
test -x $VIRTUALBOX_BIN || -x $VIRTUALBOX_CONTROL_BIN || { echo "$VIRTUALBOX_BIN or $VIRTUALBOX_CONTROL_BIN not installed"; | |
if [ "$1" = "stop" ]; then exit 0; | |
else exit 5; fi; } | |
# Check for existence of needed config file and read it | |
VIRTUALMACHINE_CONFIG="/etc/virtualbox/$VIRTUALMACHINE.xml" | |
#echo "VIRTUALMACHINE_CONFIG=$VIRTUALMACHINE_CONFIG" | |
#echo " " | |
test -r "$VIRTUALMACHINE_CONFIG" || { echo "$VIRTUALMACHINE_CONFIG does not exist"; | |
if [ "$1" = "stop" ]; then exit 0; | |
else exit 6; fi; } | |
case "$1" in | |
start) | |
echo -n "Starting $SERVICENAME: $VIRTUALMACHINE " | |
if [ $VMSTATUS = "1" ]; then echo "ALREADY RUNNING"; exit 2; else echo ""; fi; | |
## Start daemon with start-stop-daemon. If this fails the return value is set appropriately by start-stop-daemon. | |
start-stop-daemon --background --make-pidfile --pidfile "$PIDFILE" --start --exec $VIRTUALBOX_BIN -- -s "$VIRTUALMACHINE" | |
;; | |
stop) | |
echo "ACPI power button depression sent to Virtual Machine." | |
echo -n "Shutting down $SERVICENAME: $VIRTUALMACHINE " | |
if [ $VMSTATUS = "0" ]; then echo "NOT RUNNING"; exit 2; else echo ""; fi; | |
VBoxManage controlvm "$VIRTUALMACHINE" acpipowerbutton | |
echo -n "Please wait for shutdown to complete: " | |
while [ `VBoxManage list runningvms | grep "$VIRTUALMACHINE" | wc -l` != "0" ] | |
do | |
echo -n "." | |
sleep 2 | |
done | |
echo "Done" | |
rm -f "$PIDFILE" | |
;; | |
force-stop) | |
echo "This is a very harsh way of managing the machines, please shut them down using SSH or RDP" | |
echo -n "Shutting down $SERVICENAME: $VIRTUALMACHINE" | |
if [ $VMSTATUS = "0" ]; then echo "NOT RUNNING"; exit 2; else echo ""; fi; | |
while [ `$VIRTUALBOX_CONTROL_BIN list runningvms | grep "$VIRTUALMACHINE" | wc -l` != "0" ] | |
do | |
echo -n "." | |
result=`$VIRTUALBOX_CONTROL_BIN controlvm "$VIRTUALMACHINE" poweroff` | |
sleep 1 | |
done | |
echo "Done" | |
;; | |
restart) | |
## Stop the service and regardless of whether it was running or not, start it again. | |
$0 stop | |
$0 start | |
;; | |
status) | |
echo -n "Checking for service $SERVICENAME : " | |
if [ $VMSTATUS = "1" ]; then echo "RUNNING"; | |
else echo "NOT RUNNING"; fi; | |
#VBoxManage showvminfo "$VIRTUALMACHINE" | |
;; | |
*) | |
## If no parameters are given, print which are avaiable. | |
echo "Usage: $0 {start|stop|force-stop|status|restart}" | |
echo " " | |
exit 1 | |
;; | |
esac | |
: | |
echo " " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment