Page 1 of 1

Listing / Manipulating VMs for Another User

Posted: 17. Nov 2014, 09:08
by Huckle
I'd like to have my VMs start and stop with the host operating system (which is Windows 7). I've used Group Policy to set scripts that run when the machine starts up and when the machine shuts down. The Start up script works like a champ, starting the named VM in headless mode so that by the time user log-in happens it's already resumed and accepting network connections. The shutdown script, on the other hand, is not working during shutdown.

Start up Script:

Code: Select all

"C:\Program Files\Oracle\VirtualBox\VBoxHeadless.exe" -s "Ubuntu14.04"
Shutdown Script:

Code: Select all

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm 09db26ba-2d24-42aa-a71d-668a220cfe18 savestate
When I double click the shutdown script batch file it successfully suspends the VM and saves it's state. However, if I start the VM and then shut down the host, the script throws an error stating that it cannot find a VM with that name (later UUID as I thought that might help). The error message it presents is below.

Code: Select all

VBoxManage.exe: error: Could not find a registered machine named '09db26ba-2d24-
42aa-a71d-668a220cfe18'
VBoxManage.exe: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), compo
nent VirtualBox, interface IVirtualBox, callee IUnknown
VBoxManage.exe: error: Context: "FindMachine(Bstr(a->argv[0]).raw(), machine.asO
utParam())" at line 95 of file VBoxManageControlVM.cpp
I suspect this has something to do with VBoxManage not listing VMs from other Windows users. The shutdown script is likely running in the context of the System account, rather than my user. My question is, why can VBoxHeadless seem to be able to find the VM, despite also running under the System account? And how can I get VBoxManage to do the same? Utilizing the `runas` command in Windows is not an option because it requires entering the user's password (even if run from a privileged context such as System).

Re: Listing / Manipulating VMs for Another User

Posted: 17. Nov 2014, 14:55
by scottgus1
If I recall correctly, there are two problems with using shutdown scripts to shut down guests:
1. the account the shutdown script runs under
2. the point in the host shutdown at which the shutdown script runs

#1: You need to make sure that the account being used by any script calling Vboxmanage is the same account that you logged on the host with, and which is running the guests. This will help you run backup scripts and such, but will not get around #2

#2: The Group Policy shutdown script runs after the part of the OS that shuts down desktop applications, which includes your guests. Windows asks to close / kills your desktop apps, then later runs the GP script. So even if your script is running in the right account, all of your guests have been killed by the OS before the script starts running, and so there's no guests for Vboxmanage to find.

A solution to get your guests to run with the host start-up and shutdown without these issues is to run your guests as a service (see viewtopic.php?f=6&t=4762) It supports both full shutdown or save-state of the guests on host shutdown. I've used it before, it works well. One drawback, you can't use Vboxmanage on these guests while the service is running them - different accounts. So no backup scripts, no heartbeat/restart scripts, etc. Just start & save-state/shutdown. You can stop the guests while the host is still running, then restart them with the Virtualbox GUI, do something with them, then shut them down and restart the service, but no Vboxmanage while running as a service.

The solution I use: I run the host with automatic log-on of the account (google Sysinternals Auto Log On) then launch my guests with a script or shortcuts in the Startup menu. Vboxmanage has full access. I don't use the OS's Start Menu button to shut down the host - I used the hosts' Group Policy to disable Shutdown & Restart & Log Off ; "Lock" is my default. I can lock the host to prevent unauthorized access while the guests are fully operational. To shut down the host,I use a script to call Vboxmanage and shut down the guests first, then call Shutdown.exe to shut down or reboot the host. I set Windows Updates on the host to Download and Manual Install so the host won't reboot unexpectedly. I use a VBS script from Microsoft's TechNet to install Windows Updates from a script which makes sure the guests are shutdown with Vboxmanage before the Updates are run and the host reboots. So far everything works well.

Re: Listing / Manipulating VMs for Another User

Posted: 19. Nov 2014, 07:14
by Huckle
scottgus1 wrote:If I recall correctly, there are two problems with using shutdown scripts to shut down guests:
1. the account the shutdown script runs under
2. the point in the host shutdown at which the shutdown script runs
I'm not so certain #2 will be a problem since the process doesn't have a window and is started under the local system account during boot. I won't know for certain until we resolve #1 though.
... run your guests as a service
This would be ideal. I was hoping that virtualbox had some under utilized service component I was unaware of, but sadly the project looks to be entirely unofficial. From the limited documentation it looks like a simple service that switches to the user context specified in the config file and then runs vboxheadless. That's basically the same thing that GPO scripts will get me. Worse yet, it sleeps to keep windows from shutting down before the VMs. I like my solution better in this regard, as Windows will wait for GPO scripts to finish before shutting down. The documentation did mention the environment variable VBOX_USER_HOME however, which lead me to http://lifeofageekadmin.com/allow-multiple-users/.

