Johannes Kastl wrote:
> On 2/27/09 1:06 PM Freek de Kruijf wrote:
> 
>> In that case you need to be logged on using KDE or GNOME.
> 
> Chris asked about an Xsession.
> 
>> I want the virtual machine to be started when the host system is started. 
>> For 
>> this I am looking for an init script in /etc/init.d/ to start and stop a 
>> virtual machine. In case I can't find one I will write it myself, it can't 
>> be 
>> very difficult.
> 
> Put that script in /etc/init.d/VM-autostart, make it executable.
> Depending on the host OS you can use a skeleton like
> /etc/init.d/skeleton (openSUSE), I assume Debian etc. do also have a
> script that you can modify.
> 
> One important point is how to tell the script to wait until the VM is
> suspended/powered off/...
> 
> Regards,
> OJ
> 

My script is as follows: you can create it as /et/init.d/vbox-headless or
something and then use chkconfig to turn it on (on OpenSuSE I think it even
appears in the Yast services section...).
Set the VBOXNAME variable near the top of the script to the name of your virtual
machine (or the UUID)...
You could easily have several of these to handle multiple VMs.
Note that in this script, the standard "stop" function does a save-state
shutdown of the VM - I figure that this is what I want mostly when the host goes
down.
To terminate the VM, I set the "kill" function to do an ACPI poweroff which
cleanly shuts the VM down (as long as your VM is listening for ACPI signals...).
You can then call that with "service vbox kill".

#!/bin/sh
### BEGIN INIT INFO
# Provides:       vbox-headless
# Required-Start: $network $remote_fs
# Required-Stop:
# Default-Start:  3 5
# Default-Stop:
# Description:    Start the virtual machine
### END INIT INFO

. /etc/rc.status
rc_reset

VBOXNAME="vbox"

vboxmanage()
{
    CMD="/usr/bin/VBoxManage -nologo $@"
    su - vbox -c "$CMD"
}


case "$1" in
    start)
        echo -n "Starting Virtual Box $VBOXNAME"
        vboxmanage startvm "$VBOXNAME" -type vrdp
        rc_status -v
    ;;
    stop)
        echo -n "Shutting down Virtual Box $VBOXNAME"
        vboxmanage controlvm "$VBOXNAME" savestate
        rc_status -v
    ;;
    kill)
        echo -n "Killing Virtual Box $VBOXNAME"
        vboxmanage controlvm "$VBOXNAME" acpipowerbutton
        COUNT=100
        while [ $COUNT -gt 0 ]
        do
                vboxmanage showvminfo "$VBOXNAME" -machinereadable | grep -q
'VMState="poweroff"' && break
                COUNT=$((COUNT-1))
                sleep 1
                echo -n "$COUNT ... "
        done
        test $COUNT -gt 0
        rc_status -v
    ;;
    restart)
        $0 stop
        $0 start
        rc_status
    ;;
    pause)
        vboxmanage controlvm "$VBOXNAME" pause
        rc_status -v
    ;;
    force-reload | reload)
        $0 kill
        $0 start
        rc_status -v
    ;;
    status)
        echo -n "Checking for Virtual Box $VBOXNAME: "
        vboxmanage showvminfo $VBOXNAME | grep "State:"
        rc_status -v
    ;;
    probe)
        rc_failed 3
        rc_status -v
    ;;
    *)
        echo "Usage: $0 
{start|stop|status|kill|restart|force-reload|reload|probe}"
        exit 1
    ;;
esac
rc_exit




-- 
Peter Ford, Developer                 phone: 01580 893333 fax: 01580 893399
Justcroft International Ltd.                              www.justcroft.com
Justcroft House, High Street, Staplehurst, Kent   TN12 0AH   United Kingdom
Registered in England and Wales: 2297906
Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 1XS

_______________________________________________
vbox-users mailing list
[email protected]
http://vbox.innotek.de/mailman/listinfo/vbox-users

Reply via email to