Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an Ubuntu server on which a Virtualbox virtual machine needs to be running at all times. The VMs are administered by a specific user VMAdmin which has no admin privileges. I need to create a init.d script for handling my virtualbox VMs.
It should:

  • start VM whenever the host system boots;
  • save the guest system's state whenever the host is shut down;
  • provide commands for starting, shutting down, resetting and backing up the VM.
share|improve this question
up vote 7 down vote accepted

The following script takes care of all of the above:

#! /bin/sh
# /etc/init.d/vbox

#Edit these variables!
VMUSER=VMAdmin
VMNAME="cdb62186-7c30-4c25-a0b0-e4a32cfb0504"
BASEFOLDER=/home/VMAdmin/path/to/backups/

case "$1" in
    start)
        echo "Starting VirtualBox VM..."
        sudo -H -u $VMUSER VBoxManage startvm "$VMNAME" --type headless
        ;;
    reset)
        echo "Resetting VirtualBox VM..."
        sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" reset
        ;;
    stop)
        echo "Saving state of Virtualbox VM..."
        sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" savestate
        ;;
    shutdown)
        echo "Shutting down Virtualbox VM..."
        sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" acpipowerbutton
        ;;
    status)
        sudo -H -u $VMUSER VBoxManage list vms -l | grep -e ^"$VMNAME": -e ^State | sed s/\ \ //g | cut -d: -f2-
        ;;
    backup)
        echo ""
        sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" acpipowerbutton

        echo "Waiting for VM "$VMNAME" to poweroff..."
        until $(sudo -H -u $VMUSER VBoxManage showvminfo --machinereadable "$VMNAME" | grep -q ^VMState=.poweroff.)
        do
          sleep 1
        done

        FILENAME=$(date +"%Y_%m_%d-%T")
        echo "Backing up Virtualbox VM to '$BASEFOLDER$FILENAME'..."
        sudo -H -u $VMUSER VBoxManage clonevm "$VMNAME" --options keepallmacs --name $FILENAME --basefolder $BASEFOLDER

        echo "Restarting VirtualBox VM..."
        sudo -H -u $VMUSER VBoxManage startvm "$VMNAME" --type headless
        echo ""
        ;;
    *)
        echo "Usage: sudo service vbox {start|stop|status|shutdown|reset|backup}"
        exit 1
        ;;
esac

exit 0

Tell the script be the first to shutdown and the last to startup:

sudo update-rc.d vbox defaults 99 01

To add backup task to crontab, run:

sudo crontab -e

And add a line like:

* 3 * * 5 service vbox backup

Which will run a weekly backup on Fridays at 3 AM. For more info on creating a crontab task see: http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

Related question: Bash script to wait for Virtualbox VM shutdown?

share|improve this answer
    
I love it! But, it doesn't look like an upstart script ;-) – Andrew Jul 29 '14 at 14:09
    
Thank you for this script. can you adapt it to take the name of the virtualbox as parameter ? – skonsoft Oct 12 '15 at 0:43
1  
@skonsoft: you could probably replace $VMNAME with $2 and the use start the service with sudo service vbox start VMNAME. I'm not sure if this usage is recomended or even allowed. – Emanuel Ey Oct 12 '15 at 9:28
    
Yes i did it and it works. Command like this: sudo service vbox VMNAME start. I asked you to do this because i want to sahre with you another script that uses your script to start all configured VM (Just by calling your script ) and it works very well – skonsoft Oct 12 '15 at 10:57
1  
@skonsoft I'd like to see that script. Please add it as another answer to this question or post it as a standalone question with answer and share the link. Also, remember to upvote it this question was useful to you ;) – Emanuel Ey Oct 12 '15 at 11:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.