I have a VirtualBox machine with Windows XP on a Windows XP Host.

How can I start VirtualBox as a service, so I can get a virtual machine under the hood, and I don't have to start VirtualBox in order to access my virtual machine (via VNC from the network)?

I found that VirtualBox Manage might be the way to go, but since I'm a beginner with this, I don't know where to start.

Any ideas?

link|improve this question

feedback

4 Answers

up vote 3 down vote accepted

Create a Shortcut to C:\Program Files\innotek VirtualBox\VBoxManage.exe

After the quotation marks enter: startvm <your virtual machine name>

Example:

“C:\Program Files\innotek VirtualBox\VBoxManage.exe” startvm XP

Copy/Move the shortcut to your startup folder.

p.s.: if you want to delay the Virtual Machine until your system is fully booted, you can do this in XP via Startup Delayer.

link|improve this answer
It appears to works fine, Is there anyway to "hide" the window for the virtual machine also, so it be running more like a windows service? – Jhonny D. Cano -Leftware- Jan 5 '10 at 19:41
running VBox as a service will not hide the window. you can minimize it automatically to the system tray with AWM though: eusing.com/WindowManager/WindowManager.htm – Molly7244 Jan 5 '10 at 19:59
9  
checkout "VBoxHeadless" for launching VM's with no UI. You will need to use RDP if you want to see the VM's screen. – Mark Porter Jan 5 '10 at 20:25
This is not a service. This only runs VBox when you log in - not when the machine is powered on. VBoxHeadless is the way to go for running as a service. See my answer below (which I'm about to improve per Aaron Copley's comment). – John Hart Apr 10 at 17:41
feedback

Looks like the simplest answer at this point is VBoxVMService. I haven't tried it yet, I'll try to remember to come here and update the answer if/when I do.

link|improve this answer
feedback

Just offering this as another option:

Use the builtin Windows command 'sc' to create a service. Combine this with 'vboxheadless' will get you to where you want to be.

Untested with vboxheadless specifically, but I did create a test service that runs calc.exe. You should be doing something like this:

sc create servicenamehere start= auto binPath= "C:\path\to\vboxheadless.exe --startvm name"

Refer to the documentation for sc for more information. Note: Those spaces after the equal sign are intentional.

link|improve this answer
feedback

Note that the currently accepted answer (Molly7244) actually starts the VM when you login - not when you boot the machine. It is not, in other words, a service.

For an actual service that runs when the machine boots, I use two scripts (originally from here) in conjunction with cygwin (cygrunsrv). Makes use of the VBoxHeadless mode as mentioned elsewhere on this page.

The first script runs your VM via VBoxHeadless; it gets the name of the right VM to run (and other info such as your VBOX home directory) form environment variables. The second script installs a service for a particular VM (by using cygrunsrv to call the first script with the right env. vars set). Finally there's a third file which contains common functions. If you put all of these into a directory together, you can install a new vm like so:

$ VBOX_USER_HOME="/path/to/.VirtualBox/" vboxd-install MyVMName 3333

And then start the service with "net start vboxd-MyVMName" or "cygrunsrv -S vboxd-MyVMName".

Here is the VM-running script, "vboxd":

#!/bin/bash
# from http://forums.virtualbox.org/viewtopic.php?f=1&t=23536

##
## Manages start / stop of VirtualBox virtual machines
##

## load common functions
basedir="$(readlink -f $(dirname $0))"
source "$basedir/.libcommon" || exit 1

## parse arguments
parseArg vmName "$1" "$VBOXD_VM_NAME"
parseArg vmPort "$2" "$VBOXD_VM_PORT"

## define signal handler
function onHalt {
    warn "Stopping virtual machine '$vmName'"
    "$VBOX_INSTALL_PATH/VBoxManage" controlvm "$vmName" savestate
    exit 0
}

## install signal handler; cygrunsrv uses SIGTERM by default
trap 'onHalt' TERM

## hardcode this path if you like; it's required for VBox* utils to
## find the correct VirtualBox.xml config file and is usually set
## during a call to vboxd-install.
#export VBOX_USER_HOME="$USERPROFILE\\.VirtualBox"

## default VBoxHeadless port specification
portSpec="-e \"TCP/Ports=$vmPort\""

## determine vm state
info "Querying virtual machine '$vmName' state"
vmState=$( \
    "$VBOX_INSTALL_PATH/VBoxManage" showvminfo "$vmName" \
    | grep '^State:' \
    | sed 's/State: *//' )