Hopefully, by setting this environment variable I can get the local system account to see my VM during the shutdown GPO script. I will test and report back.

Re: Listing / Manipulating VMs for Another User

Posted: 21. Nov 2014, 06:34
by Huckle
Reporting back. Still not working, but there has been progress.

The scripts have been updated to report
  • Who the current user context is
  • What VBOX_USER_HOME is
  • Which VMs VBoxManage believes exist
  • The results of the start/stop action
Statup Script:

Code: Select all

2>&1 (
  echo ==================================== 
  whoami 
  echo %VBOX_USER_HOME% 
  "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" list vms
  start /b "" "C:\Program Files\Oracle\VirtualBox\VBoxHeadless.exe" -s 2d8f5355-293f-4220-9976-eec1d12b0cee
) | wtee -a "C:\Users\Public\Virtual Machines\start.log" 
Shutdown Script:

Code: Select all

2>&1 (
  echo ==================================== 
  whoami 
  echo %VBOX_USER_HOME% 
  "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" list vms
  "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm 2d8f5355-293f-4220-9976-eec1d12b0cee savestate 
) | wtee -a "C:\Users\Public\Virtual Machines\stop.log"
Output of the Startup script:

Code: Select all

nt authority\system
C:\Users\Public\Virtual Machines 
"Ubuntu14.04" {09db26ba-2d24-42aa-a71d-668a220cfe18}
"Webserver" {2d8f5355-293f-4220-9976-eec1d12b0cee}
"foo" {eddff518-31ac-44e8-bfac-58c043d0821b}
Oracle VM VirtualBox Headless Interface 4.3.10
(C) 2008-2014 Oracle Corporation
All rights reserved.

VBoxHeadless.exe: error: Failed to assign the machine to the session (E_FAIL)
VBoxHeadless.exe: error: Details: code VBOX_E_VM_ERROR (0x80bb0003), component Machine, interface IMachine, callee IUnknown
VBoxHeadless.exe: error: Context: "LockMachine(session, LockType_VM)" at line 960 of file VBoxHeadless.cpp
Shutdown Script output:

Code: Select all

==================================== 
nt authority\system
C:\Users\Public\Virtual Machines 
"Ubuntu14.04" {09db26ba-2d24-42aa-a71d-668a220cfe18}
"Webserver" {2d8f5355-293f-4220-9976-eec1d12b0cee}
"foo" {eddff518-31ac-44e8-bfac-58c043d0821b}
VBoxManage.exe: error: Failed to assign the machine to the session (E_FAIL)
VBoxManage.exe: error: Details: code VBOX_E_VM_ERROR (0x80bb0003), component Machine, interface IMachine, callee IUnknown
VBoxManage.exe: error: Context: "LockMachine(a->session, LockType_Shared)" at line 100 of file VBoxManageControlVM.cpp

Re: Listing / Manipulating VMs for Another User

Posted: 4. Mar 2015, 19:04
by ITEM93
I'm trying a similar setup for a deprecated server that needs to run in the background without a user being logged in. I have a startup and shutdown .bat script that both run successfully, am just having issues getting them to run successfully through the group policy.

Startup Script:

Code: Select all

@ECHO OFF
SET MAN="c:\Program Files\Oracle\VirtualBox\VboxManage.exe"
SET VM="VMName"
%MAN% startvm %VM% --type headless
echo VM should have started from GP %Date% >> C:\Data\Start.txt
On the guest I configured HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System\shutdownwithoutlogon to 1
And the power options for the ACPI to shutdown

Shutdown Script:

Code: Select all

@ECHO OFF
shutdown /a
SET MAN="c:\Program Files\Oracle\VirtualBox\VboxManage.exe"
SET VM="VMName"
%MAN% controlvm %VM% acpipowerbutton
:loop
	SET COUNTER=0
	SET RUNNING=
	FOR /f "delims=" %%A in ('%MAN% list runningvms') do (
		SET RUNNING=%RUNNING% %%A
		SET /A COUNTER=%COUNTER%+1
	)
	ECHO "Waiting for %RUNNING%"
	timeout 10 > NUL
if %COUNTER% GTR 0 GOTO :loop
shutdown /s /t 5
The script is being run as I am having output in the .txt logs, however the VM is not starting up.
Scripts are place in "C:\Windows\System32\GroupPolicy\Machine\Scripts" and group policy configured in "Local Computer Policy\Computer Configuration\Windows Settings\Scripts (Startup/Shutdown)"
Host OS- Windows7x64
Guest OS - Server 2003
VirtualBox version- 4.3.22r98236
I've used Group Policy to set scripts that run when the machine starts up and when the machine shuts down. The Start up script works like a champ, starting the named VM in headless mode so that by the time user log-in happens it's already resumed and accepting network connections.
Where did you configure your Group Policy?