繁体   English   中英

无法在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

尽管我不知道原因,但是仍然可以。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM