2

I am trying to create a snapshot of active/idle virtual machine on KVM hypervisor using the following code:

It looks like the xmlDesc doesnt have enough information to create a snapshot of neither running nor idle virtual machine or maybe something different ?

Do I have to modify the xml dump before passing to the method ? or some separate destination img file is needed for the snapshot ?

Does anybody have any idea how to resolve the issue ?

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

An error message:

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
user2023107
  • 65
  • 1
  • 9

3 Answers3

1

xml file wrong, getXMLDesc(int flags) Provides an XML description of the domain.

you should create the snapshot xml by yourself, not generate automaticlly.

1

To create a snapshot you will need to pass the XML described in this documentation: libvirt: Snapshot XML format. The root tag is domainsnapshot and then define at least name and description.

Rudá Moura
  • 1,221
  • 9
  • 12
0

you should pass a snapshot XML to the function snapshotCreateXML, not the domain's XML.

try the following:

snapshotXML = "<domainsnapshot><name>my-snapshot</name></domainsnapshot>";
domainSnapshot = domain.snapshotCreateXML(snapshotXML);
cd1
  • 15,908
  • 12
  • 46
  • 47