Search code examples
ansiblebackupqemukvmlibvirt

libvirt: How can I automate domain xml update / verification?


I want to automate the process of backups for domains on an libvirt based Hypervisor. To gain the ability of incremental backups the »Domain XML« needs to contain:

  • the top level qemu namespace declartion attribute and
  • the qemu:capabilities / qemu:add elements

as shown here:

<domain type='kvm' id='1' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
  [..]
  <qemu:capabilities>
    <qemu:add capability='incremental-backup'/>
  </qemu:capabilities>
  [..]
 </domain>

Doing that from an interactive shell is no problem.

$ sudo virsh edit some-domain # that launches an interactive editor (vi/vim)
                              # containing the XML configuration 
                              # saving the result updates the domains
                              # XML

My problem is that I somehow do not have any clue how to automate that with Ansible. In order to check / create those attributes / elements as shown above I can utilize the community.general.xml module, but how can I apply that to the interactive editor launched by virsh edit … ?

There is virsh dumpxml … I can use to gain the XML configuration of a domain, check and modify it. But how to set/write the result?

UPDATE

Thanks to the answer of @peter-krempa the final Ansible code now looks like that:

# "{{ item }}" refers to the name of the domain / vm

- name: Read Domain XML
  community.libvirt.virt:
    command: get_xml
    name: "{{ item }}"
  register: vms_xml


- name: Add namespaced capability for incremental backups
  community.general.xml:
    xmlstring: "{{ vms_xml.get_xml }}"
    xpath: '/domain/qemu:capabilities/qemu:add'
    attribute: capability
    value: incremental-backup
    namespaces:
      qemu: 'http://libvirt.org/schemas/domain/qemu/1.0'
  register: vm_xml_with_inc

- name: Redefine Domain
  community.libvirt.virt:
    name: "{{ item }}"
    command: define
    xml: "{{ vm_xml_with_inc.xmlstring }}"

Solution

  • To write a domain definition (configuration) from an existing XML you already have you use virsh define /path/to/def.xml.

    Note that the reason why backups are not enabled by your libvirt is that it's simply too old and the feature was not completed at that point. At the point when it is completed you no longer need to specify the flag. In your case the backup metadata may break if you'll migrate the VM or want to use some other blockjob or create a snapshot.

    Also note that libvirt's documentation for using <qemu:add capability override states:

    Libvirt provides an XML namespace and an optional library libvirt-qemu.so for dealing specifically with qemu. When used correctly, these extensions allow testing specific qemu features that have not yet been ported to the generic libvirt XML and API interfaces. However, they are unsupported, in that the library is not guaranteed to have a stable API, abusing the library or XML may result in inconsistent state the crashes libvirtd, and upgrading either qemu-kvm or libvirtd may break behavior of a domain that was relying on a qemu-specific pass-through. If you find yourself needing to use them to access a particular qemu feature, then please post an RFE to the libvirt mailing list to get that feature incorporated into the stable libvirt XML and API interfaces.