2013-03-16 49 views
2

我嘗試使用下面的代碼來創建有功/無虛擬機上的KVM管理程序的快照:如何使用libvirt創建VM的快照?

它看起來像xmlDesc可是沒有足夠的信息來創建既沒有運行,也沒有空閒的虛擬機或者也可以是快照不同?

在傳遞給方法之前,我是否必須修改xml轉儲? 或快照需要一些單獨的目標img文件?

有沒有人有任何想法如何解決這個問題?

package org.vmclient; 

import org.libvirt.Connect; 
import org.libvirt.Domain; 
import org.libvirt.DomainInfo; 
import org.libvirt.DomainSnapshot; 
import org.libvirt.LibvirtException; 

public class Test { 

public static void main(String[] args) { 

    // create and initialize variables 
    Connect connect = null; 
    Domain domain = null; 
    int numberOfSnaps; 
    DomainSnapshot domainSnapshot; 

    /* 
    * do NOT change! Create a connection 
    */ 
    try { 
     connect = new Connect("qemu:///system"); 
    } catch (LibvirtException e) { 
     System.out.println("exception caught:" + e); 
     System.out.println(e.getError()); 
    } 

    /* 
    * Perform an activity 
    */ 
    try { 
     //create a snapshot 
     domain = connect.domainLookupByName("Ubuntu"); 
     /* 
     DomainInfo di = new DomainInfo(); 
     di = domain.getInfo(); 
     System.out.println(di); 
     */ 
     numberOfSnaps = domain.snapshotNum(); 
     System.out.println(numberOfSnaps); 

     //1. get xmlDesc of current machine 
     String xmlDesc = domain.getXMLDesc(0); 
     //2. check if xmlDesc isnt empty 
     System.out.println(xmlDesc); 

     //3. pass xmlDesc to create a snapshot of the machine 
     //try { 
      domainSnapshot = domain.snapshotCreateXML(xmlDesc); 
     //}catch(LibvirtException e){ 
      //System.out.println(e.getMessage()); 

     //} 
     System.out.println("working ??"); 
     //4. check if snap was created 

     numberOfSnaps = domain.snapshotNum(); 
     System.out.println(numberOfSnaps); 

    } catch (LibvirtException e) { 
     System.out.println("exception caught:" + e); 
     System.out.println(e.getError()); 
    } 
}//end main 
}//end Test.java 

的錯誤消息:

libvir: Domain Snapshot error : XML error: domainsnapshot 
exception caught:org.libvirt.LibvirtException: XML error: domainsnapshot 
level:VIR_ERR_ERROR 
code:VIR_ERR_XML_ERROR 
domain:VIR_FROM_DOMAIN_SNAPSHOT 
hasConn:false 
hasDom:false 
hasNet:false 
message:XML error: domainsnapshot 
str1:XML error: %s 
str2:domainsnapshot 
str3:null 
int1:-1 
int2:-1 

回答

1

xml文件錯誤, getXMLDesc(INT標誌) 提供域的XML描述。

你應該自己創建快照xml,而不是自動生成。

+0

這沒有任何意義 – 2013-04-10 08:04:38

+0

是的,它的作用。這就是libvirt的工作原理。 – cd1 2014-11-18 22:49:18

1

要創建快照,您需要傳遞本文檔中描述的XML:libvirt: Snapshot XML format。根標籤爲domainsnapshot,然後至少定義爲namedescription

0

您應該將快照XML傳遞給函數snapshotCreateXML,而不是域的XML。

嘗試以下操作:

snapshotXML = "<domainsnapshot><name>my-snapshot</name></domainsnapshot>"; 
domainSnapshot = domain.snapshotCreateXML(snapshotXML); 
相關問題