Home » Questions » Computers [ Ask a new question ]

How can I run a VirtualBox machine as a service on Windows XP ?

How can I run a VirtualBox machine as a service on Windows XP ?

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

Asked by: Guest | Views: 165
Total answers/comments: 3
bert [Entry]

"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."
bert [Entry]

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.
bert [Entry]

"If the vboxd scripts are giving you as much trouble as they did me, you might want to try this; it's a rather simpler solution, implemented in Perl, which provides the same functionality. Specifically, the VM starts up with the system (assuming the service is set to start automatically, which is the default), and goes down cleanly when the service is stopped.

Major differences from the vboxd solution, aside from being considerably simpler to deal with in my experience, are that this script doesn't pass a VRDE port setting to VBoxHeadless, and that the VM shutdown is handled via ""acpipowerbutton"" rather than ""savestate"".

If this doesn't suit your purposes, it's easy enough to change; in the first case, add '-e ""TCP/Ports=[,port...]""' to the VBoxHeadless command line string ($cmd), and in the second case, just change ""acpipowerbutton"" to ""savestate"" in the SIGTERM handler.

Here's the Perl script:

#!/usr/bin/perl
# Service wrapper for VirtualBox VM.

use strict;

# Windows-style path pointing to your VirtualBox home directory
# (i.e., where your VirtualBox.xml lives)
my $vboxhome = 'c:\\Documents and Settings\\Aaron\\.VirtualBox';

# Cygwin path pointing to the VirtualBox install directory
my $vboxpath = '/cygdrive/c/Program Files/Oracle/VirtualBox';

# Name or UUID of your virtual machine
my $vmname = '{83dfb4b1-4243-4a07-9d37-9df5573792d8}';

$ENV{'VBOX_USER_HOME'} = $vboxhome;

my $exit_handler = sub {
print ""Caught SIGTERM; shutting down VM.\n"";
exec(""'$vboxpath/VBoxManage.exe' controlvm $vmname acpipowerbutton"");
};

$SIG{TERM} = $exit_handler;

print ""[debug] pid $$\n"";

# Command line for VBoxHeadless - add your VRDE port here if you want one
my $cmd = ""'$vboxpath/VBoxHeadless.exe' --startvm $vmname 2>&1 1>/dev/null"";
print ""[debug] $cmd\n"";
system($cmd);

print ""VM died unexpectedly; exiting.\n"";

And here's the cygrunsrv command line I used to install the Windows service:

cygrunsrv --install '<service-name>' --desc '<description>' \
--path '/usr/bin/perl' --args '<full-path-to-service-script>' \
--chdir '<path-to-service-script-dir>' --termsig TERM --shutsig TERM \
--preshutdown --interactive

From there, it should just be a matter of issuing 'sc start <service-name>', and you should be good to go -- remember that, by default, cygrunsrv services log to /var/log/<service-name>.log, so if anything misbehaves, that's the first place to look for a reason why.

One thing to keep in mind is that, when you run the VM this way, it will run under the built-in SYSTEM account, rather than your own. This being the case, you won't be able to administer the VM via the VirtualBox console while it's running as a service; it will appear to be in the ""Powered off"" state, and you won't be able to get to its settings. (Trying to power on the VM while it's running as a service is not recommended -- I don't know what will happen, but at best it'll be a no-op, and at worst it'll hose your VM.) If you need to change the VM's settings or access the console, stop the service first (and wait for VBoxHeadless.exe to exit), and then configure it/power it up in the VirtualBox console as you normally would.

You could probably solve this problem by installing a second interactive service to run the VirtualBox console; since that would run the console under the SYSTEM account as well, it'd most likely be able to see and manage the running VM. I haven't had a need to do that myself, so I'm not sure it would work, but I don't see a reason why it wouldn't. (If you try it, I'd appreciate a comment to this answer, letting me know how it worked out for you. Thanks!)

Hope this helps!"