VirtualBox Python API 恢复快照

发布于 2024-10-21 01:05:40 字数 2051 浏览 2 评论 0原文

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

    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

我想我可能错过了一些东西,有什么线索吗? 谢谢, C.

I'm trying to manage some Virtual Machines through the vboxapi provided with the SDK.
So far I managed to launch the VM and poweroff it, but I'm not able to restore the snapshot.
Looks like the power off procedure locks the virtual machine until the scripts terminates, as a matter of fact this is the error that I get:

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

And following is the specific functions I sequentially call to stop vm and restore snapshot.

    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

I think I'm probably missing something, any clue of what that is?
Thanks,
C.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

維他命╮ 2024-10-28 01:05:41

如果关闭计算机,则必须创建一个新的 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()。

推特@dsanchezlavado

If you powerDown the machine you have to create a new IConsole object to restore the snapshot. In your code you can add this lines before restore the snapshot.

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

In the SDK:
A console object gets created when a machine has been locked for a particular session (client
process) using IMachine::lockMachine() or IMachine::launchVMProcess().

twitter @dsanchezlavado

看春风乍起 2024-10-28 01:05:41

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

You need to lock the machine first so that the console object get created for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文