How to wait for a guest shutdown from python script

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
DmitryGuryanov
Posts: 2
Joined: 12. Nov 2013, 02:14

How to wait for a guest shutdown from python script

Post by DmitryGuryanov »

Hello,

I'm writing a script, which can be used to run a VM in fullscreen mode as an Xsession (so that you can select "Windows" session in GDM, type you login and password and get running VM in fullscreen mode).

I need to find a way to wait for guest shutdown to determine the time you need to return to GDM. I tried following script:

Code: Select all

import vboxapi
import time

from vboxapi import VirtualBoxManager
vboxmgr = VirtualBoxManager(None, None)
vbox = vboxmgr.vbox
mgr = vboxmgr.mgr

session = mgr.getSessionObject(vbox)
m = vbox.findMachine("win")

progress = m.launchVMProcess(session,"gui", "")
progress.waitForCompletion(-1)

console = session.console

r = vboxapi.VirtualBox_constants.VirtualBoxReflectionInfo(0)
while console.state != r.all_values("MachineState")["PoweredOff"]:
        print console.state
        time.sleep(1)


print "Powered Down !", console.state
mgr.closeMachineSession(session)
But when VM process exits console.state attribute becomes inaccessible:

Code: Select all

Traceback (most recent call last):
  File "run-vm.py", line 20, in <module>
    while console.state != r.all_values("MachineState")["PoweredOff"]:
  File "/usr/lib64/virtualbox/sdk/bindings/xpcom/python/xpcom/client/__init__.py", line 374, in __getattr__
    return getattr(interface, attr)
  File "/usr/lib64/virtualbox/sdk/bindings/xpcom/python/xpcom/client/__init__.py", line 460, in __getattr__
    return XPTC_InvokeByIndex(self._comobj_, method_index, args)
xpcom.Exception: 0x80070005 (The object is not ready)
Also I tried to use events:

Code: Select all

>>> p = m.launchVMProcess(session,"gui", "")
>>> p.waitForCompletion(-1)
>>> console = session.console
>>> es = console.eventSource
>>> l = es.createListener()
>>> es.registerListener(l, (r.all_values("VBoxEventType")["OnStateChanged"],), False)
>>> ev = es.getEvent(l, 30000)
>>> ev
<XPCOM component '<unknown>' (implementing IEvent)>
>>> ev = es.getEvent(l, 30000)
>>> ev
<XPCOM component '<unknown>' (implementing IEvent)>
>>> ev.type
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/virtualbox/sdk/bindings/xpcom/python/xpcom/client/__init__.py", line 374, in __getattr__
    return getattr(interface, attr)
  File "/usr/lib64/virtualbox/sdk/bindings/xpcom/python/xpcom/client/__init__.py", line 460, in __getattr__
    return XPTC_InvokeByIndex(self._comobj_, method_index, args)
xpcom.Exception: 0x800706be (Call to remote object failed (NS_ERROR_CALL_FAILED))
So it seems IConsole object doesn't work properly after VM shutdown :(

Does anyone know, how to solve such problem?
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: How to wait for a guest shutdown from python script

Post by noteirak »

Moved to "Virtualbox API" forum.

The console object doesn't exist anyomore because, when a VM is powered off, all the associate objects are destroyed aswell. This is true for the session linked to it, and also the console (since it is linked to the session).
Since the console object gets destroyed, it cannot be an event producer anymore, so you won't get any event from there.

On the other hand, you can also get events from the Machine object, or from the Virtualbox object. You would definitly get the events from the Virtualbox object, maybe from the Machine one.
If you want to manually check the VM status instead of waiting on events, you should use the state of the machine object, and not the related session or console.
The read-only machine object (the one you get from IVirtualbox::findMachine()) will always exist while the one you got from ISession::getMachine() will only exist while a session exist.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
DmitryGuryanov
Posts: 2
Joined: 12. Nov 2013, 02:14

Re: How to wait for a guest shutdown from python script

Post by DmitryGuryanov »

Thank you! Both methods works fine!
Post Reply