VirtualBox Python API恢复快照

时间:2011-03-09 23:53:49

标签: python api virtualbox

我正在尝试通过SDK提供的vboxapi来管理一些虚拟机。 到目前为止,我设法启动VM并关闭它,但我无法恢复快照。 看起来断电过程会锁定虚拟机,直到脚本终止,事实上这是我得到的错误:

    progress = self.session.console.restoreSnapshot(self.mach.currentSnapshot)
  File "", line 3, in restoreSnapshot
xpcom.Exception: 0x80070005 (The object is not ready)

以下是我依次调用停止虚拟机和恢复快照的特定功能。

    def stop(self):
        if self.mach:
            # Poweroff the virtual machine.
            progress = self.session.console.powerDown()
            # Wait for task to complete with a 60 seconds timeout.
            progress.waitForCompletion(VIRTUALBOX_TIMEOUT)
            # Check if poweroff was successful.
            if progress.resultCode != 0:
                log("[Virtual Machine] [PowerOff] [ERROR] Unable to poweroff virtual machine \"%s\"." % self.mach.name)
                return False
            else:
                log("[Virtual Machine] [PowerOff] Virtual machine \"%s\" powered off successfully." % self.mach.name)
        else:
            log("[Virtual Machine] [PowerOff] [ERROR] No virtual machine handle.")
            return False

        return True

    def restore_snapshot(self):
        if self.mach:
            # Restore virtual machine snapshot.
            progress = self.session.console.restoreSnapshot(self.mach.currentSnapshot)
            # Wait for task to complete with a 60 seconds timeout.
            progress.waitForCompletion(VIRTUALBOX_TIMEOUT)
            # Check if snapshot restoring was successful.
            if progress.resultCode != 0:
                log("[Virtual Machine] [Restore Snapshot] [ERROR] Unable to restore virtual machine \"%s\" snapshot." % self.mach.name)
                return False
            else:
                log("[Virtual Machine] [Restore Snapshot] Virtual machine \"%s\" successfully restored to current snashot." % self.mach.name)
        else:
            log("[Virtual Machine] [Restore Snapshot] [ERROR] No virtual machine handle.")
            return False

        return True

我想我可能会遗漏一些东西,这是什么的线索? 谢谢, 下进行。

2 个答案:

答案 0 :(得分:2)

如果powerDown机器,则必须创建新的IConsole对象以恢复快照。在您的代码中,您可以在恢复快照之前添加此行。

def restore_snapshot(self):
    if self.mach:
         self.mach.lockMachine(self.session,1)
         console = self.session.console
         progress = console.restoreSnapshot(self.mach.currentSnapshot)

在SDK中: 当机器已针对特定会话(客户端)锁定时,将创建控制台对象 使用IMachine :: lockMachine()或IMachine :: launchVMProcess()。

twitter @dsanchezlavado

答案 1 :(得分:0)

您需要先锁定机器,以便为您创建控制台对象。