Page 1 of 2

HOWTO - virtualbox as a service on Windows (SRVANY.EXE)

Posted: 7. Mar 2009, 04:59
by rasker
This howto describes a method of running virtualbox as a service on Windows using the srvany.exe service wrapper

srvany.exe is a Windows Resource Kit tool from Microsoft for running any application or script using the Windows service manager. It wraps a normal application/script providing it with an interface to Windows service manager. This allows windows to start virtualbox at boot and stop it (in a very ugly way!) at shutdown or at your request. As I suggested in the previous sentence the main problem with this method is that when the service is stopped the process is terminated (without actually shutting the virtual machine down normally). This is probably not good and so this method is really only for educational/informational purposes. However it may work for your particular situation.

Pre-requisites
  1. A version of VirtualBox that supports the vboxheadless interface and vrdp (vrdp is a requirement of vboxheadless).
  2. A copy of the srvany.exe and instsrvw.exe binaries which can be gotten either on the Microsoft site or from the Download link here.
  3. Download and Install Windows Service Commander from here
  4. For troubleshooting you might also want to get a copy of Process Explorer from Sysinternals from the technet site - > here
  5. You must be comfortable editing the registry.
The Steps
  1. The first step is to build your virtual machine as you would any virtualbox machine and make sure it works as you wish.
  2. Next, close the VirtualBox GUI and copy the virtualbox.xml file from the c:\Documents and Settings\<your_login_name>\.VirtualBox\ directory to the c:\Documents and Settings\LocalService\.VirtualBox\ directory. Create the .VirtualBox directory if it doesn't already exist. This allows VirtualBox to get the right config when it is executed from the LocalService account.
  3. Create a directory in c:\Program Files\ called srvany. Copy srvany.exe, Instsrv.exe and Instsrvw.exe from the archive downloaded in step 2 to the srvany directory.
  4. Start Windows Service Commander and select Tools -> Install New Service from the menu. this will start a wizard to help you set the service up. Click Next
    • In the Enter Path to the service executable: box, click Browse and browse to and select the c:\Program Files\srvany\srvany.exe file. Leave the Service will run in it's own process radio button selected and click Next.
    • Give your service a name e.g. Virtualbox_Linux, a Display Name (which can be the same as the Service Name) and a brief description of the virtual machine. Click Next.
    • Leave the LocalSystem account radio button selected and click the Allow service to interact with desktop checkbox. Click Next.
    • Set the startup options as you wish (defaults are good for now). Click Next twice (skip the Dependencies page) and click Finish.
    If you look down the list in Windows Service Commander you will see your newly created service!
  5. The next step is to provide srvany with information about the program you want to run. Click START -> Run and enter regedit in the runbox and click OK.
    • Using regedit browse to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ and select the service you have just created (e.g. Virtualbox_Linux).
    • Click Edit -> New -> Key from the menu and create a new key called Parameters. Click on the newly created Parameters Key so it is highlighted.
    • Right click in the right window and select New -> string value from the menu. Type in Application in the Name field of the newly created key.
    • Repeat the above step and create two more keys called AppDirectory and AppParameters.
    • Double click the Application key and enter c:\windows\system32\cmd.exe in the Value data field.
    • Double click the AppParameters key and enter /c "c:\Program Files\Sun\xVM Virtualbox\VBoxHeadless.exe" -startvm <your virtualmachine e.g. Virtualbox_Linux> -p <some port number that is not 3389> in the Value data field. We use a port number that is not 3389 to not conflict with the windows rdp server if it is configured to be used.
    • Double click the AppDirectory key and enter the path to the Virtualbox program directory in the Value data field (c:\Program Files\Sun\xVM Virtualbox\).
That is it. You can now start your virtual machine from the Services management console, Windows Service Commander or using net start <service name> from a cmd prompt.

Notes

