It is currently Sat Dec 16, 2017 9:20 am
All times are UTC + 8 hours

[HOWTO] start and stop VM with your NAS.

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

[HOWTO] start and stop VM with your NAS.

Postby sksbir » Thu Aug 11, 2016 6:12 am

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.
sksbir
 
Posts: 250
Joined: Tue Aug 25, 2015 9:23 pm

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

Postby duffoy » Wed Feb 08, 2017 8:07 pm

@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.
duffoy
 
Posts: 13
Joined: Mon Jan 30, 2017 9:28 pm

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

Postby sksbir » Thu Feb 09, 2017 4:55 pm

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.
sksbir
 
Posts: 250
Joined: Tue Aug 25, 2015 9:23 pm

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

Postby duffoy » Tue Feb 14, 2017 2:35 am

@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 2034 times
duffoy
 
Posts: 13
Joined: Mon Jan 30, 2017 9:28 pm

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

Postby sksbir » Tue Feb 14, 2017 10:03 pm

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...
sksbir
 
Posts: 250
Joined: Tue Aug 25, 2015 9:23 pm

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

Postby duffoy » Tue Feb 14, 2017 11:53 pm

@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.
duffoy
 
Posts: 13
Joined: Mon Jan 30, 2017 9:28 pm

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

Postby sksbir » Wed Feb 15, 2017 6:08 pm

@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 :)
sksbir
 
Posts: 250
Joined: Tue Aug 25, 2015 9:23 pm

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

Postby duffoy » Wed Feb 15, 2017 6:43 pm

@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.
duffoy
 
Posts: 13
Joined: Mon Jan 30, 2017 9:28 pm

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

Postby sksbir » Sun Feb 19, 2017 2:58 am

search "setting up ssh keys in Linux" , you have to generate keys with ssh-keygen , and using an another file named 'known_hosts'
sksbir
 
Posts: 250
Joined: Tue Aug 25, 2015 9:23 pm

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

Postby duffoy » Mon Apr 10, 2017 5:07 am

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.
duffoy
 
Posts: 13
Joined: Mon Jan 30, 2017 9:28 pm

Next

Return to Virtualization

  • You cannot post new topics in this forum
    You cannot reply to topics in this forum
    You cannot edit your posts in this forum
    You cannot delete your posts in this forum
    You cannot post attachments in this forum
  • Who is online

    Users browsing this forum: No registered users and 1 guest

cron