Skip to content


VirtualBox launch script

I’ve been playing around with VirtualBox some more today (more on it, and other virtualization related stuff might follow later). I got a Windows XP instance running fine (and fast!) in it. Still got some issues (shared folders seem not to work when logged in as a non-administrator user in XP, although I tend to blame Windows for this issue, not VirtualBox), played around with a little Linux installation acting as an iSCSI target for its virtual block devices, accessing them from Windows using the Microsoft iSCSI initiator, etc. Here’s a screenshot of all this.

I added a launcher for my Windows VM to my panel, which was simply running ‘VBoxManage startvm <VM UUID>’. This was not an optimal solution though, as I wanted it to show a little error dialog when something went wrong when attempting to launch the VM (eg because I forgot it was already running), when the virtual disk files aren’t accessible (because I forgot to mount the volume they reside on), etc.

So I cooked a little script which runs some sanity checks before launching the virtual machine, and reports any errors. Here it is:

#!/bin/bash

# This script was written by Nicolas Trangez <eikke eikke com>
# It can act as a launcher script for VirtualBox virtual machines
# and provides some basic error checking/reporting when doing so.
# It might be useful when using a shortcut to launch machines in a
# desktop environment.

VBOXMANAGE=$(which VBoxManage)
ZENITY=$(which zenity)
VBOXVMUUID=$1

if [ ! -x ${ZENITY} ]; then
        echo "Error: The zenity tool could not be found."
        exit 1
fi

if [ ! $# = 1 ]; then
        ${ZENITY} --error --title "Argument required" --text "This script should be called with one argument: the UUID of the VirtualBox virtual machine you want to start.

To find out this UUID, run \"${VBOXMANAGE} list vms\"."
        exit 2
fi

if ! ${VBOXMANAGE} list vms | grep ^UUID | grep ${VBOXVMUUID} > /dev/null; then
        ${ZENITY} --error --title "Unknown Virtual Machine" --text "No VirtualBox virtual machine with UUID \"${VBOXVMUUID}\" could be found."
        exit 3
fi

if ${VBOXMANAGE} showvminfo ${VBOXVMUUID} | grep ^Primary\ master > /dev/null; then
        DISKFILE=$(${VBOXMANAGE} showvminfo ${VBOXVMUUID} | grep ^Primary\ master | sed -e "s/Primary\ master:\ *//" -e 's/\ *(UUID.*$//')
        if [ ! -r ${DISKFILE} ]; then
                ${ZENITY} --error --title "Primary master not found" --text "The image file for the primary master disk, ${DISKFILE}, could not be found."
                exit 1
        fi
fi

if ${VBOXMANAGE} showvminfo ${VBOXVMUUID} | grep ^Primary\ slave > /dev/null; then
        DISKFILE=$(${VBOXMANAGE} showvminfo ${VBOXVMUUID} | grep ^Primary\ slave | sed -e "s/Primary\ slave:\ *//" -e 's/\ *(UUID.*$//')
        if [ ! -r ${DISKFILE} ]; then
                ${ZENITY} --error --title "Primary slave not found" --text "The image file for the primary slave disk, ${DISKFILE}, could not be found."
                exit 1
        fi
fi

if ${VBOXMANAGE} showvminfo ${VBOXVMUUID} | grep ^Secondary\ slave > /dev/null; then
        DISKFILE=$(${VBOXMANAGE} showvminfo ${VBOXVMUUID} | grep ^Secondary\ slave | sed -e "s/Secondary\ slave:\ *//" -e 's/\ *(UUID.*$//')
        if [ ! -r ${DISKFILE} ]; then
                ${ZENITY} --error --title "Secondary slave not found" --text "The image file for the secondary slave disk, ${DISKFILE}, could not be found."
                exit 1
        fi
fi

VBOXOUT=$(${VBOXMANAGE} startvm ${VBOXVMUUID})
VBOXCODE=$?

if [ ! ${VBOXCODE} = 0 ]; then
        echo -e ${VBOXOUT}
        VBOXERROR=$(echo ${VBOXOUT} | grep "\[\!\]\ Text" | sed "s/.*\[\!\]\ Text\ *=\ *//")
        VBOXERROR=$(echo ${VBOXERROR} | sed "s/\ \[\!\].*//")
        ${ZENITY} --error --title "Error starting Virtual Machine" --text "An error occured while starting the virtual machine:

${VBOXERROR}

Run \"${VBOXMANAGE} startvm ${VBOXVMUUID}\" to see more details."
        exit 4
fi

Enjoy!

Posted in Linux, Technology.

Tagged with , .


3 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Shad Sterling says

    This script fails on paths containing spaces. Adding a few well placed quote marks seems to fix it.

  2. Edzams says

    I’m an absolute noob when it comes to scripting so how do I use this script. Thanks in advance.

Continuing the Discussion

  1. Video codec. linked to this post on July 22, 2008

    Divx codec….

    Windows media player codec. Nero rip audio cd lame codec. Xvid codec….



Some HTML is OK

or, reply to this post via trackback.