]> xenbits.xensource.com Git - libvirt.git/commitdiff
* configure.in libvirt.spec.in docs/examples/Makefile.am
authorDaniel Veillard <veillard@redhat.com>
Tue, 25 Apr 2006 16:08:48 +0000 (16:08 +0000)
committerDaniel Veillard <veillard@redhat.com>
Tue, 25 Apr 2006 16:08:48 +0000 (16:08 +0000)
  docs/examples/index.py docs/examples/python/*: integrated
  examples for Python from David Lutterkort
Daniel

13 files changed:
ChangeLog
configure.in
docs/examples/Makefile.am
docs/examples/index.py
docs/examples/python/Makefile.am [new file with mode: 0644]
docs/examples/python/README [new file with mode: 0644]
docs/examples/python/dominfo.py [new file with mode: 0755]
docs/examples/python/domrestore.py [new file with mode: 0755]
docs/examples/python/domsave.py [new file with mode: 0755]
docs/examples/python/domstart.py [new file with mode: 0755]
docs/examples/python/guava.xml [new file with mode: 0644]
docs/examples/python/papaya.xml [new file with mode: 0644]
libvirt.spec.in

index c40b127de0f737fedd2da2626bf465f98c6666b8..57fea3f9d9d1c220afd60c1f4dc4b3c957a55aec 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
index 843d829eb262db57d14920e17efe2008a8d36eb0..159a80893188fa786e6c5039ce115765aa54e344 100644 (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)
index 4866bfcd311d61ab8c0a3485d219dea47874f305..18e1b134ddf7363f13b6122b73474828d35341a6 100644 (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 
index 91defeb9bfc032a1277cd3ea16c566824fbd0279..5f56f2f35d0acc18a0992262fd4d4c58b1ee55a8 100755 (executable)
@@ -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 
diff --git a/docs/examples/python/Makefile.am b/docs/examples/python/Makefile.am
new file mode 100644 (file)
index 0000000..ae92d94
--- /dev/null
@@ -0,0 +1,4 @@
+EXTRA_DIST=                                            \
+       README                                          \
+       dominfo.py domrestore.py domsave.py domstart.py \
+       guava.xml papaya.xml
diff --git a/docs/examples/python/README b/docs/examples/python/README
new file mode 100644 (file)
index 0000000..d1593f9
--- /dev/null
@@ -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
+
diff --git a/docs/examples/python/dominfo.py b/docs/examples/python/dominfo.py
new file mode 100755 (executable)
index 0000000..d1009ea
--- /dev/null
@@ -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")
+
+
diff --git a/docs/examples/python/domrestore.py b/docs/examples/python/domrestore.py
new file mode 100755 (executable)
index 0000000..b032138
--- /dev/null
@@ -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/docs/examples/python/domsave.py b/docs/examples/python/domsave.py
new file mode 100755 (executable)
index 0000000..eec7f9e
--- /dev/null
@@ -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/docs/examples/python/domstart.py b/docs/examples/python/domstart.py
new file mode 100755 (executable)
index 0000000..f39eb66
--- /dev/null
@@ -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"
diff --git a/docs/examples/python/guava.xml b/docs/examples/python/guava.xml
new file mode 100644 (file)
index 0000000..4cff175
--- /dev/null
@@ -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>
diff --git a/docs/examples/python/papaya.xml b/docs/examples/python/papaya.xml
new file mode 100644 (file)
index 0000000..51e85d6
--- /dev/null
@@ -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>
index c5e89e00c75a4b4038eae1d0d694649a08739b5d..8d4db9454b8cc8d0829cdb6c02164319071a4b5e 100644 (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