* configure.in libvirt.spec.in docs/examples/Makefile.am

docs/examples/index.py docs/examples/python/*: integrated
  examples for Python from David Lutterkort
Daniel
This commit is contained in:
Daniel Veillard 2006-04-25 16:08:48 +00:00
parent be54328be3
commit f7a48c19f1
13 changed files with 289 additions and 0 deletions

View File

@ -1,3 +1,9 @@
Tue Apr 25 17:10:10 CEST 2006 Daniel Veillard <veillard@redhat.com>
* configure.in libvirt.spec.in docs/examples/Makefile.am
docs/examples/index.py docs/examples/python/*: integrated
examples for Python from David Lutterkort
Tue Apr 25 13:37:22 EDT 2006 Daniel Veillard <veillard@redhat.com>
* src/xend_internal.c: applied patch from Jim Fehlig when parsing

View File

@ -249,5 +249,6 @@ cp COPYING.LIB COPYING
AC_OUTPUT(Makefile src/Makefile include/Makefile docs/Makefile \
docs/examples/Makefile docs/devhelp/Makefile \
docs/examples/python/Makefile \
libvirt.pc libvirt.spec include/libvirt.h \
python/Makefile python/tests/Makefile)

View File

@ -1,4 +1,5 @@
# Beware this is autogenerated by index.py
SUBDIRS=python
INCLUDES = -I$(top_builddir)/include -I$(top_srcdir)/include -I@srcdir@/include
DEPS = $(top_builddir)/src/libvirt.la
LDADDS = @STATIC_BINARIES@ $(top_builddir)/src/libvirt.la

View File

@ -221,6 +221,7 @@ def dump_Makefile():
for file in glob.glob('*.res'):
extras.append(file)
Makefile="""# Beware this is autogenerated by index.py
SUBDIRS=python
INCLUDES = -I$(top_builddir)/include -I$(top_srcdir)/include -I@srcdir@/include
DEPS = $(top_builddir)/src/libvirt.la
LDADDS = @STATIC_BINARIES@ $(top_builddir)/src/libvirt.la

View File

@ -0,0 +1,4 @@
EXTRA_DIST= \
README \
dominfo.py domrestore.py domsave.py domstart.py \
guava.xml papaya.xml

View File

@ -0,0 +1,15 @@
Some simple examples on how to use the Python API for libvirt
The examples are:
dominfo.py - print information about a running domU based on the results of
virDomainGetInfo and virDomainGetXMLDesc
domstart.py - create a domU from an XML description if the domU isn't
running yet
domsave.py - save all running domU's into a directory
domrestore.py - restore domU's from their saved files in a directory
The XML files in this directory are examples of the XML format that libvirt
expects, and will have to be adapted for your setup. They are only needed
for domstart.py

86
docs/examples/python/dominfo.py Executable file
View File

@ -0,0 +1,86 @@
#! /usr/bin/python
# dominfo - print some information about a domain
import libvirt
import sys
import os
import libxml2
import pdb
def usage():
print 'Usage: %s DOMAIN' % sys.argv[0]
print ' Print information about the domain DOMAIN'
def print_section(title):
print "\n%s" % title
print "=" * 60
def print_entry(key, value):
print "%-10s %-10s" % (key, value)
def print_xml(key, ctx, path):
res = ctx.xpathEval(path)
if res is None or len(res) == 0:
value="Unknown"
else:
value = res[0].content
print_entry(key, value)
return value
if not os.access("/proc/xen", os.R_OK):
print 'System is not running a Xen kernel'
sys.exit(1)
if len(sys.argv) != 2:
usage()
sys.exit(2)
name = sys.argv[1]
# Connect to libvirt
conn = libvirt.openReadOnly(None)
if conn == None:
print 'Failed to open connection to the hypervisor'
sys.exit(1)
try:
dom = conn.lookupByName(name)
# Annoyiingly, libvirt prints its own error message here
except libvirt.libvirtError:
print "Domain %s is not runing" % name
sys.exit(0)
info = dom.info()
print_section("Domain info")
print_entry("State:", info[0])
print_entry("MaxMem:", info[1])
print_entry("UsedMem:", info[2])
print_entry("VCPUs:", info[3])
# Read some info from the XML desc
xmldesc = dom.XMLDesc(0)
doc = libxml2.parseDoc(xmldesc)
ctx = doc.xpathNewContext()
print_section("Kernel")
print_xml("Type:", ctx, "/domain/os/type")
print_xml("Kernel:", ctx, "/domain/os/kernel")
print_xml("initrd:", ctx, "/domain/os/initrd")
print_xml("cmdline:", ctx, "/domain/os/cmdline")
print_section("Devices")
devs = ctx.xpathEval("/domain/devices/*")
for d in devs:
ctx.setContextNode(d)
#pdb.set_trace()
type = print_xml("Type:", ctx, "@type")
if type == "file":
print_xml("Source:", ctx, "source/@file")
print_xml("Target:", ctx, "target/@dev")
elif type == "block":
print_xml("Source:", ctx, "source/@dev")
print_xml("Target:", ctx, "target/@dev")
elif type == "bridge":
print_xml("Source:", ctx, "source/@bridge")
print_xml("MAC Addr:", ctx, "mac/@address")

View File

@ -0,0 +1,36 @@
#! /usr/bin/python
# domstart - make sure a given domU is running, if not start it
import libvirt
import sys
import os
import libxml2
import pdb
def usage():
print 'Usage: %s DIR' % sys.argv[0]
print ' Restore all the domains contained in DIR'
print ' It is assumed that all files in DIR are'
print ' images of domU\'s previously created with save'
if len(sys.argv) != 2:
usage()
sys.exit(2)
dir = sys.argv[1]
imgs = os.listdir(dir)
conn = libvirt.open(None)
if conn == None:
print 'Failed to open connection to the hypervisor'
sys.exit(1)
for img in imgs:
file = os.path.join(dir, img)
print "Restoring %s ... " % img,
sys.stdout.flush()
ret = conn.restore(file)
if ret == 0:
print "done"
else:
print "error %d" % ret

40
docs/examples/python/domsave.py Executable file
View File

@ -0,0 +1,40 @@
#! /usr/bin/python
# domstart - make sure a given domU is running, if not start it
import libvirt
import sys
import os
import libxml2
import pdb
def usage():
print 'Usage: %s DIR' % sys.argv[0]
print ' Save all currently running domU\'s into DIR'
print ' DIR must exist and be writable by this process'
if len(sys.argv) != 2:
usage()
sys.exit(2)
dir = sys.argv[1]
conn = libvirt.open(None)
if conn == None:
print 'Failed to open connection to the hypervisor'
sys.exit(1)
doms = conn.listDomainsID()
for id in doms:
if id == 0:
continue
dom = conn.lookupByID(id)
print "Saving %s[%d] ... " % (dom.name(), id),
sys.stdout.flush()
path = os.path.join(dir, dom.name())
ret = dom.save(path)
if ret == 0:
print "done"
else:
print "error %d" % ret
#pdb.set_trace()

View File

@ -0,0 +1,50 @@
#! /usr/bin/python
# domstart - make sure a given domU is running, if not start it
import libvirt
import sys
import os
import libxml2
import pdb
# Parse the XML description of domU from FNAME
# and return a tuple (name, xmldesc) where NAME
# is the name of the domain, and xmldesc is the contetn of FNAME
def read_domain(fname):
fp = open(fname, "r")
xmldesc = fp.read()
fp.close()
doc = libxml2.parseDoc(xmldesc)
name = doc.xpathNewContext().xpathEval("/domain/name")[0].content
return (name, xmldesc)
def usage():
print 'Usage: %s domain.xml' % sys.argv[0]
print ' Check that the domain described by DOMAIN.XML is running'
print ' If the domain is not running, create it'
print ' DOMAIN.XML must be a XML description of the domain'
print ' in libvirt\'s XML format'
if len(sys.argv) != 2:
usage()
sys.exit(2)
(name, xmldesc) = read_domain(sys.argv[1])
conn = libvirt.openReadOnly(None)
if conn == None:
print 'Failed to open connection to the hypervisor'
sys.exit(1)
try:
dom = conn.lookupByName(name)
except libvirt.libvirtError:
print "Starting domain %s ... " % name,
sys.stdout.flush()
dom = conn.createLinux(xmldesc, 0)
if dom == None:
print "failed"
sys.exit(1)
else:
print "done"

View File

@ -0,0 +1,22 @@
<domain type='xen'>
<name>guava</name>
<os>
<type>linux</type>
<kernel>/var/xen/boot/vmlinuz-2.6.15-1.33_FC5guest</kernel>
<initrd>/var/xen/boot/initrd-2.6.15-1.33_FC5guest.img</initrd>
<cmdline>ro root=/dev/VolGroup00/LogVol00 rhgb quiet</cmdline>
</os>
<memory>262144</memory>
<vcpu>1</vcpu>
<devices>
<disk type='file'>
<source file='/var/xen/guava'/>
<target dev='xvda'/>
</disk>
<interface type='bridge'>
<source bridge='xenbr0'/>
<mac address='00:16:3e:78:a2:42'/>
<script path='/etc/xen/scripts/vif-bridge'/>
</interface>
</devices>
</domain>

View File

@ -0,0 +1,26 @@
<domain type='xen'>
<name>papaya</name>
<os>
<type>linux</type>
<kernel>/var/xen/boot/vmlinuz-2.6.15-1.33_FC5guest</kernel>
<initrd>/var/xen/boot/initrd-2.6.15-1.33_FC5guest.img</initrd>
<cmdline>ro root=/dev/VolGroup00/LogVol00 rhgb quiet single</cmdline>
</os>
<memory>262144</memory>
<vcpu>1</vcpu>
<devices>
<disk type='file'>
<source file='/var/xen/papaya'/>
<target dev='xvda'/>
</disk>
<disk type='block'>
<source dev='/dev/mapper/vg00-lv02'/>
<target dev='sda1'/>
</disk>
<interface type='bridge'>
<source bridge='xenbr0'/>
<mac address='00:16:3e:78:a2:0c'/>
<script path='/etc/xen/scripts/vif-bridge'/>
</interface>
</devices>
</domain>

View File

@ -100,6 +100,7 @@ rm -fr %{buildroot}
%doc python/tests/*.py
%doc python/TODO
%doc python/libvirtclass.txt
%doc docs/examples/python
%changelog
* Mon Apr 10 2006 Daniel Veillard <veillard@redhat.com> 0.1.0-1