mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
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
This commit is contained in:
2
examples/python/.gitignore
vendored
Normal file
2
examples/python/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
Makefile
|
||||
Makefile.in
|
||||
3
examples/python/Makefile.am
Normal file
3
examples/python/Makefile.am
Normal file
@@ -0,0 +1,3 @@
|
||||
EXTRA_DIST= \
|
||||
README \
|
||||
dominfo.py domrestore.py domsave.py domstart.py
|
||||
14
examples/python/README
Normal file
14
examples/python/README
Normal file
@@ -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
|
||||
84
examples/python/dominfo.py
Executable file
84
examples/python/dominfo.py
Executable file
@@ -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")
|
||||
36
examples/python/domrestore.py
Executable file
36
examples/python/domrestore.py
Executable 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
examples/python/domsave.py
Executable file
40
examples/python/domsave.py
Executable 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()
|
||||
50
examples/python/domstart.py
Executable file
50
examples/python/domstart.py
Executable 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.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"
|
||||
Reference in New Issue
Block a user