Skip to content

Instantly share code, notes, and snippets.

@jibone
Created March 5, 2012 11:21
Show Gist options
  • Save jibone/1977921 to your computer and use it in GitHub Desktop.
Save jibone/1977921 to your computer and use it in GitHub Desktop.
Create, modify, start and stop a VBoxHeadless
#! /bin/sh
#
# Modified version... based on https://forums.virtualbox.org/viewtopic.php?f=7&t=32842#
#
# Copyright (c) 2010 Christopher Thompson
# All rights reserved.
#
# Author: Christopher Thompson, 2010
#
# /etc/init.d/virtualbox
#
# Description: The virtualbox headless service allows a specified virtualbox
# VM to be run with just RDP access.
#
SERVICENAME=VirtualBox
VIRTUALMACHINE=90ae3ff9-d2cd-4a16-9004-db6174c6dcdd
VMSTATUS=`VBoxManage list runningvms | grep $VIRTUALMACHINE | wc -l`
#VMSTATUS=`VBoxManage list runningvms | grep Ubuntu | wc -l`
PIDFILE=/var/run/$SERVICENAME-$VIRTUALMACHINE.pid
# Check for missing binaries
VIRTUALBOX_BIN=/usr/bin/VBoxHeadless
test -x $VIRTUALBOX_BIN || { echo "$VIRTUALBOX_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
VIRTUALMACHINE_CONFIG=/etc/virtualbox/VirtualBox.xml
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 -- --startvm "$VIRTUALMACHINE"
;;
stop)
echo "ACPI power button pressed."
echo -n "Shutting down $SERVICENAME: $VIRTUALMACHINE "
if [ $VMSTATUS = "0" ]; then echo "NOT RUNNING"; exit 2; else echo ""; fi;
VBoxManage controlvm $VIRTUALMACHINE acpipowerbutton
echo -n "Waiting for shutdown: "
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;
start-stop-daemon --pidfile $PIDFILE --stop $VIRTUALBOX_BIN -- --startvm "$VIRTUALMACHINE"
rm -f $PIDFILE
;;
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 Ubuntu BTS : "
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}"
exit 1
;;
esac
:
masuri:/etc/init.d#
# Create a VBox VM #
====================
VBoxManage createvm --name "Ubuntu 10.10 Server" --register
VBoxManage modifyvm "Ubuntu 10.10 Server" --memory 256 --acpi on --boot1 dvd --nic1 bridged --bridgeadapter1 eth0
VBoxManage createhd --filename Ubuntu_10_10_Server.vdi --size 10000
VBoxManage storagectl "Ubuntu 10.10 Server" --name "IDE Controller" --add ide
VBoxManage storageattach "Ubuntu 10.10 Server" --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium Ubuntu_10_10_Server.vdi
VBoxManage storageattach "Ubuntu 10.10 Server" --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium /home/ubuntu-10.10-server-amd64.iso
# control the VM #
==================
VBoxHeadless --startvm "Ubuntu 10.10 Server"
VBoxManage controlvm "Ubuntu 10.10 Server" poweroff
VBoxManage controlvm "Ubuntu 10.10 Server" pause
VBoxManage controlvm "Ubuntu 10.10 Server" reset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment