Page 1 of 1

HOWTO - virtualbox as a service on Windows (NT Wrapper)

Posted: 9. Mar 2009, 15:13
by rasker
This howto describes a method of running virtualbox as a service on Windows using the ntwrapper service wrapper

NT Wrapper from Duodata Software is a tool 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.

NT Wrapper is an alternative tool to srvany.exe from Microsoft. It expands on the functionality provided by srvany.exe to add more flexible control of the application that you want to run as a service.

Pre-requisites
  1. A version of VirtualBox that supports the vboxheadless interface and vrdp (vrdp is a requirement of vboxheadless).
  2. Install a copy of NT Wrapper Lite which can be downloaded from 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
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. Start NT Wrapper Manager Lite and select Service -> New from the menu. Run through the tabs entering data as follows:
    1. General Tab : Enter a Service Name (This is a general service name for NT Wrapper and applies to NT Wrapper only, not your virtual machines, so something like "Vbox Machines" would work), Display name and description. Leave the Service Logon section the same (LocalSystem and Interact with Desktop checked).
    2. Enhanced Tab : Leave as default
    3. Dependancies Tab : Leave as default
    4. Applications to Run As a Service Tab : Nt Wrapper can run multiple applications together under a single instance of NT Wrapper similar to how svchost works for Microsoft OS native services but without individual control of each service. For each service (virtualbox machine in this case) run through the tabs on this page entering data as follows:
      1. General Tab
        1. Work directory : Enter a working directory. you can point this at the virtualbox program directory (c:\Program Files\sun\xvm virtualbox\)
        2. Executable : Browse to and select the vboxheadless.exe in the c:\Program Files\sun\xvm virtualbox\ directory
        3. Parameters : -startvm <name of virtual machine> -p <any port number>
        4. Own log file : You can set a log file to capture the output from stdout and stderr (e.g. c:\myvirtmach.log)
      2. Enhanced Tab : Leave the defaults except check the Is Console App. checkbox
      3. Timings : Defaults are ok here, (indeed they are meaningless in the context of VirtualBox machines as services as they apply to the NT Wrapper Service not the Virtual machines)
      4. Environment : Leave defaults
      5. Redirect Output Tab : You should give each machine an individual logfile for stdout. This will capture any error message if your virtual machine is not starting.
      6. Log On Tab : Leave the defaults.
    5. If this is the first service being setup with NT Wrapper then once you are happy with the configuration click on the Install button. The Install button installs the NT Wrapper service with the application you have just defined. If you want to add other virtual machines then click New on the Applications to Run As a Service and fill out the tabs as before. NT Wrapper Lite has no limit on the number of applications it manages from a single service instance.
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. Shutting down the virtual machine will only stop the NT Wrapper service if it is the last NT Wrapper managed application to exit. With NT Wrapper Lite you can only run one service. If you need to start/stop more than one machine then you need the NT Wrapper Pro version which can create one service per machine.
  3. If you have more than one virtual machine being started by NT Wrapper and you shut down one of the virtual machines from within the virtual machine itself then you cannot start the virtual machine again using NT Wrapper without restarting the NT Wrapper server. This means that all the virtual machines will be terminated and restarted. Effectively this means that you should only run one virtual machine using NT Wrapper.
  4. Stopping the service usually successfully terminates the vboxheadless process. 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 (as the virtual machines configuration xml file remains locked).
  5. 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:
  6. Use Process Explorer to see what is really (really!) going on.