diff options
author | Daniel P. Berrange <berrange@redhat.com> | 2009-09-16 13:07:48 +0100 |
---|---|---|
committer | Daniel P. Berrange <berrange@redhat.com> | 2009-09-21 14:41:46 +0100 |
commit | cebeba7bd79421a30d9d233f0668d4ec4c4c9060 (patch) | |
tree | 6b1778f3ef3f47b016e33675c3bdec64809c48f2 /examples/python | |
parent | Remove unused images from docs/ directory (diff) | |
download | libvirt-cebeba7bd79421a30d9d233f0668d4ec4c4c9060.tar.gz libvirt-cebeba7bd79421a30d9d233f0668d4ec4c4c9060.tar.bz2 libvirt-cebeba7bd79421a30d9d233f0668d4ec4c4c9060.zip |
Move docs/examples into examples/
* Makefile.am: Add examples/dominfo examples/domsuspend examples/python
as SUBDIRS
* configure.in: Update AC_OUTPUT for new/old Makefiles
* docs/Makefile.am: Remove examples from SUBDIRS
* docs/examples/info1.c: Move to examples/dominfo/info1.c
* docs/examples/suspend.c: Move to examples/domsuspend/suspend.c
* docs/examples: Remove all remaining files
* docs/examples/python: Moved to examples/python/
* examples/dominfo/Makefile.am, examples/domsuspend/Makefile.am: New
build files
* libvirt.spec.in: Update to take account of moved examples
Diffstat (limited to 'examples/python')
-rw-r--r-- | examples/python/.gitignore | 2 | ||||
-rw-r--r-- | examples/python/Makefile.am | 3 | ||||
-rw-r--r-- | examples/python/README | 14 | ||||
-rwxr-xr-x | examples/python/dominfo.py | 84 | ||||
-rwxr-xr-x | examples/python/domrestore.py | 36 | ||||
-rwxr-xr-x | examples/python/domsave.py | 40 | ||||
-rwxr-xr-x | examples/python/domstart.py | 50 |
7 files changed, 229 insertions, 0 deletions
diff --git a/examples/python/.gitignore b/examples/python/.gitignore new file mode 100644 index 000000000..282522db0 --- /dev/null +++ b/examples/python/.gitignore @@ -0,0 +1,2 @@ +Makefile +Makefile.in diff --git a/examples/python/Makefile.am b/examples/python/Makefile.am new file mode 100644 index 000000000..07422260f --- /dev/null +++ b/examples/python/Makefile.am @@ -0,0 +1,3 @@ +EXTRA_DIST= \ + README \ + dominfo.py domrestore.py domsave.py domstart.py diff --git a/examples/python/README b/examples/python/README new file mode 100644 index 000000000..02c5bfb40 --- /dev/null +++ b/examples/python/README @@ -0,0 +1,14 @@ +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 diff --git a/examples/python/dominfo.py b/examples/python/dominfo.py new file mode 100755 index 000000000..e41709c51 --- /dev/null +++ b/examples/python/dominfo.py @@ -0,0 +1,84 @@ +#! /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") diff --git a/examples/python/domrestore.py b/examples/python/domrestore.py new file mode 100755 index 000000000..b0321388d --- /dev/null +++ b/examples/python/domrestore.py @@ -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 diff --git a/examples/python/domsave.py b/examples/python/domsave.py new file mode 100755 index 000000000..35e2c8a54 --- /dev/null +++ b/examples/python/domsave.py @@ -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() diff --git a/examples/python/domstart.py b/examples/python/domstart.py new file mode 100755 index 000000000..52fb79e84 --- /dev/null +++ b/examples/python/domstart.py @@ -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.open(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" |