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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With