[HOWTO] start and stop VM with your NAS.

Just about everything is virtual these days. Topics relating to virutalization go here.

Moderator: Lillian.W@AST

mc1457
Posts: 5
youtube meble na wymiar Warszawa
Joined: Sun Nov 13, 2022 9:10 pm

Re: [HOWTO] start and stop VM with your NAS.

Post by mc1457 »

There are bugs in the latest release of AppCentral/virtualbox (ADM 4.1.0.RLQ1, VirtualBox app 6.1.32.r3) . Edit /usr/local/AppCentral/virtualbox/CONTROL/env.sh and make the following changes:
  • Code: Select all

    DEBUG="flase"
    to

    Code: Select all

    DEBUG="false"
    (doesn't break anything, but annoys my OCD)
  • Code: Select all

    export VIRTUALBOX_LD_LIBRARY_PATH=$PKG_PATH/lib:$XORG_LD_LIBRARY_PATH
    to

    Code: Select all

    export VIRTUALBOX_LD_LIBRARY_PATH=$PKG_PATH/bin:$XORG_LD_LIBRARY_PATH
    It appears they eliminated the /lib folder and put the load modules in /bin but never updated the env.sh script
mc1457
Posts: 5
Joined: Sun Nov 13, 2022 9:10 pm

Re: [HOWTO] start and stop VM with your NAS.

Post by mc1457 »

Don't modify the start-stop.sh script. This is for controlling the virtualbox daemon. The vboxinit script (/volume1/Web/virtualbox/vboxinit) is a red herring - this is an example script for phpvirtual box that you could get working, but it's not intended for the Asustor.

The OP's script is close, but needed updating. Here's an update with improved instructions. This will create a script that will start/stop a single virtualbox guest with the system, stopping it before virtualbox and starting it after.

NOTE: you must fix the bug noted in my previous post in /usr/local/AppCentral/virtualbox/CONTROL/env.sh.

Code: Select all

#!/bin/sh
# Create a shared folder named "system" (/share/system)
# create this script there as vmcontrol.sh
# chmod u+x /share/system/vmcontrol.sh
# update VM_NAME below to the name of your guest
# link it with /usr/local/etc/init.d/S82vmcontrol
# link it with /usr/local/etc/init.d/K60vmcontrol
# ln -s /share/system/vmcontrol.sh /usr/local/etc/init.d/S82vmcontrol
# ln -s /share/system/vmcontrol.sh /usr/local/etc/init.d/K60vmcontrol

PKG_PATH=/usr/local/AppCentral/virtualbox

if [ ! -e $PKG_PATH/CONTROL/env.sh ]; then
    echo "Can't find $PKG_PATH/CONTROL/env.sh"
    exit 1
fi

source $PKG_PATH/CONTROL/env.sh

VM_NAME="aws-storage-gateway"

source $PKG_PATH/CONTROL/env.sh
. /lib/lsb/init-functions

VBoxManage list vms|grep "^\"$VM_NAME\"" >/dev/null 2>&1
if [ $? -ne 0 ]
then
        log_failure_msg "VM $VM_NAME seems not to exists"
        log_end_msg 255
fi


case "$1" in
    start)
                log_daemon_msg "Starting VM" "$VM_NAME"
        VBoxManage list runningvms|grep "^\"$VM_NAME\"" >/dev/null 2>&1
                if [ $? -eq 0 ]
                then
                        log_warning_msg "VM $VM_NAME is already started, nothing done"
                else
                        VBoxManage startvm $VM_NAME --type headless
                        BCLE=20
                        RETCOD=1
                        while [ \( $BCLE -gt 0 \) -a \( $RETCOD -ne 0 \) ]
                        do
                                VBoxManage list runningvms|grep "^\"$VM_NAME\"" >/dev/null 2>&1
                                RETCOD=$?
                                BCLE=`expr $BCLE - 1`
                                [ $RETCOD -ne 0 ] && sleep 10
                        done
                        if [ $RETCOD -ne 0 ]
                        then
                                log_failure_msg "VM $VM_NAME unable to start"
                        fi
                fi
        log_end_msg 0
        ;;
    stop)
        log_daemon_msg "Stopping VM" "$VM_NAME"
        VBoxManage list runningvms|grep "^\"$VM_NAME\"" >/dev/null 2>&1
                if [ $? -ne 0 ]
                then
                        log_warning_msg "VM $VM_NAME is already stopped, nothing done"
                else
                        # gracefully stopping VM by short pressing power button
                        VBoxManage controlvm $VM_NAME acpipowerbutton
                        BCLE=20
                        RETCOD=0
                        while [ \( $BCLE -gt 0 \) -a \( $RETCOD -eq 0 \) ]
                        do
                                VBoxManage list runningvms|grep "^\"$VM_NAME\"" >/dev/null 2>&1
                                RETCOD=$?
                                BCLE=`expr $BCLE - 1`
                                [ $RETCOD -eq 0 ] && sleep 10
                        done
                        if [ $RETCOD -eq 0 ]
                        then
                                log_failure_msg "VM $VM_NAME unable to stop"
                        fi
                fi
        log_end_msg 0
                ;;

    *)
                echo $1 option not recognized
        echo "Usage: $0 {start|stop}"
        exit 2
        ;;
