[libvirt] [PATCH] python: Expose blockPeek and memoryPeek in Python binding

Osier Yang jyang at redhat.com
Wed Dec 14 13:16:05 UTC 2011


An simple example to show to use it:

\#! /usr/bin/python

import os
import sys
import libvirt

disk = "/var/lib/libvirt/images/test.img"

conn = libvirt.open(None)
dom = conn.lookupByName('test')

mem_contents = dom.memoryPeek(0, 128, libvirt.VIR_MEMORY_VIRTUAL);
sys.stdout.write(mem_contents)

% python test.py | hexdump
0000000 1660 0209 0000 0000 0000 0000 0000 0000
0000010 0000 0000 0000 0000 d3a0 01d0 0000 0000
0000020
---
 python/generator.py             |    1 +
 python/libvirt-override-api.xml |   17 +++++++++
 python/libvirt-override.c       |   75 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+), 0 deletions(-)

diff --git a/python/generator.py b/python/generator.py
index 88c52b9..1657f4f 100755
--- a/python/generator.py
+++ b/python/generator.py
@@ -262,6 +262,7 @@ py_types = {
     'unsigned char *':  ('z', None, "charPtr", "char *"),
     'char *':  ('z', None, "charPtr", "char *"),
     'const char *':  ('z', None, "charPtrConst", "const char *"),
+    'size_t': ('n', None, "size_t", "size_t"),
 
     'virDomainPtr':  ('O', "virDomain", "virDomainPtr", "virDomainPtr"),
     'const virDomainPtr':  ('O', "virDomain", "virDomainPtr", "virDomainPtr"),
diff --git a/python/libvirt-override-api.xml b/python/libvirt-override-api.xml
index 7c18763..2e0c7cb 100644
--- a/python/libvirt-override-api.xml
+++ b/python/libvirt-override-api.xml
@@ -405,5 +405,22 @@
       <arg name='flags' type='unsigned int' info='an OR'ed set of virDomainModificationImpact'/>
       <return type='int' info='0 in case of success, -1 in case of failure'/>
     </function>
+    <function name='virDomainBlockPeek' file='python'>
+      <info>Read the contents of domain's disk device</info>
+      <arg name='dom' type='virDomainPtr' info='pointer to the domain'/>
+      <arg name='disk' type='const char *' info='disk name'/>
+      <arg name='offset' type='unsigned long long' info='offset within block device'/>
+      <arg name='size' type='size_t' info='size to read'/>
+      <arg name='flags' type='unsigned int' info='unused, always passed 0'/>
+      <return type='char *' info='the returned buffer or None in case of error'/>
+    </function>
+    <function name='virDomainMemoryPeek' file='python'>
+      <info>Read the contents of domain's memory</info>
+      <arg name='dom' type='virDomainPtr' info='pointer to the domain'/>
+      <arg name='start' type='unsigned long long' info='start of memory to peek'/>
+      <arg name='size' type='size_t' info='size of memory to peek'/>
+      <arg name='flags' type='unsigned int' info='an OR'ed set of virDomainMemoryFlags'/>
+      <return type='char *' info='the returned buffer or None in case of error'/>
+    </function>
   </symbols>
 </api>
diff --git a/python/libvirt-override.c b/python/libvirt-override.c
index 9e98918..64d7943 100644
--- a/python/libvirt-override.c
+++ b/python/libvirt-override.c
@@ -5017,6 +5017,79 @@ libvirt_virDomainMigrateGetMaxSpeed(PyObject *self ATTRIBUTE_UNUSED, PyObject *a
     return(py_retval);
 }
 
+static PyObject *
+libvirt_virDomainBlockPeek(PyObject *self ATTRIBUTE_UNUSED,
+                           PyObject *args) {
+    PyObject *py_retval = NULL;
+    int c_retval;
+    virDomainPtr domain;
+    PyObject *pyobj_domain;
+    const char *disk;
+    unsigned long long offset;
+    size_t size;
+    char *buf;
+    unsigned int flags;
+
+    if (!PyArg_ParseTuple(args, (char *)"OzLni:virDomainBlockPeek", &pyobj_domain,
+                          &disk, &offset, &size, &flags))
+        return(NULL);
+
+    domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
+
+    if ((buf = malloc(size)) == NULL)
+        return VIR_PY_NONE;
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    c_retval = virDomainBlockPeek(domain, disk, offset, size, buf, flags);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    if (c_retval < 0)
+        goto cleanup;
+
+    py_retval = PyString_FromStringAndSize(buf, size);
+
+cleanup:
+    memset(buf, 0, size);
+    free(buf);
+    return py_retval;
+}
+
+static PyObject *
+libvirt_virDomainMemoryPeek(PyObject *self ATTRIBUTE_UNUSED,
+                            PyObject *args) {
+    PyObject *py_retval = NULL;
+    int c_retval;
+    virDomainPtr domain;
+    PyObject *pyobj_domain;
+    unsigned long long start;
+    size_t size;
+    char *buf;
+    unsigned int flags;
+
+    if (!PyArg_ParseTuple(args, (char *)"OLni:virDomainMemoryPeek", &pyobj_domain,
+                          &start, &size, &flags))
+        return(NULL);
+
+    domain = (virDomainPtr) PyvirDomain_Get(pyobj_domain);
+
+    if ((buf = malloc(size)) == NULL)
+        return VIR_PY_NONE;
+
+    LIBVIRT_BEGIN_ALLOW_THREADS;
+    c_retval = virDomainMemoryPeek(domain, start, size, buf, flags);
+    LIBVIRT_END_ALLOW_THREADS;
+
+    if (c_retval < 0)
+        goto cleanup;
+
+    py_retval = PyString_FromStringAndSize(buf, size);
+
+cleanup:
+    memset(buf, 0, size);
+    free(buf);
+    return py_retval;
+}
+
 /************************************************************************
  *									*
  *			The registration stuff				*
@@ -5112,6 +5185,8 @@ static PyMethodDef libvirtMethods[] = {
     {(char *) "virDomainGetBlockIoTune", libvirt_virDomainGetBlockIoTune, METH_VARARGS, NULL},
     {(char *) "virDomainSendKey", libvirt_virDomainSendKey, METH_VARARGS, NULL},
     {(char *) "virDomainMigrateGetMaxSpeed", libvirt_virDomainMigrateGetMaxSpeed, METH_VARARGS, NULL},
+    {(char *) "virDomainBlockPeek", libvirt_virDomainBlockPeek, METH_VARARGS, NULL},
+    {(char *) "virDomainMemoryPeek", libvirt_virDomainMemoryPeek, METH_VARARGS, NULL},
     {NULL, NULL, 0, NULL}
 };
 
-- 
1.7.1




More information about the libvir-list mailing list