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.

The following script takes care of all of the above:

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

#Edit these variables!

case "$1" in
        echo "Starting VirtualBox VM..."
        sudo -H -u $VMUSER VBoxManage startvm "$VMNAME" --type headless
        echo "Resetting VirtualBox VM..."
        sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" reset
        echo "Saving state of Virtualbox VM..."
        sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" savestate
        echo "Shutting down Virtualbox VM..."
        sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" acpipowerbutton
        sudo -H -u $VMUSER VBoxManage list vms -l | grep -e ^"$VMNAME": -e ^State | sed s/\ \ //g | cut -d: -f2-
        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.)
          sleep 1

        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

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?

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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