我如何自动化域XML更新/验证?

huangapple go评论57阅读模式
英文:

How can I automate domain xml update / verification?

问题

I want to automate the process of backups for Domains on a libvirt based Hypervisor. To gain the ability of incremental backups the "Domain XML" needs to contain:

  • the top-level qemu namespace declaration 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 a 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. But how to set/write the result?

UPDATE

Thanks to the answer of @peter-krempa, the final ansible code now looks like this:

# "{{ 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 }}"
英文:

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 show here:

&lt;domain type=&#39;kvm&#39; id=&#39;1&#39; xmlns:qemu=&#39;http://libvirt.org/schemas/domain/qemu/1.0&#39;&gt;
  [..]
  &lt;qemu:capabilities&gt;
    &lt;qemu:add capability=&#39;incremental-backup&#39;/&gt;
  &lt;/qemu:capabilities&gt;
  [..]
 &lt;/domain&gt;

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 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. But how to set/write the result?

UPDATE

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

# &quot;{{ item }}&quot; refers to the name of the domain / vm

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


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

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

答案1

得分: 1

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 &lt;qemu:add capability override states:

> Libvirt提供了一个XML命名空间和一个可选的库
> libvirt-qemu.so专门用于处理qemu。当正确使用时,这些扩展允许测试尚未移植到通用libvirt XML和API接口的特定qemu功能。
> 但是,它们不受支持,因为该库的稳定API不受保证,滥用库或XML可能导致不一致的状态导致libvirtd崩溃,并且升级qemu-kvm或libvirtd中的任何一个可能会破坏依赖于qemu特定传递的域的行为。如果您发现自己需要使用它们来访问特定的qemu功能,那么请在libvirt邮件列表上发布一个RFE,以将该功能合并到稳定的libvirt XML和API接口中。

英文:

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 &lt;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.

huangapple
  • 本文由 发表于 2023年5月21日 12:18:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定