srvstart.exe is a tool written by Nick Rozanski 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 at shutdown or at your request. Srvstart.exe is a better solution than Microsoft's srvany.exe and NT Wrapper because it allows you to configure how a service should be stopped properly. When you first start using srvstart you might get some vebose feedback from the program which can be confusing but you should push through this as srvstart is a good program to use for our purpose (safely start/stop Virtualbox machines). This howto will run through the steps needed to configure a single virtual machine. To configure more you just need to repeat the howto with different service names and different virtual machine names.
Pre-requisites
- A version of VirtualBox that supports the vboxheadless interface and vrdp (vrdp is a requirement of vboxheadless).
- A copy of the srvstart.exe binary which can be gotten from here. The main website for srvstart is Nick Rozanski's website.
- 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
- 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. (Each time you want to change the hardware configuration of a virtual machine running as a servie, stop the machine running, make the coinfiguration change using the virtualbox frontend, test the virtual machine, and once all is how you want it to be, repeat this step).
- Create a directory in c:\Program Files\ called srvstart. Copy (extract) all the files from within the zip archive you downloaded earlier to the srvstart directory (particularly, logger.dll, srvstart.dll, srvstart.exe, svc.exe).
- Next We need to create a configuration file that configures srvstart and describes our service to srvstart. Using your notepad or your favourite text editor create a file called <virtual machine name>.srvstart.ini in c:\program files\srvstart\. e.g. c:\program files\srvstart\VBOX_LINUX.srvstart.ini.
- paste this into the file (assuming your vmachine is called VBOX_LINUX, see below for a complete explanation) :
Save this file.
Code: Select all
env=VBOXGUI="C:\Program Files\Sun\xVM VirtualBox\virtualbox.exe" env=VBOXHEADLESS="C:\Program Files\Sun\xVM VirtualBox\vboxheadless.exe" env=VBOXWEBSRV="C:\Program Files\Sun\xVM VirtualBox\vboxwebsrv.exe" env=VBOXMANAGE="C:\Program Files\Sun\xVM VirtualBox\VBoxManage.exe" env=VBOX_PROG="C:\Program Files\Sun\xVM VirtualBox\" env=VBOX_BASE="d:\vbox\" env=VBOX_MACHINES="d:\vbox\machines" env=VBOX_VDI="d:\vbox\VDI" env=VBOX_MACHINE1=vbox_linsrv [VBOX_LINUX] startup=%VBOXHEADLESS% -startvm VBOX_LINUX shutdown_method=command shutdown=%VBOXMANAGE% controlvm VBOX_LINUX savestate debug=0 debug_out=>d:\vbox\VDI\VBOX_LINUX.log
- 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\srvstart\srvstart.exe file.
- Append the following to the command line : svc VBOX_LINUX -c "c:\Program Files\srvstart\srvstart.ini"
- check that it looks something like :
Code: Select all
c:\Program Files\srvstart\srvstart.exe svc VBOX_LINUX -c "c:\Program Files\srvstart\srvstart.ini"
- Leave the Service will run in it's own process radio button selected and click Next.
- Give your service a name e.g. VBOX_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.
Notes
As usual there are a few caveats and issues. However the srvstart method is better than using srvany or NT wrapper and the like and perhaps nearly as good as vboxvmservice .
- A useful feature is that once you have setup a Windows service to run srvstart you can change the configuration just by editing the ini file and restarting the service. No need to edit the Windows Service anymore.
- The parameter after the svc parameter in the service command line should be exactly the same as the keyword in the ini file which is enclosed in square brackets. Within the ini file, you can change the virtualbox machine name to your own virtual machine (the parameters after the startvm and controlvm switches).
- Don't assign a startup_dir keyword if the startup directory has white space in it. It seems to never work even with the environment variable work around. This is not a big issue as it is not usually needed anyway.
- Use Windows Service Commander to create/delete/edit windows services. The included SVC tool and the srvstart install command's are a bit flaky (parsing parameters).
- The key reason to use srvstart is because we can stop the virtual machine in the correct way (the above configuration saves the state) by using the vboxmanage controlvm command. You could also try an acpi shutdown. Saving state and closing is probably good enough though.
- srvstart does not do any smart checking of the parameters passed to it. In particular it fails to Do The Right Thing (tm) when passed parameters with white space in them. This is why some parameters are passed, quoted, in environment variables, more because this works (as in worksaround) than because it is the right way.
- If possible define the vrdp port number in the machine settings not the vboxheadless command line if you are using the savestate method of shutting down. You can get a conflict which prevents the virtual machine from starting up.
- Stopping the service usually successfully terminates the vboxheadless processes. Unfortunately the VBoxSvc process does not always do the right thing and go away. This is particularly true if you start the virtualbox Gui while the service is started. 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 and other issues.
- This method opens a console window (note not a command window). Virtualbox fails with some weird error if it can't send output to stdout (I'm guessing) so it is essential that the console window is allowed. Closing this window will kill the vboxheadless process. There are various utilities that you can use to try and start the service without the console window (by running the vboxheadless from a batchfile for example and running the batchfile as a service but this is messy and unreliable).
- Use Process Explorer to see what is really going on.
- Some error codes you might see from srvstart in the Application Event log:
- Srvstart will usually throw error messages from the win32 api which are usually related to some problem with createprocess(). here is a cheatsheet
- Error 267 and Error 2: Win32 directory/filenot found errors. If you have double checked the parameter then this is usually a problem with srvstart not parsing input parameters properly (stopping at the first white space). Assign the parameter (with quoting) to an environment variable with the env keyword and use the env. variable (using the usual %var% format) in place of the parameter.
- -xxxxxxxxx: (a large minus number like exit code -2135228415) virtualbox probably could not create/find a stdout/stderr. Tick the Interact with desktop checkbox in the windows service configuration.
- paste this into the file (assuming your vmachine is called VBOX_LINUX, see below for a complete explanation) :