2015-11-17 57 views
2

恢復VM我想在VirtualBox的API以恢復虛擬機,但我得到這個異常:無法在Python API的virtualbox

AttributeError: '<win32com.gen_py.VirtualBox Type Library.IConsole instance at 0x41746480>' object has no attribute 'restoreSnapshot' 

我怎樣才能解決呢?

這裏是我的代碼:

import vboxapi 

def wait(sth): 
    sth.waitForCompletion(-1) 

class Foo: 
    mgr=vboxapi.VirtualBoxManager() 
    vbox=mgr.vbox 
    vm=vbox.findMachine(const.VM_NAME) 

    def __init__(self): 
     self.session=self.mgr.getSessionObject(self.vbox) 
     wait(self.vm.launchVMProcess(self.session, 'gui', '')) 

    def restore(self): 
     console=self.session.console 
     wait(console.powerDown()) 
     wait(console.restoreSnapshot(self.vm.findSnapshot('test'))) 
     wait(console.powerUp()) 

foo=Foo() 
foo.restore() 

我使用Python 3.4下vboxapi 5.0.10。

另外,當我根據VirtualBox SDK Ref將console.restoreSnapshot更改爲self.vm.restoreSnapshot時,它說Method Machine::restoreSnapshot is not implemented

回答

4

這是我經過幾天的嘗試後的解決方案。

def restore(self): 
    self.log('=== powering down') 
    wait(self.session.console.powerDown()) 
    self.session.unlockMachine() 

    self.log('=== restoring') 
    self.session=self.mgr.openMachineSession(self.vm) # WHY? 
    self.vm=self.session.machine 
    wait(self.vm.restoreSnapshot(self.vm.findSnapshot(const.VM_BASE_SNAPSHOT))) 
    self.session.unlockMachine() 

    self.log('=== powering up') 
    self.vm=self.vbox.findMachine(const.VM_NAME) 
    self.session=self.mgr.getSessionObject(self.vbox) 
    while True: 
     try: 
      wait(self.vm.launchVMProcess(self.session,'gui' if const.DEBUG else 'headless','')) 
     except pywintypes.com_error: #unlocking machine 
      time.sleep(.1) 
     else: 
      break 

雖然我不知道原因,但無論如何它工作。