#!/bin/sh
# link it with /usr/local/etc/init.d/S82yourVM
# link it with /usr/local/etc/init.d/K60yourVM
# ln -s /share/Syst_interne/yourVM.sh /usr/local/etc/init.d/S82yourVM
# ln -s /share/Syst_interne/yourVM.sh /usr/local/etc/init.d/K60yourVM
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=yourVMname
source $PKG_PATH/CONTROL/env.sh
. /lib/lsb/init-functions
which VBoxManage
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"
echo `date +%Y%m%d_%T` "Starting VM $VM_NAME" >> /share/Syst_interne/StartStopVM_grenouille.log
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"
else
echo `date +%Y%m%d_%T` "Started VM $VM_NAME" >> /share/Syst_interne/StartStopVM_grenouille.log
fi
fi
log_end_msg 0
;;
stop)
log_daemon_msg "Stopping VM" "$VM_NAME"
echo `date +%Y%m%d_%T` "Stopping VM $VM_NAME" >> /share/Syst_interne/StartStopVM_grenouille.log
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"
else
echo `date +%Y%m%d_%T` "Stopped VM $VM_NAME" >> /share/Syst_interne/StartStopVM_grenouille.log
fi
fi
log_end_msg 0
;;
*)
echo $1 option not recognized
echo "Usage: $0 {start|stop}"
exit 2
;;
esac