2011-03-09 105 views
3

我試圖通過隨SDK提供的vboxapi來管理一些虛擬機。 到目前爲止,我設法啓動虛擬機並關閉它,但我無法恢復快照。 貌似關機程序鎖定的虛擬機,直到腳本結束,如事實上這是我的錯誤:VirtualBox Python API恢復快照

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

而下面是具體功能我依次調用停止VM和恢復快照。

 
    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.

回答

2

如果您powerDown機器,您必須創建一個新的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

0

您需要先鎖定設備,這樣控制檯對象獲取爲您創建。