[mdlug] Time to build a new server

Carl T. Miller carl at carltm.com
Thu Aug 18 11:24:39 EDT 2011


Robert Adkins II wrote:
> 	Is it possible to setup Virtual Box to act as a service that will
> startup and launch an image automatically from a Cold Hardware Boot?

Yes, in fact I wrote a little script to do exactly that,
and it works on Red Hat, CentOS and similar distros.  If
you're running Debian or Ubuntu it will need to be tweaked.

You can copy and paste everything below at the command
line to create the script.  Then run:
  chmod u+x /usr/local/sbin/vboxguests
  /usr/local/sbin/vboxguests

This will set up and enable the service with a blank
configuration file.  You'll have to add hosts to
/etc/vboxguests to make it work.

c



cat > /usr/local/sbin/vboxguests <<\EndOfScript
#!/bin/sh

# @(#) vboxguests    creates init file for VirtualBox guests   
carl at carltm.com


CreateInitFile() {
cat > /etc/init.d/vboxguests <<\EoT
#!/bin/sh
# vboxguests        Manages startup of configured VirtualBox guests
#
# chkconfig: 2345 99 0
# description: Manages startup of configured VirtualBox guests
#
### BEGIN INIT INFO
# Short-Description: vboxguests
# Description: Manages startup of configured VirtualBox guests
# Default-Start: 2 3 4 5
# Default-Stop: 0 6
### END INIT INFO

if [ -f /etc/vboxguests ]; then
  case "$1" in
    start)
      touch /var/lock/subsys/vboxguests
      for Guest in `sed 's/#.*//' /etc/vboxguests`; do
        echo Starting "$Guest"...
        VBoxManage -q startvm $Guest --type headless
        sleep 2
      done
    ;;
    stop)
      rm -f /var/lock/subsys/vboxguests
      for Guest in `sed 's/#.*//' /etc/vboxguests`; do
        echo Stopping "$Guest"...
        VBoxManage -q controlvm $Guest savestate
      done
    ;;
    restart)
      for Guest in `sed 's/#.*//' /etc/vboxguests`; do
        echo Stopping "$Guest"...
        VBoxManage -q controlvm $Guest savestate
        echo Starting "$Guest"...
        VBoxManage -q startvm $Guest --type headless
        sleep 2
      done
    ;;
    status)
      for Guest in `sed 's/#.*//' /etc/vboxguests`; do
        echo -n $Guest:
        VBoxManage -q showvminfo $Guest | grep State: | sed 's/State: */ /'
      done
    ;;
    *)
      echo "vboxguests start|stop|status"
    ;;
  esac
fi
EoT

chmod 744 /etc/init.d/vboxguests
}

CreateEtcFile() {
cat > /etc/vboxguests <<\EoT
# /etc/vboxguests
#
# list the virtualbox guests to be started and stopped automatically

#guest1
#guest2

EoT
chmod 644 /etc/vboxguests
}

if which chkconfig > /dev/null 2>&1; then
  CreateInitFile
  chkconfig --add vboxguests
  if [ ! -f /etc/vboxguests ]; then
    CreateEtcFile
    echo Edit /etc/vboxguests as needed.
  fi
  echo vboxguests has now been configured as a service
else
  echo chkconfig is not in your path
fi
EndOfScript





More information about the mdlug mailing list