#!/sbin/sh # # http://adumont.serveblog.net/2009/09/01/virtualbox-smf-2/ # # This SMF method is distributed under the following MIT License terms: # # Copyright (c) 2009 Alexandre Dumont # # Permission is hereby granted, free of charge, to any person # obtaining a copy of this software and associated documentation # files (the "Software"), to deal in the Software without # restriction, including without limitation the rights to use, # copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following # conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. . /lib/svc/share/smf_include.sh # SMF_FMRI is the name of the target service. This allows multiple instances # to use the same script. getproparg() { val=`svcprop -p $1 $SMF_FMRI` [ -n "$val" ] && echo $val } if [ -z $SMF_FMRI ]; then echo "SMF framework variables are not initialized." exit $SMF_EXIT_ERR fi start_vm() { /usr/bin/VBoxManage startvm $1 --type vrdp } stop_vm() { # STOP_METHOD=acpipowerbutton|savestate STOP_METHOD="$( getproparg vm/stop_method )" case "$STOP_METHOD" in acpipowerbutton|savestate|acpisleepbutton|poweroff) ;; *) STOP_METHOD="savestate" ;; esac /usr/bin/VBoxManage controlvm $1 $STOP_METHOD } vm_state() { /usr/bin/VBoxManage showvminfo $1 --details --machinereadable | grep VMState\= | tr -s '"' ' ' | cut -d " " -f2 if [ $? -ne 0 ]; then echo >&2 "ERROR: Failed to get VMState for VM $1" exit $SMF_EXIT_ERR_FATAL fi } INSTANCE=$( echo $SMF_FMRI | cut -d: -f3 ) case $1 in start) VM_STATE=$( vm_state $INSTANCE ) if [ "x$VM_STATE" = "xpoweroff" ] then start_vm $INSTANCE else echo "INFO: VM $INSTANCE is in state $VM_STATE, I can't start it." fi ;; stop) VM_STATE=$( vm_state $INSTANCE ) if [ "x$VM_STATE" = "xrunning" ] then stop_vm $INSTANCE else echo "INFO: VM $INSTANCE is in state $VM_STATE, I won't stop it." fi ;; esac if [ $? -ne 0 ]; then echo "ERROR: VM $INSTANCE failed to start/stop." exit $SMF_EXIT_ERR_FATAL fi exit $SMF_EXIT_OK