Reputation: 15
I'm trying to manage some Virtual Machines through the vboxapi provided with the SDK. So far I managed to launch the VM, restore a Snapshot, but I'm not able to restore the snapshot and see the result ...
def launch_vm(target):
manager = VirtualBoxManager(None, None)
vbox = manager.vbox
session = manager.mgr.getSessionObject(vbox)
pinfo("Reverting to snapshot '%s'" % target['vm_snapshot_name'])
try:
vm = vbox.findMachine(target['vm_name'])
snap = vm.findSnapshot(target['vm_snapshot_name']);
progStart = vm.launchVMProcess(session, "gui", "");
progStart.waitForCompletion(5000);
vm.lockMachine(session,1)
progress = session.console.restoreSnapshot(snap)
progress.waitForCompletion(5000);
session.unlockMachine()
except Exception, e:
perror(e)
I get this error message :
[ERROR] 0x80bb0007 (The given session is busy)
I probably don't call the good functions ... Thanks !
Upvotes: 1
Views: 2202
Reputation: 520
First of all launchVMProcess will lock the machine, so you don't need to lock it again.
Second, was your online snapshot, live or offline ?
Another thing you must take care about is that waitForCompletion() function doesn't wait for the OS to boot, it just start the OS and this is considered completed, so you might need to make a busy loop by yourself to wait for OS loading.
Upvotes: 0
Reputation: 188
I have restored a snapshot using the virtualbox api in c#.
Looking at your code you are trying to restore the snap shot after launching the VMProcess. The documentation says "The machine must not be running, otherwise the operation will fail". https://www.virtualbox.org/sdkref/interface_i_console.html
Also the launchVMProcess will automaitcally lock a session that you pass in so it might be the reason you are getting the session is busy. The code below is how I got my section running, I hope it helps.
VirtualBox.VirtualBox virtualBox = new VirtualBox.VirtualBox();
IMachine vmMachine = virtualBox.FindMachine("Windows 8");
Session session = new Session();
vmMachine.LockMachine(session, LockType.LockType_Shared);
IConsole console = session.Console;
// Restore snapshot
ISnapshot snapShot = vmMachine.FindSnapshot("Snapshot 1");
IProgress snapShotProgress = console.RestoreSnapshot(snapShot);
snapShotProgress.WaitForCompletion(300000);
// unlock before launch VMProcess
session.UnlockMachine();
IProgress launchVmProgess = vmMachine.LaunchVMProcess(session, "gui", "None");
launchVmProgess.WaitForCompletion(300000);
Upvotes: 2