[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

sksbir
Posts: 395
youtube meble na wymiar Warszawa
Joined: Tue Aug 25, 2015 9:23 pm

[HOWTO] start and stop VM with your NAS.

Post by sksbir »

Hi,

here is my solution to get my VM ( named "grenouille" ) being started while NAS starts, and gracefully stopped when NAS is shutdown.

- first : correct the bug in env.sh : [BUG correction] virtualbox env.sh correction.
[edit} bug solved with 4.3.36.r04

- create this script in a location of your choice : in my example, script is named /share/Syst_interne/StartStopVM_grenouille.sh

Code: Select all

#!/bin/sh
# link it with /usr/local/etc/init.d/S82VMgrenouille
# link it with /usr/local/etc/init.d/K60VMgrenouille
# ln -s /share/Syst_interne/StartStopVM_grenouille.sh /usr/local/etc/init.d/S82VMgrenouille
# ln -s /share/Syst_interne/StartStopVM_grenouille.sh /usr/local/etc/init.d/K60VMgrenouille

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/

VM_NAME=grenouille

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
- replace VM_NAME=grenouille in the script with the name of the VM you want to control
- make your script executable :

Code: Select all

chmod +x /share/Syst_interne/StartStopVM_grenouille.sh
- link the script has explained at begining of the code :

Code: Select all

# ln -s /share/Syst_interne/StartStopVM_grenouille.sh /usr/local/etc/init.d/S82VMgrenouille
# ln -s /share/Syst_interne/StartStopVM_grenouille.sh /usr/local/etc/init.d/K60VMgrenouille
that's it, your Vm should start with the NAS and stop with the NAS ( stop = simulate short press on power button )

[edit 10/2016] : be carefull with VM which is too long to stop. I have a problem with mariabd dabatase not gracefully stopped ( hosted in VM ) while VM is stopping, maybe because it lasts too long time, and NAS will not wait so long a proceed with hard shutdown.
duffoy
Posts: 13
Joined: Mon Jan 30, 2017 9:28 pm

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

Post by duffoy »

@sksbir thank you for your great explanations.

I am new whit virtualbox and Asustor and own also a AS6204T.

Now I have a VM which will be shutdown manually or via a APC snmp which works good.
Unfortunately this machine has no acpi or powerbutton.

I created this Script to start the VM but it's not working after a reboot I see only a error message.
If I do this over the cmd it works perfect

Code: Select all

#!/bin/sh

#script to start VM's during rebbot whit Virtualbox
#path for the script: /volume1/.@plugins/etc/init.d  
#comand to start VM: volume1/.@plugins/AppCentral/virtualbox/bin/VBoxManage startvm "nameofvm"

/volume1/.@plugins/AppCentral/virtualbox/bin/VBoxManage startvm "xyz"


I created the script under /volume1/.@plugins/etc/init.d
Also all the other system generated scripts are blue and ending whit @

Do you have any idea why it doesn't work?

Best D.
sksbir
Posts: 395
Joined: Tue Aug 25, 2015 9:23 pm

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

Post by sksbir »

Hi Duffoy
It's because you should load proper environment, like I do in my script:

Code: Select all

source $PKG_PATH/CONTROL/env.sh
. /lib/lsb/init-functions
This environment is already loaded when you access your NAS with SSH, that's why it works online, but not in init process.
duffoy
Posts: 13
Joined: Mon Jan 30, 2017 9:28 pm

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

Post by duffoy »

@sksbir

Thank you for your hint, I added this now to my script but unfortunately this is not working, the VM is not starting after a reboot.
Maybe I am to stupid or I think it to easy....

Code: Select all

#!/bin/sh

#script to start VM's during rebot whit Virtualbox
#path for the script: /volume1/.@plugins/etc/init.d 
#comand to start VM: volume1/.@plugins/AppCentral/virtualbox/bin/VBoxManage startvm "nameofvm"

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

/volume1/.@plugins/AppCentral/virtualbox/bin/VBoxManage startvm "xyz"
I saw in the S81virtualbox@ that this line is also included...

Code: Select all

source $PKG_PATH/CONTROL/env.sh

NAME=virtualbox

. /lib/lsb/init-functions

See also the Screenshot how the folder init.d contains and how the file is shown in the folder, it looks different then the other ones...why der are in blue color whit @ at the end?

Think this is to hig for me at the moment.
As I wrote, I need only smething to start the VM after boot/reboot

Best D.
Attachments
script.console.png
script.console.png (50.19 KiB) Viewed 19679 times
sksbir
Posts: 395
Joined: Tue Aug 25, 2015 9:23 pm

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

Post by sksbir »

Hi Duffoy

in my script , I'm using

Code: Select all

VBoxManage startvm $VM_NAME --type headless
. It seems you have forgotten the "--type headless".

I suggest you just to follow the instructions I gave in first topic, and replace "grenouille" with name of your VM anywhere : name of startstop script file, and VM=.. instruction inside the script.
- Script is intended to be general purpose : starting and stopping the VM of your choice, and not only the one name "grenouille"... And stopping the VM while you are stopping your NAS is always a good idea, even if the VM is already stopped.
- Script is intended to be more robust than a single "startvm" line : Script checks the state and existence of the VM, and starts VM only if it is stopped, stops it only if started.

color + @ means symbolic link.. type ls -l to get full detail. * and green means executable script, and so on...
duffoy
Posts: 13
Joined: Mon Jan 30, 2017 9:28 pm

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

Post by duffoy »

@sksbir

Thank you so much sksbir, this time I followed 100% of your roots and it works perfect.
Thought to make it easy but your way is working, so thats the better solution.
I only setup the Start simlink, but not the Shutdown on whit the Kxxxxx in case the VM restriction.

Is there a other way to shutdown the VM if the acpi method is not working in case the OS do not support such a button or it's
not possible to install the Virtualbox Guest Additionals.
As I wrote, in my case the SNMP of my APC UPS will handle this, hopefully.

The other Question, if I have more then one VM, needs every VM a own script.

It's sad that Asustor not implemented such a function directly in the OS/System, but your work is very helpful and great.

Thank you D.
sksbir
Posts: 395
Joined: Tue Aug 25, 2015 9:23 pm

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

Post by sksbir »

@duffoy
If your VM does not supports acpi method, you can try to use a remote command provided that you have enabled a network between VM and NAS, and that VM supports remote command. with linux for example , by using sshd.

Yes, every VM needs it's own script and simlink.

nice to know it was helpfull :)
duffoy
Posts: 13
Joined: Mon Jan 30, 2017 9:28 pm

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

Post by duffoy »

@sksbir

Thank you again.
Can you give one more hint, how I can do this or implement in to the script.
What I found was something like this

Code: Select all

ssh root@192.168.1.147 shutdown -h now
but it's not possible to send the password in the string, it's also not so secure.
Will be great if you can describe the solution or post a code snippet.

Best D.
sksbir
Posts: 395
Joined: Tue Aug 25, 2015 9:23 pm

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

Post by sksbir »

search "setting up ssh keys in Linux" , you have to generate keys with ssh-keygen , and using an another file named 'known_hosts'
duffoy
Posts: 13
Joined: Mon Jan 30, 2017 9:28 pm

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

Post by duffoy »

Hi sksbir...
A short additional question, how can I see or check if a VM is really been shut down gracefully.
I had a test and the VMs started not through the scripts so it think then the shutdown was not really processed.
Best D.
Post Reply

Return to “Virtualization”