info "Virtual machine '$vmName' is $vmState"

## if vm state is saved, we can't specify port without an exception,
## as port spec requires modification of the (immutable) saved machine
## state. See http://www.virtualbox.de/ticket/3609
if  [ "${vmState##saved}" != "$vmState" ]; then
    ## state is saved; clear port specification
    warn "Port specification is not allowed for saved vms"
    portSpec=""
fi

## start the VM
info "Starting virtual machine '$vmName' on port $vmPort"
"$VBOX_INSTALL_PATH/VBoxHeadless" -s "$vmName" $portSpec &

## record pid of VBoxHeadless child process and wait on it
pid="$!"
info "Waiting on VBoxHeadless child process $pid"
wait "$pid"

And here is the installer script, "vboxd-install":

#!/bin/bash
# http://forums.virtualbox.org/viewtopic.php?f=1&t=23536

##
## Registers a VirtualBox virtual machine to start as a service via cygrunsrv
##

## load common functions
basedir="$(readlink -f $(dirname $0))"
source "$basedir/.libcommon" || exit 1

## test for presence of cygrunsrv utility
if [ ! -x "$(which cygrunsrv)" ]; then
    die "Utility 'cygrunsrv' is not in path"
fi

## test VirtualBox configuration
if [ -z "$VBOX_USER_HOME" ]; then
    die "Required environment variable 'VBOX_USER_HOME' is undefined. " \
     "Please ensure this variable is set to point to the directory " \
     "containing your VirtualBox.xml configuration file."
fi
configFile=$(cygpath -u "$VBOX_USER_HOME\\VirtualBox.xml")
if [ ! -e "$configFile" ]; then
    die "VirtualBox configuration file '$(cygpath -w $configFile)' not found"
fi

## parse arguments
parseArg vmName "$1"
parseArg vmPort "$2"
parseArg vmUser "$3" "SYSTEM"

## if vmUser is not SYSTEM, update userSpec
userSpec="--interactive"
if [ "$vmUser" != "SYSTEM" ]; then
    ## "interactive" option disallowed when user is specified
    userSpec="--user \"$vmUser\""
fi

## install the service
cygrunsrv \
    --install "vboxd-$vmName" \
    --path "$basedir/vboxd" \
    --env "VBOXD_VM_NAME=$vmName" \
    --env "VBOXD_VM_PORT=$vmPort" \
    --env "VBOX_USER_HOME=$VBOX_USER_HOME" \
    --desc "VirtualBox virtual machine '$vmName' on port $vmPort" \
    $userSpec \
    --type auto \
    --termsig TERM \
    --shutsig TERM \
    --neverexits \
    --preshutdown \
    || die "Failed to install service"

And, finally, here is the ".libcommon" script referenced by both of these:

# -*-shell-script-*-
# from http://forums.virtualbox.org/viewtopic.php?f=1&t=23536

SCRIPT="$(basename $0)"
BASEDIR="$(readlink -f $(dirname $0))"
[ -z "$LOGLEVEL" ] && LOGLEVEL=2
[ -z "$LOGDATEFORMAT" ] && LOGDATEFORMAT="%Y-%m-%d %H:%M:%S "

function log {
    local now=""
    [ -n "$LOGDATEFORMAT" ] && now=$(date +"$LOGDATEFORMAT")
    echo "$SCRIPT $now$@" >&2
}

function debug {
    [ "$LOGLEVEL" -lt 3 ] && return
    log "[DEBUG] $@"
}

function info {
    [ "$LOGLEVEL" -lt 2 ] && return
    log "[INFO]  $@"
}

function warn {
    [ "$LOGLEVEL" -lt 1 ] && return
    log "[WARN]  $@"
}

function error {
    log "[ERROR] $@"
}

function die {
    error "$@"
    exit 1
}

function parseArg {
    local _name="$1"
    local _value="$2"
    local _default="$3"
    if [ -z "$_value" ]; then
        if [ -z "$_default" ]; then
            die "Required argument '$_name' is undefined"
        fi
     if [ "$_default" = "*EMPTY*" ]; then
         _value=""
     else
            _value="$_default"
     fi
    fi
    debug "$_name=\"$_value\""
    eval "$_name=\"$_value\""
}

This solution works great for me; hopefully you'll have similar luck.

link|improve this answer
1  
+1 for "not a service"... -1 for linking to the solution rather than including it in the answer. Vote is null. :) Consider revising to include the solution as link rot is detrimental to the longevity of this answer. – Aaron Copley Jan 3 at 22:07
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.