Unfortunately there are a few caveats and issues that may arise from this method.
  1. Stopping your virtual machine usually terminates the VirtualBox process without a thought. This might mean that your virtual machine hard disk could get corrupted! Use a journaling file system in the virtual machine like ext3/ext4 to help alleviate this problem.
  2. Stopping the service usually successfully terminates the cmd.exe and the vboxheadless processes. Unfortunately the VBoxSvc process does not always do the right thing and go away. This can leave locked files behind which prevent you from either starting the virtualbox gui or accessing the machine from the gui if the gui can be started.
  3. This method opens a command prompt window. Closing this window will kill the vboxheadless process but not the srvany process so the service still looks like it's running. There are various utilities that you can replace cmd.exe with to try and start the service without the cmd window.
    Cmd.exe like tools with hidden console features:
  4. Using srvany.exe to start a cmd.exe/vboxheadless service makes it difficult to capture the (stderr/stdout) output of the virtual machine. This is problematic when the virtual machine is failing to start, usually the error message flashes on the screen and then the cmd window exits. You can add a " >c:\virtsvr.log" to the end of the text in the AppParameters to capture the output. This is only useful if the virtualmachine is exiting quickly as usually nothing useful is flushed to the log file until the virtual machine exits. alternatively if you get one of the above cmd replacements working they can usually redirect the output to a log file as well.
  5. Starting and stopping a virtualbox machine configured as a service with this method seems to leave behind vboxsvc processes (they sometimes don't exit when the virtual machine is killed). This means that files may get permanently locked. Killing the vboxsvc processes doesn't seem to work either so the only recourse is to reboot the machine (which also takes much longer as it waits to terminate the vboxsvc processes left behind).
  6. Use Process Explorer to see what is really going on.
I will follow up this post with how-tos of other methods used to start Virtualbox as a Windows Service if I get the time.

I hope this is useful to someone (if only to see why not to use it: :D ).

Re: HOWTO - virtualbox as a service on Windows (SRVANY.EXE)

Posted: 10. Mar 2009, 00:54
by dschulz
I will follow up this post with how-tos of other methods used to start Virtualbox as a Windows Service if I get the time.
Please do. Srvany.exe is the only tool that I know of that can provide this sort of functionality. I have followed the exact steps provided and they work well. The problem is -- as you said -- when the system is shut down. I've been experimenting with running vboxmanage controlvm <vmname> savestate in the Windows XP shutdown script in group policy, but it doesn't work and I'm having trouble getting any debugging information out of it. Any ideas?

Re: HOWTO - virtualbox as a service on Windows (SRVANY.EXE)

Posted: 21. Mar 2009, 19:34
by rasker
@dschulz - The other howto's are in my sig. Also you can try vboxvmservice - check the windows as host forum, it's a sticky.

The best way to get useful info is to use redirection on the commandline, which is done using the > operator. Google 'cmd redirection'. Redirecting the output of Vboxmanage will probably not give all the info you need. You might also have to redirect the output of your virtualbox machine. e.g add >c:\my_vm_log.txt to your commandline.

Re: HOWTO - virtualbox as a service on Windows (SRVANY.EXE)

Posted: 21. Mar 2009, 19:55
by dschulz
Thanks! I'll definately check those out.

Re: HOWTO - virtualbox as a service on Windows (SRVANY.EXE)

Posted: 17. May 2009, 06:37
by shane297
Hello Rasker,
I have tried your How To: for srvany instructions with no luck. I am using a Vista 64bit host and Windows SBS 2008 guest.I have a question about step 2 (see below) of your instructions. Vista has replaced "c:\documents and settings" folder with "c:\users" folder. There is no localservice folder under c:\users. Do you know what I can do to accomadate this step? I have verified the c:\users\<login_name>\.VirtualBox\virtualbox.xml and I tried running the service with this login account. The service starts but does not launch the VM.

(step 2) Next, close the VirtualBox GUI and copy the virtualbox.xml file from the c:\Documents and Settings\<your_login_name>\.VirtualBox\ directory to the c:\Documents and Settings\LocalService\.VirtualBox\ directory. Create the .VirtualBox directory if it doesn't already exist. This allows VirtualBox to get the right config when it is executed from the LocalService account.

Thanks, Shane

Re: HOWTO - virtualbox as a service on Windows (SRVANY.EXE)

Posted: 6. Jun 2009, 11:44
by rasker
Hi Shane

Sorry, I'm skipping Vista for Windows 7 so I don't really know much about how Vista is different with respect to XP. Basically you need to use the user account that shows up in the service manager. You can use your own user config to place the files in the right place for the service user.

Please post back any new info you have.

Thanks
R

Re: HOWTO - virtualbox as a service on Windows (SRVANY.EXE)

Posted: 12. Aug 2009, 10:07
by renoes
The method with srvstart works pretty well, however it does not stop the service at shutdown properly, such that it stops with poweroff.

What however works nicely is the "net stop <service>" command.

I used it in the general shutdown script of my computer and looks like it works (savestate returns).

----------------------------

Shutdown scripts can be accessed via gpedit.msc under computer configuration/windows settings/scripts.

I created a .bat file with a net stop command.

Furthermore is it possible to activate the output of that shutdown see script. gpedit.msc Administrative templates/system/scripts/

When you install the service with the windows service commander you can also modify the rights of common users (for example to start or stop the service) (security options)

------------------------------

Furthermore I intend to have a user that can use the VBoxmanage commands.
For that this user will have to have a console which runs under local service rights.

This can be done with the psexec tool from sysinternals (windows resource kit tools) with the -i -s options (but only from a shell with admin rights):

1. runas /user:<admin> cmd
2. psexec -i -s cmd (inside the new shell)

within this shell Virtualbox sees the machines running under localsystem.

--------------------------------
Interesting could be also a service that runs a shell and can be started and stopped only by the user that manages the Virtualbox machines.

Anyway all this is slightly dangerous because of the localsystem rights (which are practically admin rights)

But probably the service could be also run under the useracount or the localservice account
The problem is only that Virtualbox sees its running virtual machine

---------------------------------
For hiding the console window I used startx (which also allows you to set the priority better)


---------------------------------

I realised that even Virtualbox.exe can be run under localsystem and it sees the running virtual machine!
It does neither shutdown the VBoxheadless.exe when it is closed. Interesting...

best
Sönke Schmachtel

Re: HOWTO - virtualbox as a service on Windows (SRVANY.EXE)

Posted: 12. Aug 2009, 11:03
by rasker
renoes, thanks for the feedback. You probably wanted to post this in the srvstart howto rather than the srvany howto.

R

Re: HOWTO - virtualbox as a service on Windows (SRVANY.EXE)

Posted: 3. Nov 2009, 18:55
by egoz
I use Windows 7. To create a service I use this srvany.exe because I cannot get srvstart to work. In the attachment you can find my small java program to gracefully shutdown my VM. I have used it for 2 days now, and it seems that my VM always shut down gracefully. You must have Java to use this.

This small program use runtime.exec() to start and stop VirtualBox and add a java's runtime.addShutdownHook so your VM get a graceful shutdown.

Here are things that I do:

In Windows registry at your srvany's service node:

Code: Select all

-AppParameters: /c vmasservice.bat
-AppDirectory: <vmasservice.bat directory>
-Application: C:\Windows\System32\cmd.exe
In vmasservice.bat

Code: Select all

Set JAVA_HOME=<Your java installation>
In bin/gracefulcommand/gracefulcommand.properties

Code: Select all

startupIdentifier= <Startup command identifier for logging, not esential>
startupCommand= <Full path command + parameters to run in startup>
startupDir= <Full path directory for startup command>

shutdownIdentifier= <Shutdowncommand identifier for logging, not esential>
shutdownCommand= <Full path command + parameters to run in shutdown>
shutdownDir= <Full path directory for shutdown command>
my own properties look like this:

Code: Select all

startupIdentifier= VHeadless startvm
startupCommand= C:\\Program Files\\Sun\\VirtualBox\\VBoxHeadless.exe -startvm Debian
startupDir= C:\\Program Files\\Sun\\VirtualBox\\

shutdownIdentifier= VBoxManage savestate
shutdownCommand= C:\\Program Files\\Sun\\VirtualBox\\VBoxManage.exe controlvm Debian savestate
shutdownDir= C:\\Program Files\\Sun\\VirtualBox\\
: (
I tried to write process output to sys.out but it seems that my app has to exit in a certain amount of time or Windows will forcefully kill it. Anybody knows about this more? So I didn't write that process output and tried to keep the code as small as possible with not using a third party library like log4j.

Cheers

Re: HOWTO - virtualbox as a service on Windows (SRVANY.EXE)

Posted: 3. Nov 2009, 21:00
by dmd
The download link for srvany.exe and instsrvw.exe binaries is broken.

Re: HOWTO - virtualbox as a service on Windows (SRVANY.EXE)

Posted: 3. Nov 2009, 22:02
by egoz
dmd, I download it from microsoft, http://www.microsoft.com/downloads/deta ... laylang=en

Btw, I read about Apache Daemon's Procrun here :http://blog.platinumsolutions.com/node/234. Then, with 1 hour of coding I can integrate it to my previous code.And the result is awesome, I can use

Code: Select all

net stop <service_name>
to stop my VM and run without restart with VirtualBox.exe

I include the program in the attachment, and you only need Java to use this program, no srvany anymore. It needs refactoring and documentation (License I guess) though.

To use it you must set JAVA_HOME environment variable, then edit the classes/vboxprocrun/vboxprocrun.properties. Here is mine:

Code: Select all

startupCommand= C:\\Program Files\\Sun\\VirtualBox\\VBoxHeadless.exe -startvm Debian
shutdownCommand= C:\\Program Files\\Sun\\VirtualBox\\VBoxManage.exe controlvm Debian savestate
vBoxDir= C:\\Program Files\\Sun\\VirtualBox\\
then in the VBoxProcrun directory type:

Code: Select all

vboxprocrun.bat install [service_name]
service_name isn't mandatory, and Voila! It's done.

and to remove the service, you could type:

Code: Select all

vboxprocrun.bat remove [vmname]

Re: HOWTO - virtualbox as a service on Windows (SRVANY.EXE)

Posted: 14. Dec 2009, 19:24
by hero
Hi egoz,
Have read your solution and like it very well.
Have tried to install it based on your attached ZIP file.
Could manage to install (and also remove) a service for my VM.
Problem come up when I tried to start that new service.
The Service stopped again immediately and in the log of VBoxProcrun I see following entries:
---------------------
[14-12-2009 17:42:39]Service Started
[14-12-2009 17:42:39]Joining with runner command
[14-12-2009 17:42:39]Starting Runner
[14-12-2009 17:42:40]VirtualBox Headless Interface 3.0.8
[14-12-2009 17:42:40](C) 2008-2009 Sun Microsystems, Inc.
[14-12-2009 17:42:40]All rights reserved.
[14-12-2009 17:42:40]Runner exit value: -2135228415
[14-12-2009 17:42:40]
[14-12-2009 17:42:40]Invalid machine name!
[14-12-2009 17:42:40]
[14-12-2009 17:42:40]Service stopped

-----------------------

My values in 'vboxprocrun.properties' are:
--------------------------------
startupCommand= C:\\Program Files\\Sun\\VirtualBox\\VBoxHeadless.exe -startvm W2008EE
shutdownCommand= C:\\Program Files\\Sun\\VirtualBox\\VBoxManage.exe controlvm W2008EE savestate
vBoxDir= C:\\Program Files\\Sun\\VirtualBox\\

--------------------------------

When I start the VM manually (VBoxHeadless.exe -startvm W2008EE) in ..\VirtualBox,
it starts normally.


Any clue on that issue?
Many thanks in advance for your -very welcome- feedback.

Re: HOWTO - virtualbox as a service on Windows (SRVANY.EXE)

Posted: 23. Jan 2010, 10:24
by tadak
Hey guys,
I have been struggling with this for quite some time.
I run Windows 2008 64 bit and need to start a vm at startup.
The srvany.exe seemed like a good idea, but it does not run properly in a 64 bit environment.
Then I read about the shutdown scripts everyone is using.
I created a bat file to start VBoxHeadless and put it in the startup scripts of the local group policy.
This seems to work great.

Re: HOWTO - virtualbox as a service on Windows (SRVANY.EXE)

Posted: 23. Jan 2010, 18:48
by rasker
@tadak: The issue isn't so much starting a virtual machine automatically. As you found there are a few ways to do this (group policy script, startup folder cmd file etc etc.). The issue is that when you shut down the host machine, the host os just kills the virtual machine process rather than gracefully shutting down the guest machine. Many times this won't be an issue but there is some (quite high!) chance that this will corrupt the file system on the virtual disk and either corrupt your data or make the machine unbootable.

Re: HOWTO - virtualbox as a service on Windows (SRVANY.EXE)

Posted: 13. Jun 2010, 09:36
by armorris007
Windows 7 users will be interested in the new location of the LocalService profile:
C:\Windows\System32\config\systemprofile\.VirtualBox

Copy the VirtualBox.xml file into here and all should work.

(Ensure you have modified the file to make the references to the <VM>.XML absolute.