My client needs to have a virtualized Linux running on his Windows Server 2008 (R1). I've managed to setup VirtualBox as a service with srvany
, but I see no way of creating a kind of 'stop script' so the virtual machine powers off correctly when I stop the registered service.
I know there may be other options of creating a headless VM, but that's not the point. How can I create a 'stop script' that is executed when I stop the registered service (or the system is rebooted)?
Or maybe, there are some other ways to launch 'system shutdown' scripts so VM is stopped properly on shutdown?
(for those that might be interested)
How I created a VirtualBox service
- Setup a virtual machine
- Install "Windows Server 2003 Resource Kit Tools.exe".
- Copy ".VirtualBox" folder to 'C:\Windows\System32\config\systemprofile\' so 'system' user (which runs services) has its VirtualBox machines there.
WARN: You'll need to correct VirtualBox.xml paths to snapshots, if your VM has any. - Create an empty service named "UbuntuD" with the help of
srvany
:
sc create UbuntuD binPath= "C:\Program Files (x86)\Windows Resource Kits\Tools\srvany.exe" start= auto DisplayName= "Ubuntu Daemon"
- Specify the service start command. Add these keys to registry
[HKEY\_LOCAL\_MACHINE\SYSTEM\CurrentControlSet\Services\UbuntuD\Parameters] Application="C:\Program Files\Sun\VirtualBox\VBoxHeadless.exe" AppParameters="--vrdp=off -s Ubuntu" AppDirectory="C:\Program Files\Sun\VirtualBox\"
- There's no 'stop script' for our hacky service, so we add a shutdown script in 'Local Policy' -> Computer configuration -> Windows Settings -> Scripts (Startup/shutdown):
C:\Program Files\Sun\VirtualBox\vboxmanage
, params:controlvm Ubuntu savestate
- Run the service