Wednesday, July 22, 2015

Automatic Oracle Virtualbox VMs startup and shutdown on Linux

7:40 AM Posted by Dilli Raj Maharjan No comments

  • Create /etc/init.d/myvirtualbox executable file with following contents.


#!/bin/bash
# /etc/init.d/myvirtualbox


# Variable Declaration
declare -a HOSTS
HOSTS=( "OEL1" "OEL2" )
ACTION=""
DEBUG=1
USER="oracle"
LOCK_DIR="/var/lock/myvirtualbox"
LOCK_FILE=""
LOG_DIR="/var/log"
LOG_FILE="${LOG_DIR}/myvirtualbox.log"


# Function to log the information to logfile
function logit {
    echo $1
    echo $1 >> ${LOG_FILE}
}

# Function to check existance of lock file
function checklock {
    case $1 in
        1)
            if [ -e ${LOCK_FILE} ]; then
                logit "${LOCK_FILE} already exists, Fail to start Virtualbox host ${HOST}"
                logit "Verify VM status, if it is stopped then manually remove ${LOCK_FILE} to start"
                exit 1
            fi
        ;;
        2)
            if [ ! -e ${LOCK_FILE} ]; then
                logit "${LOCK_FILE} doesnot exists, Fail to stop Virtualbox host ${HOST}"
                logit "Verify VM status, if it is running then manually stop Virtual host ${HOST} with command below"
                logit "su - oracle -c \"VBoxManage controlvm ${HOST} savestate\""
                exit 1
            fi
        ;;
        *)
        ;;
    esac
}


# checking lock dir exists
if [ ! -e ${LOCK_DIR} ]; then
    [ $DEBUG -eq "1" ] && logit "Lock dir does not exists, Creating ${LOCK_DIR}"
    mkdir -p ${LOCK_DIR}
fi

# Starting, Stopping or resetting on basic of argument
case $1 in 
    start)
        logit "Starting myvirtualbox VMS"
        for HOST in "${HOSTS[@]}"
        do

            LOCK_FILE="${LOCK_DIR}/${HOST}"
            checklock 1
            CMD="VBoxManage startvm ${HOST} --type=headless"
            [ $DEBUG -eq 1 ] && echo $CMD
            su - ${USER} -c "$CMD" | tee -a ${LOG_FILE}
            [ $? -eq "0" ] && touch ${LOCK_FILE} 
            [ $? -eq "0" ] && logit "Virtualbox host ${HOST} started successfully"
        done

    ;;
    stop)
        logit "Stopping myvirtualbox VMS"
        for HOST in "${HOSTS[@]}"
        do
            LOCK_FILE="${LOCK_DIR}/${HOST}"
            checklock 2 
            CMD="VBoxManage controlvm ${HOST} savestate"
            [ $DEBUG -eq 1 ] && echo $CMD
            su - ${USER} -c "$CMD" | tee -a ${LOG_FILE}
            [ $? -eq "0" ] && rm ${LOCK_DIR}/${HOST}
            [ $? -eq "0" ] && logit "Virtualbox host ${HOST} stopped successfully"
        done

    ;;

    restart)
        logit "Restarting myvirtualbox VMS"
        if [ "$#" -eq "2" ]; then
            LOCK_FILE="${LOCK_DIR}/${2}"
            CMD="VBoxManage controlvm ${2} reset"
            [ $DEBUG -eq 1 ] && echo $CMD
            su - oracle -c "$CMD"
        else
        for HOST in "${HOSTS[@]}"
        do
            LOCK_FILE="${LOCK_DIR}/${HOST}"
            CMD="VBoxManage controlvm ${HOST} reset"
            [ $DEBUG -eq 1 ] && echo $CMD
            su - oracle -c "$CMD" | tee -a ${LOG_FILE}
            [ $? -eq "0" ] && logit "Virtualbox host ${HOST} restarted successfully"
        done

        fi
    ;;
    *)
        echo "Usage: $0 {start|stop|restart} [VMname in case of restart]"
        exit 1


esac


  • Make the file executable.

chmod 755 /etc/init.d/myvirtualbox

  • Create symbolic link of the executable file to run level directories.


ln -s /etc/init.d/myvirtualbox /etc/rc.d/rc2.d/S99myvirtualbox
ln -s /etc/init.d/myvirtualbox /etc/rc.d/rc3.d/S99myvirtualbox
ln -s /etc/init.d/myvirtualbox /etc/rc.d/rc4.d/S99myvirtualbox
ln -s /etc/init.d/myvirtualbox /etc/rc.d/rc5.d/S99myvirtualbox


ln -s /etc/init.d/myvirtualbox /etc/rc.d/rc0.d/K01myvirtualbox
ln -s /etc/init.d/myvirtualbox /etc/rc.d/rc6.d/K01myvirtualbox

0 comments:

Post a Comment