Amon Gabriel
Amon Gabriel

Reputation: 11

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?

Upvotes: 1

Views: 2934

Answers (2)

Montmons
Montmons

Reputation: 1436

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

Upvotes: 1

crashtua
crashtua

Reputation: 491

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.

Upvotes: 6

Related Questions