VirtualBox Python API memulihkan snapshot

Saya mencoba mengelola beberapa Mesin Virtual melalui vboxapi yang disediakan dengan SDK. Sejauh ini saya berhasil meluncurkan VM dan mematikannya, tetapi saya tidak dapat memulihkan snapshotnya. Sepertinya prosedur mematikan mengunci mesin virtual hingga skrip berhenti, sebenarnya ini adalah kesalahan yang saya dapatkan:

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

Dan berikut ini adalah fungsi spesifik yang saya panggil secara berurutan untuk menghentikan vm dan memulihkan 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

Saya pikir saya mungkin melewatkan sesuatu, ada petunjuk tentang apa itu? Terima kasih, C.


person Claudio    schedule 09.03.2011    source sumber


Jawaban (2)


Jika Anda mematikan mesin, Anda harus membuat objek IConsole baru untuk memulihkan snapshot. Dalam kode Anda, Anda dapat menambahkan baris ini sebelum memulihkan snapshot.

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

Di SDK: Objek konsol dibuat ketika mesin telah dikunci untuk sesi tertentu (proses klien) menggunakan IMachine::lockMachine() atau IMachine::launchVMProcess().

twitter @dsanchezlavado

person DSL    schedule 19.04.2011

Anda perlu mengunci mesin terlebih dahulu agar objek konsol dapat dibuat untuk Anda.

person AhlyM    schedule 28.01.2015