Running a headless VirtualBox in Windows

In a previous post I wrote about using VirtualBox as a means to run your own development and test servers within the confinement of your own system. Using the default UI you’ll have several open windows at a time that you don’t need though, and they can get in your way. Tom de Grunt pointed me to the option to run VirtualBox in headless mode using the command line (he has a short blog post about it).

There’s a separate command to run a VM in headless mode as described in the manual here. When you run Windows and use VBoxHeadless you’ll notice that it hogs your command prompt (and that the only way to get it back is to kill the VBoxHeadless session with CTRL+C) which gives you the same effect as before: useless open Windows. There is a ticket open on the Virtual Box bug tracker here, and the ticket contains a couple of interesting workarounds. In this article I’ll describe a number of ways that I found to solve this.

Using the start command to run VBoxHeadless

Instead of running VBoxHeadless directly you can run it like so: start /b VBoxHeadless -s nameofvm. The start command starts a new process with the given parameters, the /b option stars the process without opening a new window.

Note that running your VM in headless mode will enable VDRE (remote desktop) for your VM. If you don’t want this you can add -v off to the list of options to the VBoxHeadless command.

While this is a really easy way to run your VirtualBox VMs in headless mode (especially if you usually have a command prompt open anyway) it has a downside: if you close your command prompt the processes you started with start /b will get killed with it. This makes this solution kind of brittle (imagine closing your command prompt by accident and then losing your SSH connections to all the VMs you started and losing the work therein… that’s no fun).

I found a possible solution to that downside here, where the suggestion was given to write a vbs script that starts a shell in which the VBoxHeadless command is started and then severs the connection between that shell and the place it’s been started from. With a bit of effort you could write a script that accepts the VM name as a parameter so you don’t need a script per VM.

Running your VirtualBox VM as Windows services

VBoxVmService is an open source tool that allows you to add VirtualBox VMs as services in Windows. To use it you need to install the extension pack though, which binds you to the PUEL license (which has restrictions you may not like). Assuming that you don’t have a problem with these limitations this is a really easy solution, you just put it in a folder of your choice and configure it through an INI file. It has a command line tool that you can use to install and uninstall the Windows service. Another great feature is that it will cleanly shut down your VMs if you shut down Windows.

The downsides for me are that you have to configure your VMs in an INI file, and that you can’t track state of your VMs in the VirtualBox GUI (which you can with the first solution).

If you want to start one or more VMs at runtime, and do not want to worry about them being shut down cleanly when you shut down Windows then this solution is the way to go.

Running VirtualBox with phpVirtualBox

phpVirtualBox is an open source web GUI that runs your VMs in headless mode. While it doesn’t meet my needs (you have to install a webserver and PHP to get it running, and that’s stuff I want to run in my VMs and not on my Windows installation) it is a really cool project and it’s ideal for headless machines that run VirtualBox to offer VMs.

I haven’t tried this myself, if you have I would love to see some comments on this article about your experiences.

What I’d like to have ideally…

Ideally I’d like to see an option in the VirtualBox GUI itself to start a machine in headless mode (there is a ticket open for it with low priority, it has been open for quite some time so I don’t expect that feature to be there soon).

In conclusion

While not ideal the first option is the best option for me right now.

I’m hoping to find some time to take a look and see if I can modify the VirtualBox UI to allow me to start VMs in headless mode from there, or create a separate tool to do so conveniently using one of the VirtualBox APIs.

%d bloggers like this: