无法在 Python Virtualbox API 中恢复 VM

Can't restore VM in Python Virtualbox API

我想在 Virtualbox API 中恢复虚拟机,但我遇到了这个异常:

AttributeError: '<win32com.gen_py.VirtualBox Type Library.IConsole instance at 0x41746480>' object has no attribute 'restoreSnapshot'

我该如何解决?

这是我的代码:

import vboxapi

def wait(sth):
    sth.waitForCompletion(-1)

class Foo:
    mgr=vboxapi.VirtualBoxManager()
    vbox=mgr.vbox
    vm=vbox.findMachine(const.VM_NAME)

    def __init__(self):
        self.session=self.mgr.getSessionObject(self.vbox)
        wait(self.vm.launchVMProcess(self.session, 'gui', ''))

    def restore(self):
        console=self.session.console
        wait(console.powerDown())
        wait(console.restoreSnapshot(self.vm.findSnapshot('test')))
        wait(console.powerUp())

foo=Foo()
foo.restore()

我在 Python 3.4.

下使用 vboxapi 5.0.10

此外,当我根据 VirtualBox SDK Ref 将 console.restoreSnapshot 更改为 self.vm.restoreSnapshot 时,它说 Method Machine::restoreSnapshot is not implemented.

这是我经过几天尝试后的解决方案。

def restore(self):
    self.log('=== powering down')
    wait(self.session.console.powerDown())
    self.session.unlockMachine()

    self.log('=== restoring')
    self.session=self.mgr.openMachineSession(self.vm) # WHY?
    self.vm=self.session.machine
    wait(self.vm.restoreSnapshot(self.vm.findSnapshot(const.VM_BASE_SNAPSHOT)))
    self.session.unlockMachine()

    self.log('=== powering up')
    self.vm=self.vbox.findMachine(const.VM_NAME)
    self.session=self.mgr.getSessionObject(self.vbox)
    while True:
        try:
            wait(self.vm.launchVMProcess(self.session,'gui' if const.DEBUG else 'headless',''))
        except pywintypes.com_error: #unlocking machine
            time.sleep(.1)
        else:
            break

虽然不知道是什么原因,但总之是可以的