31 December 2010

Virtual machine as Windows service

A follow up from
Synology as Web Gateway (part one)
Once you have a virtual server up and running in Virtualbox, the question of starting it automatically in the background may be solved by the use of ServiceMan.

ServiceMan is a small win32 app i wrote as Virtualbox (3.xx) is not natively service-able. It features execution of a set of cmd lines, and on-the-fly environment variables setting. Both are needed to start a virtualbox machine (plus is able to launch many apps in one go, and support internal variables).

You need to type a cmd line to register ServiceMan as a service (it will be executed at host start).
ServiceMan -i [ServiceName]

ServiceMan will get all information from a old-fashioned ServiceMan.ini located in the executable directory. Here is mine in this case:



[General]
;this shows in services control panel
Description=ServiceMan (a virtual machine using virtualbox)

;
;From now, each section is considered to be triggered sequentially
;at host startup and shutdown
;
;We're launching a virtual server in this one
[VirtualUbuntu]

;
;Aliases : allow to define internal variables you'll be reusing later in config file
;here i'm using a portable edition of virtualbox, and need to set some paths
;
Aliases=VboxUserHome=D:\Machines\Portable-VirtualBox\data\.VirtualBox;VboxDir=D:\Machines\Portable-VirtualBox\app64

;A more conventional virtual box installation should use this
;Aliases=VboxUserHome=C:\Documents and Settings\[User Name]\.VirtualBox;VboxDir=C:\Program Files\Oracle\VirtualBox

;
;Environment variables to add to your configuration when executing cmd lines
;
EnvVars=VBOX_USER_HOME=%VboxUserHome%;VBOX_INSTALL_PATH=%VBoxDir%

;
;Working directory
;
WorkingDir=%VboxDir%

;
;we can redefine envvars for that particular service event
;with StartupEnvVars=VBOX_USER_HOME=%VboxUserHome%;VBOX_INSTALL_PATH=%VBoxDir%
;following are the command executed sequentially. It waits 1000s by default
;
;doing this should force vboxsvc to read new env vars
StartupCmd1="%VboxDir%\vboxsvc.exe" /reregserver
;it's possible to define a pause between commands (default is 1000 ms)
;StartupPause1=1000
StartupCmd2="%VboxDir%\vboxmanage.exe" list systemproperties
StartupCmd3="%VboxDir%\vboxmanage.exe" list vms
StartupCmd4="%VboxDir%\vboxheadless.exe" -startvm VirtualLinux

;
;and shutdown event: again, if we need to redefine things...
;ShutdownEnvVars=VBOX_USER_HOME=%VboxUserHome%;VBOX_INSTALL_PATH=%VBoxDir%
;ShutdownWorkingDir=%VboxDir%
;
ShutdownCmd1="%VboxDir%\vboxmanage.exe" controlvm VirtualLinux savestate

;
;you may have more than one app to launch as a service: open another section
;[AnotherSetOfCommand]
;etc, etc
;



If you need to start and stop for testing, use the service control panel or the cmd line -s / -k option.

Note that it generates a session exec log in ServiceMan directory (which, therefore, should be writable).

Download ServiceMan

No comments: