I want to write a code which restores a specific snapshot on a specific virtual machine using pyvbox.
I have this:
def readSnap(mach_name):
vbox = virtualbox.VirtualBox()
vm = vbox.find_machine(mach_name)
snap = vm.restore_snapshot()
But the last line throws:
virtualbox.library_base.VBoxError: 0x80020009
(Method Machine::restoreSnapshot is not implemented)
And I am quite confused about using this module. I know that restore_snapshot() takes an argument which must be an instance of ISnapshot but I don't know how to use it either.
Thank you for your help and time.
I found a solution. The problem was this line which I didn't include in the code:
snap = vm.find_snapshot(s_name)
And after restoring a snapshot you need to unlock your session with this:
session.unlock_machine()
And this is my final code:
def read_snapshot(m_name, s_name):
start = time.time()
name = "read_snapshot"
vb = virtualbox.VirtualBox()
session = virtualbox.Session()
try:
vm = vb.find_machine(m_name)
snap = vm.find_snapshot(s_name)
vm.create_session(session=session)
except virtualbox.library.VBoxError as e:
return Report(name, "failed", e.msg, True)
except Exception as e:
return Report(name, "failed", str(e), True)
restoring = session.machine.restore_snapshot(snap)
while restoring.operation_percent < 100:
time.sleep(0.5)
session.unlock_machine()
if restoring.completed == 1:
return Report(name, "success", "restoring completed in {:>.4} sec".format(str(time.time() - start)), False)
else:
return Report(name, "failed", "restoring not completed", True)
IMachine
documents a find_snapshot
function. If you know the name or UUID of the snapshot the pass that. Otherwise it says pass it a null (None
?) argument to to get the root snapshot. You can then use the children
attribute to iterate over the snapshots checking the attributes of the snapshots until you find the one you want.
One thing to note is that you said you want to "restore a specific snapshot". However, your function does not take an argument specifying which snapshot that is. So you're going to add parameters such that the snapshot can be found.
Your code might look like:
def readSnap(mach_name, snapshot_name):
vbox = virtualbox.VirtualBox()
vm = vbox.find_machine(mach_name)
snap = vm.find_snapshot(snapshot_name)
vm.restore_snapshot(snap)
User contributions licensed under CC BY-SA 3.0