esac
MilesTEG1
Posts: 11
Joined: Tue Nov 08, 2022 2:04 pm

Re: [HOWTO] start and stop VM with your NAS.

Post by MilesTEG1 »

Hello,
Thanks for the script and method to automatically start and stop VM at startup or shutdown of the nas.

As my VM's name contain space characters, I had to modify the script. And the "acpipowerbutton" command doesn't work for me, I had to change it to : "poweroff".

Here the script with some modifications, and logging add :

Code: Select all

#!/bin/sh
# link it with /usr/local/etc/init.d/S82VMlinuxmintuefi
# link it with /usr/local/etc/init.d/K60VMlinuxmintuefi
# ln -s /share/docker/_scripts/StartStopVM_LinuxMintUEFI.sh /usr/local/etc/init.d/S82VMlinuxmintuefi
# ln -s /share/docker/_scripts/StartStopVM_LinuxMintUEFI.sh /usr/local/etc/init.d/K60VMlinuxmintuefi

PKG_PATH=/usr/local/AppCentral/virtualbox
## ASPORTAL_PATH=/usr/local/AppCentral/asportal
## ASPORTAL_STAT=`apkg --info-installed asportal | grep 'Enabled:' | awk '{print $2}'`
## rcP=/usr/local/etc/init.d/P19virtualbox
## DRV_PATH=/usr/local/AppCentral/media-pack/drivers/

if [ ! -e $PKG_PATH/CONTROL/env.sh ]; then
        echo "Can't find $PKG_PATH/CONTROL/env.sh"
        exit 1
fi

# source $PKG_PATH/CONTROL/env.sh

VM_NAME="Linux Mint 21.1 UEFI"

source $PKG_PATH/CONTROL/env.sh
. /lib/lsb/init-functions

VBoxManage list vms | grep "$VM_NAME" >/dev/null 2>&1
if [ $? -ne 0 ]; then
        log_failure_msg "VM $VM_NAME seems not to exists"
        log_end_msg 255
else
        echo -e "\nVM $VM_NAME exists and will be started or stopped.\n"
fi

case "$1" in
start)
        log_daemon_msg "-> Starting VM" "$VM_NAME"
        echo -e "\n"
        VBoxManage list runningvms | grep "$VM_NAME" >/dev/null 2>&1
        if [ $? -eq 0 ]; then
                log_warning_msg "/!\\ VM $VM_NAME is already started, nothing done."
        else
                VBoxManage startvm "$VM_NAME" --type headless
                BCLE=20
                RETCOD=1
                while [ \( $BCLE -gt 0 \) -a \( $RETCOD -ne 0 \) ]; do
                        VBoxManage list runningvms | grep "$VM_NAME" >/dev/null 2>&1
                        RETCOD=$?
                        BCLE=$(expr $BCLE - 1)
                        [ $RETCOD -ne 0 ] && sleep 10
                done
                if [ $RETCOD -ne 0 ]; then
                        log_failure_msg "/!\\ VM $VM_NAME unable to start !"
                fi
        fi
        log_end_msg 0
        ;;

stop)
        log_daemon_msg "-> Stopping VM" "$VM_NAME"
        echo -e "\n"
        VBoxManage list runningvms | grep "$VM_NAME" >/dev/null 2>&1
        if [ $? -ne 0 ]; then
                log_warning_msg "VM $VM_NAME is already stopped, nothing done."
        else
                # gracefully stopping VM by short pressing power button
                # VBoxManage controlvm "$VM_NAME" acpipowerbutton
                VBoxManage controlvm "$VM_NAME" poweroff
                BCLE=20
                RETCOD=0
                while [ \( $BCLE -gt 0 \) -a \( $RETCOD -eq 0 \) ]; do
                        VBoxManage list runningvms | grep "$VM_NAME" >/dev/null 2>&1
                        RETCOD=$?
                        BCLE=$(expr $BCLE - 1)
                        [ $RETCOD -eq 0 ] && sleep 10
                done
                if [ $RETCOD -eq 0 ]; then
                        log_failure_msg "/!\\ VM $VM_NAME unable to stop"
                fi
        fi
        log_end_msg 0
        ;;

*)
        echo "$1 option not recognized or no option passed !"
        echo "Usage: $0 {start|stop}"
        exit 2
        ;;
esac

edit : I would like to have some informations about the links placed in the /usr/local/etc/init.d/ folder ?
I'd like to put some others to ensure some docker containers are well started, because It often happen that some aren't restarted after a reboot, despite setting restart: always policy in the docker-compose.yml file...
Thanks again :)
Post Reply

Return to “Virtualization”