python - 无法在 Python Virtualbox API 中恢复 VM

标签 python virtualbox pywin32

我想在 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 是未实现

最佳答案

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

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

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

关于python - 无法在 Python Virtualbox API 中恢复 VM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33754549/

相关文章:

Python/Windows : List only USB removable drives

python - 难以理解 wxPython 中的 wx.Sizer

Python pywin32 - VK_SLEEP 按下不工作

python - 如何从缓存中获取过期时间

python - 使用具有 "blackbox"函数的多核

python - 如何在日期python中转换YYMMDDHH时间戳

vue.js - Vagrant VirtualBox Local Dev Env、Ubuntu、Yarn、Vue CLI 3 - 问题

python - 遍历作为列表的字典值以创建新的字典列表

linux - 我无法执行命令 modprobe vboxdrv

ubuntu - 将 FreeSWITCH 注册到 X-Lite 时不断收到错误 408