1

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?

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 :).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.