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
- A version of VirtualBox that supports the vboxheadless interface and vrdp (vrdp is a requirement of vboxheadless).
- 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.
- Download and Install Windows Service Commander from here
- For troubleshooting you might also want to get a copy of Process Explorer from Sysinternals from the technet site - > here
- You must be comfortable editing the registry.
- The first step is to build your virtual machine as you would any virtualbox machine and make sure it works as you wish.
- 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.
- 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.
- 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.
- 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\).
Notes
Unfortunately there are a few caveats and issues that may arise from this method.
- 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.
- 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.
- 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:- the start builtin command
Nirsoft's NirCmd tool
Naughter Software's StartX tool
CMDOW
- the start builtin command
- 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.
- 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).
- Use Process Explorer to see what is really going on.
I hope this is useful to someone (if only to see why not to use it: ).