901-poweron-vm
#!/bin/bash
#
# Start-up VM and wait until VM has started
# This is not a perfect science as there is really
# not a good way for VB to know that the Guest is
# completed with the 'startup'
# Ok to break out of script!
#
#
if [ "$UID" -eq 0 ]
  then echo "This script should NOT be run as root."
  exit 1
fi
#
# Source config file
if [ ! -f ~/.vbconfig ]; then
    echo "Config File ~/.vbconfig not found!"
    echo "Run: set-folders to create .vbconfig"
    exit 1
fi
. ~/.vbconfig
#
if [[ ! ("$#" == 1) ]]; then
  echo "Usage for $0 : VM Name required as argument."
  exit 1
fi
VB_VM_NAME=$1
# The ITERA and SLEEP parameters determine polling duration and interval
#   to check if the VM has been powered down
VM_POWER_ON_ITERA=60
VM_POWER_ON_SLEEP=5
#
# Check if VM is already running
VM_POWERED_ON=`VBoxManage list runningvms|grep -c '^"'${VB_VM_NAME}'"'`
if [ $VM_POWERED_ON -eq 1 ]; then
  echo "VM $VB_VM_NAME is already up!"
  exit 0
else
  VBoxManage startvm  "${VB_VM_NAME}" --type headless
  echo "You can Ctrl-break from this script at any time ..."
fi
#
VM_LOG_FOLDER=`VBoxManage showvminfo ${VB_VM_NAME} | \
               grep "Log folder" | cut -d ":" -f2 | sed 's/^[ ]*//'`
VM_LOG_FILE=${VM_LOG_FOLDER}/VBox.log
VM_GA_LOADED="0"
COUNTER=1
until [ $VM_GA_LOADED -gt 0 ]; do
    sleep $VM_POWER_ON_SLEEP
    VM_GA_LOADED=`grep -c "Guest Additions" ${VM_LOG_FILE}`
    echo "Waiting $VM_POWER_ON_SLEEP additional seconds for VM to fully power on"
    echo " - Iteration ${COUNTER}/${VM_POWER_ON_ITERA} ..."
    let COUNTER+=1
    if [ $COUNTER -gt $VM_POWER_ON_ITERA ]; then
        echo "VM does not seem to be powering on!"
        echo "Or Guest Additions is not installed to defect a full Power on"
        echo "Exiting ..."
        exit 1
    fi
done
if [ $COUNTER -gt 1 ]; then
    SECS_TO_POWERON=$(echo "${VM_POWER_ON_SLEEP}*${COUNTER}" | bc)
    echo "Guest Additions started in approximately $SECS_TO_POWERON seconds."
fi
VM_POWER_ON_WAIT=$SECS_TO_POWERON
echo "Waiting an additional $VM_POWER_ON_WAIT seconds ..."
sleep $VM_POWER_ON_WAIT
#
echo "Check and display Errors:"
grep -v "AIOMgr: Flush failed" $VM_LOG_FILE | grep -i 'VERR_\|error\|failed'
#
echo "IP address of VM:"
$VB_BIN_LOC/060-get-vm-ip.bash $VB_VM_NAME
exit

QR Code
QR Code scripts:901-poweron-vm (generated for current page)