2006-02-21 13:23:16 +00:00
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2008-04-23 17:08:31 +00:00
< html xmlns = "http://www.w3.org/1999/xhtml" >
<!--
This file is autogenerated from python.html.in
Do not edit this file. Changes will be lost.
-->
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" / >
< link rel = "stylesheet" type = "text/css" href = "main.css" / >
< link rel = "SHORTCUT ICON" href = "32favicon.png" / >
< title > libvirt: Python API bindings< / title >
< meta name = "description" content = "libvirt, virtualization, virtualization API" / >
< / head >
< body >
< div id = "header" >
< div id = "headerLogo" > < / div >
< div id = "headerSearch" >
< form action = "search.php" enctype = "application/x-www-form-urlencoded" method = "get" >
< input id = "query" name = "query" type = "text" size = "12" value = "" / >
< input id = "submit" name = "submit" type = "submit" value = "Search" / >
< / form >
< / div >
< / div >
< div id = "body" >
< div id = "content" >
< h1 > Python API bindings< / h1 >
< p > The Python binding should be complete and are mostly automatically
2007-03-22 15:17:31 +00:00
generated from the formal description of the API in xml. The bindings are
articulated around 2 classes < code > virConnect< / code > 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
2008-04-23 17:08:31 +00:00
lower case, for example the C functions:< / p >
< p >
< code > int < a href = "html/libvirt-libvirt.html#virConnectNumOfDomains" > virConnectNumOfDomains< / a >
(virConnectPtr conn);< / code >
< / p >
< p >
< code > int < a href = "html/libvirt-libvirt.html#virDomainSetMaxMemory" > virDomainSetMaxMemory< / a >
(virDomainPtr domain, unsigned long memory);< / code >
< / p >
< p > become< / p >
< p >
< code > virConn::numOfDomains(self)< / code >
< / p >
< p >
< code > virDomain::setMaxMemory(self, memory)< / code >
< / p >
< p > This process is fully automated, you can get a summary of the conversion
2007-03-22 15:17:31 +00:00
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
2008-04-23 17:08:31 +00:00
specificities in their argument conversions:< / p >
< ul > < li > < code > < a href = "html/libvirt-libvirt.html#virConnectListDomains" > virConnectListDomains< / a > < / code >
2007-03-22 15:17:31 +00:00
is replaced by < code > virDomain::listDomainsID(self)< / code > which returns
2008-04-23 17:08:31 +00:00
a list of the integer ID for the currently running domains< / li > < li > < code > < a href = "html/libvirt-libvirt.html#virDomainGetInfo" > virDomainGetInfo< / a > < / code >
2007-03-22 15:17:31 +00:00
is replaced by < code > virDomain::info()< / code > which returns a list of
2008-04-23 17:08:31 +00:00
< ol > < li > state: one of the state values (virDomainState)< / li > < li > maxMemory: the maximum memory used by the domain< / li > < li > memory: the current amount of memory used by the domain< / li > < li > nbVirtCPU: the number of virtual CPU< / li > < li > cpuTime: the time used by the domain in nanoseconds< / li > < / ol > < / li > < / ul >
< p > So let's look at a simple example inspired from the < code > basic.py< / code >
test found in < code > python/tests/< / code > in the source tree:< / p >
< pre > import < span style = "color: #0071FF; background-color: #FFFFFF" > libvirt< / span >
2006-02-21 13:23:16 +00:00
import sys
conn = < span style = "color: #0071FF; background-color: #FFFFFF" > libvirt< / span > .openReadOnly(None)
if conn == None:
print 'Failed to open connection to the hypervisor'
sys.exit(1)
2006-03-28 14:48:57 +00:00
try:
dom0 = conn.< span style = "color: #007F00; background-color: #FFFFFF" > lookupByName< / span > ("Domain-0")
except:
2006-02-21 13:23:16 +00:00
print 'Failed to find the main domain'
sys.exit(1)
print "Domain 0: id %d running %s" % (dom0.< span style = "color: #FF0080; background-color: #FFFFFF" > ID< / span > (), dom0.< span style = "color: #FF0080; background-color: #FFFFFF" > OSType< / span > ())
2008-04-23 17:08:31 +00:00
print dom0.< span style = "color: #FF0080; background-color: #FFFFFF" > info< / span > ()< / pre >
< p > There is not much to comment about it, it really is a straight mapping
from the C API, the only points to notice are:< / p >
< ul > < li > the import of the module called < code > < span style = "color: #0071FF; background-color: #FFFFFF" > libvirt< / span > < / code > < / li > < li > getting a connection to the hypervisor, in that case using the
openReadOnly function allows the code to execute as a normal user.< / li > < li > getting an object representing the Domain 0 using < span style = "color: #007F00; background-color: #FFFFFF" > lookupByName< / span > < / li > < li > if the domain is not found a libvirtError exception will be raised< / li > < li > extracting and printing some information about the domain using
2007-03-22 15:17:31 +00:00
various < span style = "color: #E50073; background-color: #FFFFFF" > methods< / span >
2008-04-23 17:08:31 +00:00
associated to the virDomain class.< / li > < / ul >
< / div >
< div id = "menu" >
< ul class = "l0" > < li >
< a title = "Front page of the libvirt website" class = "inactive" href = "index.html" > Home< / a >
< / li > < li >
< a title = "Details of new features and bugs fixed in each release" class = "inactive" href = "news.html" > News< / a >
< / li > < li >
< a title = "Get the latest source releases, binary builds and get access to the source repository" class = "inactive" href = "downloads.html" > Downloads< / a >
< / li > < li >
< a title = "Information for users, administrators and developers" class = "active" href = "docs.html" > Documentation< / a >
< ul class = "l1" > < li >
< a title = "Information about deploying and using libvirt" class = "inactive" href = "deployment.html" > Deployment< / a >
< / li > < li >
< a title = "Overview of the logical subsystems in the libvirt API" class = "inactive" href = "intro.html" > Architecture< / a >
< / li > < li >
< a title = "Description of the XML formats used in libvirt" class = "inactive" href = "format.html" > XML format< / a >
< / li > < li >
< a title = "Hypervisor specific driver information" class = "inactive" href = "drivers.html" > Drivers< / a >
< / li > < li >
< a title = "Reference manual for the C public API" class = "inactive" href = "html/index.html" > API reference< / a >
< / li > < li >
< a title = "Bindings of the libvirt API for other languages" class = "active" href = "bindings.html" > Language bindings< / a >
< ul class = "l2" > < li >
< span class = "active" > Python< / span >
< / li > < / ul >
< / li > < / ul >
< / li > < li >
< a title = "User contributed content" class = "inactive" href = "http://wiki.libvirt.org" > Wiki< / a >
< / li > < li >
< a title = "Frequently asked questions" class = "inactive" href = "FAQ.html" > FAQ< / a >
< / li > < li >
< a title = "How and where to report bugs and request features" class = "inactive" href = "bugs.html" > Bug reports< / a >
< / li > < li >
< a title = "How to contact the developers via email and IRC" class = "inactive" href = "contact.html" > Contact< / a >
< / li > < li >
< a title = "Miscellaneous links of interest related to libvirt" class = "inactive" href = "relatedlinks.html" > Related Links< / a >
< / li > < li >
< a title = "Overview of all content on the website" class = "inactive" href = "sitemap.html" > Sitemap< / a >
< / li > < / ul >
< / div >
< / div >
< div id = "footer" >
< div id = "projects" >
< dl id = "p1" > < dt >
< a href = "http://augeas.net/" > Augeas< / a >
< / dt > < dd >
< span > A configuration editing tool and API< / span >
< / dd > < dt >
< a href = "http://libvirt.org/" > libvirt< / a >
< / dt > < dd >
< span > The open source virtualization API< / span >
< / dd > < / dl >
< dl id = "p2" > < dt >
< a href = "http://cobbler.et.redhat.com/" > Cobbler< / a >
< / dt > < dd >
< span > OS provisioning and profile management< / span >
< / dd > < dt >
< a href = "http://ovirt.org/" > oVirt< / a >
< / dt > < dd >
< span > Virtualization management across the data center< / span >
< / dd > < / dl >
< dl id = "p3" > < dt >
< a href = "http://freeipa.org/" > FreeIPA< / a >
< / dt > < dd >
< span > Identity, policy and audit management< / span >
< / dd > < dt >
< a href = "http://virt-manager.org/" > Virtual Machine Manager< / a >
< / dt > < dd >
< span > Virtualization management from the desktop< / span >
< / dd > < / dl >
< / div >
< / div >
< / body >
< / html >