Libvirt comes with bindings to support other languages than pure C. First the headers embeds the necessary declarations to allow direct access from C++ code, but also we have bindings for higher level kind of languages:
Support, requests or help for libvirt bindings are welcome on the mailing list, as usual try to provide enough background informations and make sure you use recent version, see the help page.
The remaining of this page focuses on the Python bindings.
The Python binding should be complete and are mostly automatically
generated from the formal description of the API in xml. The bindings are
articulated around 2 classes virConnect
and virDomain mapping to
the C types. Functions in the C API taking either type as argument then
becomes methods for the classes, their name is just stripped from the
virConnect or virDomain(Get) prefix and the first letter gets converted to
lower case, for example the C functions:
int virConnectNumOfDomains
(virConnectPtr conn);
int virDomainSetMaxMemory
(virDomainPtr domain, unsigned long memory);
become
virConn::numOfDomains(self)
virDomain::setMaxMemory(self, memory)
This process is fully automated, you can get a summary of the conversion in the file libvirtclass.txt present in the python dir or in the docs.There is a couple of function who don't map directly to their C counterparts due to specificities in their argument conversions:
virConnectListDomains
is replaced by virDomain::listDomainsID(self)
which returns
a list of the integer ID for the currently running domainsvirDomainGetInfo
is replaced by virDomain::info()
which returns a list of
So let's look at a simple example inspired from the basic.py
test found in python/tests/
in the source tree:
import libvirt import sys conn = libvirt.openReadOnly(None) if conn == None: print 'Failed to open connection to the hypervisor' sys.exit(1) try: dom0 = conn.lookupByName("Domain-0") except: print 'Failed to find the main domain' sys.exit(1) print "Domain 0: id %d running %s" % (dom0.ID(), dom0.OSType()) print dom0.info()
There is not much to comment about it, it really is a straight mapping from the C API, the only points to notice are:
libvirt
Graphics and design by Diana Fong