简体   繁体   中英

How to create snapshot with libvirt API in python

I want to manage (create, delete, list...) snapshots in KVM with the libvirt API. After some googling I found the libvirt-domain-snapshot in the link below but I did not find this module for python.

https://libvirt.org/html/libvirt-libvirt-domain-snapshot.html

How can I access the libvirt-domain-snapshot module from python or is there another way to manage snapshots through the libvirt API?

Use pip install libvirt-python to install libvirt bindings. Then libvirt_connection = libvirt.open('qemu:///system') to create connection to libvirt. Get the vm vm_dom = libvirt_connection.lookupByUUIDString(domain_uuid) ( domain_uuid is a string that contains UUID of your domain). And finally call

vm_dom.snapshotCreateXML(
                SNAPSHOT_XML_TEMPLATE.format(snapshot_name=snapshot_name),
                libvirt.VIR_DOMAIN_SNAPSHOT_CREATE_ATOMIC
)

where SNAPSHOT_XML_TEMPLATE looks like this:

SNAPSHOT_XML_TEMPLATE = """<domainsnapshot>
  <name>{snapshot_name}</name>
</domainsnapshot>"""

This will create disc and ram snapshot with given name. libvirt-python compatible with python3 and python2.

Well, if you would look at libvirt's docs and look under Application Development -> Language bindings , you'll see their API has Python Bindings .

Since they import a libvirt module that isn't packed with a regular install of Python there's a big chance you will have to install it yourself. Luckily though it appears to be part of the Python Package Index .

So you could probably just run:

python -m pip install libvirt or python3 -m pip install libvirt (or any other equivalent, depending on which version of Python you are using) to install the module.

Then you're all set :).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM