After days of googling and a lot of trial and errors, I finally succeed running virtualbox as service. I tried VBoxVmService by mattz but have no luck. Then I tried another method with srvstart and Windows Service Commander explained here. It works for one Virtual Machine, but not for multiple Virtual Machines. While I need to run three Virtual Machines simultaneously. All without any user logged on! OK, enough talking, let’s start the tutorial…
-
Preparation
- I tried this only on Windows XP SP3 and Windows 2003 Server SP2 (I think it may works on Windows 2000 Server as well)
update: this method works on Windows Server 2008 (check Brian’s comment below) Thanks Brian!
update Sept 29th 2009 : it works on Windows Seven 64 bit (check Mr Incredible’s comment). Thanks Mr Incredible!
update Sept 29th 2009: it works on Windows Vista (check Victor Pajor’s comment below). Thanks Victor Pajor!
- SUN VirtualBox for Windows version 2.2.2 or more (download here)
update Oct 13th 2009: it works on Windows XP SP3 with VirtualBox 3.0.8 (check Kikeze’s comment below). Thanks Kikeze!
- srvstart (download here)
- Windows Service Commander (download here)
-
Installation
-
- Install SUN VirtualBox, create and configure your guest OS’s
- Make sure you click the Remote Display setting, and check the "Enable VRDP server" option. Then assign a port number like 3001, 3002, 3003, etc.
- Setup all your guest OS’s as needed (install applications, setup preferences, tweaks, etc.)
- Shutdown all your guest OS’s
- Go to C:\Documents and Settings\YOURUSERNAME\.VirtualBox\ and open the file VirtualBox.xml with notepad.
- Press CTRL+F (find) and type "src" without quotes and press ENTER
-
Edit the words
src="Machines\YOURVMNAMEYOURVMNAME.xml" into src="C:Documents and Settings\YOURUSERNAME\.VirtualBox\Machines\YOURVMNAME\YOURVMNAME.xml. Close the file, and click Yes when asked
"Do you want to save the changes?". If you have more than one Virtual Machines, then you must repeat this step and replace the paths to your Virtual Machines’s .xml file accordingly.
original src
modified src
-
Copy the file named
VirtualBox.xml from
C:Documents and Settings\YOURUSERNAME\.VirtualBox\ to
C:Documents and Settings\LocalService\.VirtualBox\
- Install Windows Service Commander
- Create a new directory C:\vm
- Extract the srvstart_run.v110.zip file to this C:vm directory
- Open notepad, copy and paste the code below
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="C:\Documents and Settings\YOURUSERNAMEHERE\.VirtualBox\"
env=VBOX_MACHINES="C:\Documents and Settings\YOURUSERNAMEHERE\.VirtualBox\machines\"
env=VBOX_VDI="C:\Documents and Settings\YOURUSERNAMEHERE\.VirtualBox\HardDisks\"
env=USERPROFILE=%SystemDrive%\Documents and Settings\LocalService\
debug=1
debug_out=>C:\vm\vm.log
[YOURVMNAME]
startup=%VBOXHEADLESS% -startvm YOURVMNAME
shutdown_method=command
shutdown=%VBOXMANAGE% controlvm YOURVMNAME savestate
- Replace YOURUSERNAME above with your own windows user account, and YOURVMNAME with your own Virtual Machine’s name
- Close Notepad and save it as srvstart.ini in C:\VM
- Run Windows Service Commander
- Click Tools > Install new service
- The Service install wizard will show up, click Next
- Enter
c:\VM\srvstart.exe svc YOURVMNAME -c "c:\vm\srvstart.ini"
- Again, replace YOURVMNAME above with your own Virtual Machine’s name
- Press Next
- Enter any name you want in the Name and Display Name field (ie. My VM Service), then add some descriptions if needed (optional)
- Press Next
- Check the "Allow service to interact with desktop" option
- Press Next
- Leave the startup as Manual and error as Ignore for now
- Press Next
- Press Next again as there’s nothing to change here
- Click Finish
- Now you’ll see the new service name in the service list
- Click on it and press the triangle button at the toolbar to start the service
- If the red round icon turns into green, then your VM is running as service successfully!
- If everything’s work as expected, you can change the startup type of your service as automatic, so every time your host Windows OS runs, your VirtualBox service will also runs
- If you want to modify an existing Virtual Machine’s configuration (eg. add memory, etc), or add a new Virtual Machine, you must set the VirtualBox service’s startup type to manual first, shutdown all your guest OS’s, and restart the host OS. We do this so there is no process is locking up the VirtualBox.xml file. After restart, the .VirtualBox.xml file can be modified and recopied into C:\Documents and Settings\LocalService\.VirtualBox (see step no. 8 above)
- To control your Virtual Machines, you can use Remote Desktop Connection and put your host computer’s IP, followed by a colon and the port number u assigned in the Remote Display setting (see step no. 2 above) eg. 192.168.2.60:3001
- See the debug=1 line in the srvstart.ini file above? You can safely set it to debug=0 if no errors occurred when practicing this tutorial.
- This tutorial shows how to run a single Virtual Machine as service, next time I will write another tutorial to run multiple Virtual Machines like mine. Check out the tutorial to run multiple Virtual Machine!
- Source with some modifications from VirtualBox forum (rasker’s post)
- Snapshots will be added later.
Victor Pajor
Hello Ericzen
sorry…
have you tried to change the paths in srvstart.ini to “C:\Users” as mentioned by Brian? i don’t have any win2008 server machine, so i cannot test it myself.
as an aside, I should mention that the “LocalService” directory does not exist in Server 2008, so I created it. Also, Server 2008 will not let you create a new folder with a leading dot in the filename. After creating the LocalService folder, I attempted to start the VM, the service went green, and then red, and the “.virtualbox” folder was created in LocalService by Virtualbox.
I’m running Seven 64 bit as Host and Home Server as Guest and needed a way to run virtualbox as a service on startup.
I had to change the path in the “env=VBOX_VDI=” entry because the VDI file was on another drive and directory altogether.
Also it is important to note that any of the paths to the “env=VBOX_*****=” must end in a “\”.
Instead of the “LocalService” account which doesn’t exist under Windows 7, I copied the VirtualBox.xml file to the “C:\Users\Default” account.
Voila. Works a treat.
Interesting to note that although the VM is running OK, if one opens the VirtualBox application, it seems to think that the VM is still powered down.
Brilliant work!
LiveDeviL Reply:
September 30th, 2009 at 12:10 am
@Mr Incredible, Thank you Mr Incredible for sharing your experiences running this thing on Windows Seven 64 bit. Your tips will be very valuable addition to this tutorial.
Windows Server 2008 x64
LocalSystem or Default did not work for me. Always getting error -xxxxxxxx or 267.
Here is a workaround:
Copy .virtualbox folder with VirtualBox.xml to
C:\Windows\System32\config\systemprofile
Here is the original post for Windows Vista.
http://forums.virtualbox.org/viewtopic.php?f=6&t=1887&start=15
My VirtualBox.xml that works:
env=VBOXHEADLESS=”C:\Programs\Sun\VirtualBox\vboxheadless.exe”
env=VBOXMANAGE=”C:\Programs\Sun\VirtualBox\VBoxManage.exe”
debug=1
debug_out=>C:\vm\vm.log
[XE]
startup=%VBOXHEADLESS% -startvm XE -p 77777
shutdown_method=command
shutdown=%VBOXMANAGE% controlvm XE savestate
LiveDeviL Reply:
September 30th, 2009 at 12:11 am
@Victor Pajor, Thank you Victor Pajor for the workaround tips for Vista user. Very much appreciated.
Just another observation about the shutdown process. Shutting down or restarting Windows does not clean-stop any services which are running:- it just terminates them. So although the process described above will save the VM machine state if the service is stopped properly, it won’t do so if windows is shut down “normally”. As a result, and if you want the VM to remember its last state, you need to run a script at shutdown using the group policy editor. I created a simple bat file in which I had “net stop VBServer” where VBServer was the name of my service. That then got saved to c:\windows\system32\GroupPolicy\Machine\Scripts\Shutdown. Use the Group Policy Editor to specify the script to be run on shutdown.
Here’s my srvstart.ini
env=VBOXGUI=”P:\Program Files\Sun\virtualbox\virtualbox.exe”
env=VBOXHEADLESS=”P:\Program Files\Sun\VirtualBox\vboxheadless.exe”
env=VBOXWEBSRV=”P:\Program Files\Sun\VirtualBox\vboxwebsrv.exe”
env=VBOXMANAGE=”P:\Program Files\Sun\VirtualBox\VBoxManage.exe”
env=VBOX_PROG=”P:\Program Files\Sun\VirtualBox\”
env=VBOX_BASE=”F:\Users\Bob\.VirtualBox\”
env=VBOX_MACHINES=”F:\Users\Bob\.VirtualBox\machines\”
env=VBOX_VDI=”I:\VirtualBox\”
env=USERPROFILE=%SystemDrive%\Users\Default\
debug=0
debug_out=>C:\vm\vm.log
[VBSERVER]
startup=%VBOXHEADLESS% –startvm VBSERVER –vrdp=config
shutdown_method=command
shutdown=%VBOXMANAGE% controlvm VBSERVER savestate
Hello =)
I test this method with virtualbox 3.0.8 over Windows XP SP3 and works very well without problems
Thanks for this excellent tutorial
LiveDeviL Reply:
October 13th, 2009 at 9:25 am
@Kikeze, Hello Kikeze…
You’re welcome… Thank you for your information… I will update the post to include your info.
The srvstart command is throwing an error:
C:\vm>srvstart svc SquidVM -c “c:\vm\srvstart.ini”
ERROR: Exception 7 trapped in source file ‘D:\Home\Author\srvstart.v110\dll\CmdR
unner.cpp’ line 296
ERROR: Class ‘CmdRunner’ method ‘CmdRunner’
An internal error has occurred.
Any idea whats wrong?
I followed this tutorial and got also the
ERROR: Exception 7 trapped in source file ‘D:\Home\Author\srvstart.v110\dll\CmdR
unner.cpp’ line 296
I noticed that if I direct the srvstart to a non-existant config file, e.g. -c “c:\rm\doesnotexist” it gives the exact same error.
This is my .ini file:
env=VBOXGUI=”C:\Program Files\Sun\virtualbox\virtualbox.exe”
env=VBOXHEADLESS=”C:\Program Files\Sun\VirtualBox\vboxheadless.exe”
env=VBOXWEBSRV=”C:\Program Files\Sun\VirtualBox\vboxwebsrv.exe”
env=VBOXMANAGE=”C:\Program Files\Sun\VirtualBox\VBoxManage.exe”
env=VBOX_PROG=”C:\Program Files\Sun\VirtualBox\”
env=VBOX_BASE=”D:\Users\helum\.VirtualBox\”
env=VBOX_MACHINES=”D:\Users\helum\.VirtualBox\machines\”
env=VBOX_VDI=”D:\Users\helum\.VirtualBox\HardDisks\”
env=USERPROFILE=%SystemDrive%\Users\Default\
debug=0
debug_out=>C:\vm\vm.log
[Ubuntu804]
startup=%VBOXHEADLESS% –startvm Ubuntu804 –vrdp=config
shutdown_method=command
shutdown=%VBOXMANAGE% controlvm Ubuntu804 savestate
A question I have is: are the spaces before “env” important? I removed them
forgot to mention: I have Win7 64 bit