To operate the VM inconspicuously, obviously you don’t run the full-fledged VirtualBox GUI. VirtualBox can also be fully managed from its command-line interface, called VBoxManage (you can read the User Manual if you want to learn more about VBoxManage).
In what follows, I will discuss how you can set VirtualBox to run in headless mode, and also how to set the virtual machine running pfSense to automatically enter the ‘saved state’ mode when you shut your Windows host down, and how to make it automatically start again when you boot your host.
Firstly, I need to say that I have spent quite a few hours trying to figure out on my own how to run VirtualBox as a service, so it can detect power events on the host, such as shutdown, logoff or startup. Especially the shutdown / logoff part. No, don’t even think about Group Policies, since in Windows 7, during shutdown, all programs are closed first and only afterwards it runs whatever scripts we set. Which means that we cannot tell VBoxManage or VBoxHeadless to nicely shut down our VM or to save its state.
The best bet is VBoxVmService, developed by an enthusiastic VirtualBox fan and available for free on Sourceforge. Download the latest build here and after unzipping it, read the Howto in the docs folder. It is very explanatory and simple.
By the way, don’t fret if the service does not start your VM right away so don’t try yet to debug it; just reboot after installing the service.
Still, the service is set for delayed start, so your VM will not start right away after your Windows boots. I didn’t quite like the idea, so in the VBoxVmService.ini file (you’ve read the Howto for VBoxVmService, right?) I have set my VM’s AutoStart option to no, and then created a small VBScript file to execute automatically at startup. It is a much faster option.
Note: C:\Program Files\Oracle\VirtualBox\ is the full path for a default VirtualBox installation. If you changed its location, you need to modify the path in the script accordingly.
- Type the following code in a text file and then save it as headless_start.vbs:
Set WshShell = WScript.CreateObject("WScript.Shell") cmd = chr(34) & "C:\Program Files\Oracle\VirtualBox\VBoxHeadless.exe" & chr(34) & " --startvm" & " pfSense" obj = WshShell.Run(cmd, 0) set WshShell = Nothing
Copy the file in C:\Windows\System32\GroupPolicy\Machine\Scripts\Startup
- If you have Windows 7 Professional and up, or Vista Business and up. Run the Local Group Policy Editor as administrator (the easy way: click the Start orb and type in the search box gpedit.msc).
- Go to Computer Configuration > Windows Settings and left click Scripts (Startup/shutdown).
- In the right side of the window, right-click Startup and select Properties. In the properties window click the Add button and then browse to the VBScript file which we have created earlier for startup and select it:
- Click OK, and then OK again.
- If you have a Home Edition of Windows 7 (or Vista). Copy the VBS file in C:\Users\[yourusernamehere]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup. This will work only for the specified user.
- Still having problems? Check on SevenForums.com for a wealth of advice on how to run programs at startup in Windows 7 / Vista.
Now, if you need to access your VM console, you now have to identify the IP address of your VirtualBox Host-Only Network (type in ipconfig in a Command window) and then connect to it using Remote Desktop Connection available in Windows 7 (we used the port set in Part 3). See the next two screenshots:
Just in case: if you need some more quick VBScript files to control your VM when in headless mode, here they are:
- To save state: headless_savestate.vbs:
Set WshShell = WScript.CreateObject("WScript.Shell") cmd = chr(34) & "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" & chr(34) & " controlvm" & " pfSense" & " savestate" obj = WshShell.Run(cmd, 0) set WshShell = Nothing
- To force reset: headless_reset.vbs:
Set WshShell = WScript.CreateObject("WScript.Shell") cmd = chr(34) & "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" & chr(34) & " controlvm" & " pfSense" & " reset" obj = WshShell.Run(cmd, 0) set WshShell = Nothing
- To shut it down (ACPI power button): headless_acpiturnoff.vbs:
Set WshShell = WScript.CreateObject("WScript.Shell") cmd = chr(34) & "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" & chr(34) & " controlvm" & " pfSense" & " acpipowerbutton" obj = WshShell.Run(cmd, 0) set WshShell = Nothing
Congrats! You are done.