VirtualBox command warper
id : hcl5p1e6q8
category : computer
blog : unixlinux
created : 02/21/10 - 23:49:32

  • Here is a little script, to start, stop and save VM's on Sun's virtualbox :

.01#!/bin/bash
.02
.03#System binaries
.04TAIL="/usr/bin/tail" 
.05AWK="/bin/awk"
.06TR="/bin/tr"
.07CUT="/bin/cut"
.08GREP="/bin/grep"
.09
.10#Virtual box binaries
.11VBOXMANAGE="/usr/bin/VBoxManage"
.12VBOXHEADLESS="/usr/bin/VBoxHeadless"
.13
.14#Virtual box options
.15VBOXMANAGE_INFO="showvminfo"
.16VBOXMANAGE_LIST="list"
.17VBOXMANAGE_CONTROL_VM="controlvm"
.18VBOXMANAGE_SAVE_STATE="savestate"
.19VBOXHEADLESS_START_VM="--startvm"
.20
.21#Other usefull commands
.22QUIET_VBOX="tail -n+5"
.23
.24ARROW='---->'
.25
.26
.27#Get virtual machines names
.28VIRTUAL_MACHINES=`${VBOXMANAGE} ${VBOXMANAGE_LIST} vms | ${QUIET_VBOX} | ${AWK} {'print $1'} | ${TR} -d '"'`
.29
.30
.31for VIRTUAL_MACHINE in ${VIRTUAL_MACHINES}; do
.32
.33                #Get virtual machine name
.34                state=`${VBOXMANAGE} ${VBOXMANAGE_INFO} ${VIRTUAL_MACHINE} | ${GREP} State | ${CUT} -d ':' -f2,3,4`
.35
.36                #Print info to user
.37                echo ${VIRTUAL_MACHINE} ${ARROW} $state
.38done
.39
.40#Read command
.41read COMMAND
.42
.43CHOOSEN_VIRTUAL_MACHINE=`echo $COMMAND | ${CUT} -d ' ' -f 1`
.44CHOOSEN_STATE=`echo $COMMAND | ${CUT} -d ' ' -f 2`
.45
.46case $CHOOSEN_STATE in
.47                start)
.48                    ${VBOXHEADLESS} ${VBOXHEADLESS_START_VM} ${CHOOSEN_VIRTUAL_MACHINE}
.49                    ;;
.50                save)
.51                    ${VBOXMANAGE} ${VBOXMANAGE_CONTROL_VM} ${CHOOSEN_VIRTUAL_MACHINE} ${VBOXMANAGE_SAVE_STATE}
.52                    ;;
.53
.54                stop)
.55                    ${VBOXMANAGE} ${VBOXMANAGE_CONTROL_VM} ${CHOOSEN_VIRTUAL_MACHINE} ${VBOXMANAGE_POWER_OFF}
.56                    ;;
.57esac