英文:
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:
<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 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:
# "{{ 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 }}"
答案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 <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 <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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论