Setup
- Windows 7 host
- Gentoo Linux guest (but this method should work for almost any other guest)
- Raw disk writes by guest
Objective
- Start the guest on startup or logon
- Stop the guest on shutdown or logoff
Prerequesites
- PowerShell (not sure if the initial version from around Vista's launch works)
- Some sort of Visual Basic script parser (usually built into Windows)
- Admin access if you use raw disk writes and to hook the scripts to startup/shutdown events
Overview
You'll be making a chain of scripts, 4 for startup and 2 for shutdown, and a shortcut for admin privilege escalation. To make things easier, keep the scripts in the same folder as the guest OS vbox, vdi, and vmdk files; these instructions will assume this directory structure as well. Starting your virtual machine this way will completely hide any VBox GUI and you will only be able to access your virtual machine through remote access methods, such as SSH. You will still be able to see the virtual machine as a process running under VBoxHeadless and to monitor its resource usage under Windows as with any other program.
Startup scripts
1: startup.ps1
This is the launch point. Open notepad.exe and enter the following lines.
Code: Select all
cd "<location_of_scripts_and_guest_OS>"
c:\windows\system32\wscript.exe ./launch_guest.vbs
2: launch_guest.vbs
This script elevates the privileges to get admin access so raw disks are accessible. Open notepad.exe and enter the following lines.
Code: Select all
Set objShell = CreateObject("Shell.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")
strPath = FSO.GetParentFolderName (WScript.ScriptFullName)
If FSO.FileExists(strPath & "\guest_startup.vbs") Then
objShell.ShellExecute "wscript.exe", _
Chr(34) & strPath & "\guest_startup.vbs" & Chr(34), "", "runas", 1
Else
MsgBox "Script file guest_startup.vbs not found"
End If
3: guest_startup.vbs
This file runs a batch script that starts VBox and suppresses the console window so that it runs silently in the background. Open notepad.exe and enter the following lines.
Code: Select all
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "<location_of_scripts_and_guest_os>\guest_startup.bat" & Chr(34), 0
Set WshShell = Nothing
4: guest_startup.bat
This file actually kicks off VBoxHeadless with the virtual machine. Open notepad.exe and enter the following lines.
Code: Select all
start "" /B "<virtualbox_install_location>\VBoxHeadless.exe" -s "<virtual_machine_name_in_VBox>" -v off
5: Test and launch
Now you can try out the script by running startup.ps1 to see if it kicks everything off. You'll get an admin prompt when running it after you've logged in, but if you add it as a hook to the Group Policy Editor startup/logon events you won't be prompted each time it's run and the virtual machine is launched.
Shutdown scripts
1: shutdown.ps1
This is the launch point for shutdown. Open notepad.exe and enter the following lines.
Code: Select all
cd "<location_of_scripts_and_guest_OS>"
./guest_shutdown.lnk
exit
2: guest_shutdown.bat
This script 'pushes' the power button on the virtual machine to get it to shut down through ACPI. Therefore ACPI must be set up in the virtual machine for this to work. Open notepad.exe and enter the following lines.
Code: Select all
"<virtualbox_install_location>\VBoxManage.exe" controlvm <virtual_machine_name_in_VBox> acpipowerbutton
3: guest_shutdown.lnk
This is just a shortcut to guest_shutdown.bat with 'Always Run as Administrator' checked in the Compatibility tab. However I don't think you actually need this even if VBoxHeadless is running with admin privileges, so this step might be optional. Be sure to update shutdown.ps1 if you're skipping this step.
4: Test and launch
Now you can try out the shutdown script chain by running shutdown.ps1 and watching VBoxHeadless disappear from Task Manager's process list. If it doesn't shut down on its own, make sure the guest OS is set up to handle ACPI events. Then you can attach the script as a hook to Group Policy Editor's shutdown/logoff events.
That's all, good luck!