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
- make your script executable :
Code: Select all
chmod +x /share/Syst_interne/StartStopVM_grenouille.sh
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
[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.