Skip to contentSkip to conversation

python-lvm feature parity

PATCH New Tony Asleson 1 participant 13 messages Sep 24, 2012

lore
Share & tools
lore
#0 lore raw

cover python-lvm feature parity

This patch set brings python-lvm up to date with the features that are currently in lvm2app.h.

Minimal unit testing has been done at this time.

Tony Asleson (12):
test.py Reformated to use spaces
Add support for lvm_lv_rename.
Add support for lvm_percent_to_float
Add support for lvm_vg_[get|set]_property
Add support for lvm_lv_from_name
Add support for lvm_lv_from_uuid
Add support for lvm_pv_from_[name|uuid]
Add support for lvm_lv_get_property
Add support for lvm_pv_get_property
Add support for lvm_config_find_bool
Add support for lvm_lv_list_lvsegs, lvm_lvseg_get_property Add support for lvm_pv_list_pvsegs, lvm_pvseg_get_property

 liblvm.c | 507 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 test.py  | 125 ++++++++--------
 2 files changed, 570 insertions(+), 62 deletions(-)
Signature · 1 line
1.7.11.4
#1 lore raw

PATCH 1/12 test.py Reformated to use spaces

---
 test.py | 121 ++++++++++++++++++++++++++++++++--------------------------------
 1 file changed, 60 insertions(+), 61 deletions(-)
test.py +60 -61
index 3fb629a..7a2b078 100644
@@ -9,96 +9,95 @@ import lvm
 
 #Dump information about PV
 def print_pv(pv):
-	print 'PV name: ', pv.getName(), ' ID: ' , pv.getUuid()	, 'Size: ' , pv.getSize()
+    print 'PV name: ', pv.getName(), ' ID: ', pv.getUuid(), 'Size: ', pv.getSize()
 
 
 #Dump some information about a specific volume group
 def print_vg(h, vg_name):
+    #Open read only
+    vg = h.vgOpen(vg_name, 'r')
 
-	#Open read only
-	vg = h.vgOpen(vg_name, 'r')
+    print 'Volume group:', vg_name, 'Size: ', vg.getSize()
 
-	print 'Volume group:', vg_name, 'Size: ', vg.getSize()
+    #Retrieve a list of Physical volumes for this volume group
+    pv_list = vg.listPVs()
 
-	#Retrieve a list of Physical volumes for this volume group
-	pv_list = vg.listPVs()
+    #Print out the physical volumes
+    for p in pv_list:
+        print_pv(p)
 
-	#Print out the physical volumes
-	for p in pv_list:
-		print_pv(p)
+    #Get a list of logical volumes in this volume group
+    lv_list = vg.listLVs()
+    if len(lv_list):
+        for l in lv_list:
+            print 'LV name: ', l.getName(), ' ID: ', l.getUuid()
+    else:
+        print 'No logical volumes present!'
 
-	#Get a list of logical volumes in this volume group
-	lv_list = vg.listLVs()
-	if len(lv_list):
-		for l in lv_list:
-			print 'LV name: ', l.getName(), ' ID: ', l.getUuid()
-	else:
-		print 'No logical volumes present!'
-
-	vg.close()
+    vg.close()
 
 #Returns the name of a vg with space available
 def find_vg_with_free_space(h):
-	free_space = 0
-	rc = None
+    free_space = 0
+    rc = None
 
-	vg_names = l.listVgNames()
-	for v in vg_names:
-		vg = h.vgOpen(v, 'r')
-		c_free = vg.getFreeSize()
-		if c_free > free_space:
-			free_space = c_free
-			rc = v
-		vg.close()
+    vg_names = l.listVgNames()
+    for v in vg_names:
+        vg = h.vgOpen(v, 'r')
+        c_free = vg.getFreeSize()
+        if c_free > free_space:
+            free_space = c_free
+            rc = v
+        vg.close()
 
-	return rc
+    return rc
 
 #Walk through the volume groups and fine one with space in which we can
 #create a new logical volume
 def create_delete_logical_volume(h):
-	vg_name = find_vg_with_free_space(h)
+    vg_name = find_vg_with_free_space(h)
 
-	print 'Using volume group ', vg_name , ' for example'
+    print 'Using volume group ', vg_name, ' for example'
 
-	if vg_name:
-		vg = h.vgOpen(vg_name, 'w')
-		lv = vg.createLvLinear('python_lvm_ok_to_delete', vg.getFreeSize())
+    if vg_name:
+        vg = h.vgOpen(vg_name, 'w')
+        lv = vg.createLvLinear('python_lvm_ok_to_delete', vg.getFreeSize())
 
-		if lv:
-			print 'New lv, id= ' , lv.getUuid()
+        if lv:
+            print 'New lv, id= ', lv.getUuid()
 
-			#Create a tag
-			lv.addTag('Demo_tag')
+            #Create a tag
+            lv.addTag('Demo_tag')
 
-			#Get the tags
-			tags = lv.getTags()
-			for t in tags:
-				#Remove tag
-				lv.removeTag(t)
+            #Get the tags
+            tags = lv.getTags()
+            for t in tags:
+                #Remove tag
+                lv.removeTag(t)
 
-			lv.deactivate()
-			lv.remove()
+            lv.deactivate()
+            lv.remove()
 
-		vg.close()
-	else:
-		print 'No free space available to create demo lv!'
+        vg.close()
+    else:
+        print 'No free space available to create demo lv!'
 
 if __name__ == '__main__':
-	#Create a new LVM instance
-	l = lvm.Liblvm()
+    #Create a new LVM instance
+    l = lvm.Liblvm()
 
-	#What version
-	print 'lvm version=', l.getVersion()
+    #What version
+    print 'lvm version=', l.getVersion()
 
-	#Get a list of volume group names
-	vg_names = l.listVgNames()
+    #Get a list of volume group names
+    vg_names = l.listVgNames()
 
-	#For each volume group display some information about each of them
-	for vg_i in vg_names:
-		print_vg(l, vg_i)
+    #For each volume group display some information about each of them
+    for vg_i in vg_names:
+        print_vg(l, vg_i)
 
-	#Demo creating a logical volume
-	create_delete_logical_volume(l)
+    #Demo creating a logical volume
+    create_delete_logical_volume(l)
 
-	#Close
-	l.close()
+    #Close
+    l.close()
Signature · 1 line
1.7.11.4
#2 lore raw

PATCH 2/12 Add support for lvm_lv_rename.

In reply to Tony Asleson
---
 liblvm.c | 23 ++++++++++++++++++++++-
 test.py  |  4 ++++
 2 files changed, 26 insertions(+), 1 deletion(-)
liblvm.c +22 -1
index f46e158..4c6dcec 100644
@@ -1,4 +1,4 @@
-/* -*- Mode: C; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*- */ 
+/* -*- Mode: C; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*- */
 /*
  * Liblvm -- Python interface to LVM2 API.
  *
@@ -970,6 +970,26 @@ liblvm_lvm_lv_get_tags(lvobject *self)
 }
 
 static PyObject *
+liblvm_lvm_lv_rename(lvobject *self, PyObject *args)
+{
+    const char *new_name;
+    int rval;
+
+    LV_VALID(self);
+
+    if (!PyArg_ParseTuple(args, "s", &new_name))
+        return NULL;
+
+     if ((rval = lvm_lv_rename(self->lv, new_name)) == -1) {
+        PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj));
+        return NULL;
+    }
+
+    Py_INCREF(Py_None);
+    return Py_None;
+}
+
+static PyObject *
 liblvm_lvm_lv_resize(lvobject *self, PyObject *args)
 {
     uint64_t new_size;
@@ -1111,6 +1131,7 @@ static PyMethodDef liblvm_lv_methods[] = {
     { "addTag",          (PyCFunction)liblvm_lvm_lv_add_tag, METH_VARARGS },
     { "removeTag",          (PyCFunction)liblvm_lvm_lv_remove_tag, METH_VARARGS },
     { "getTags",          (PyCFunction)liblvm_lvm_lv_get_tags, METH_NOARGS },
+    { "rename",         (PyCFunction)liblvm_lvm_lv_rename, METH_VARARGS },
     { "resize",          (PyCFunction)liblvm_lvm_lv_resize, METH_VARARGS },
     { NULL,             NULL}   /* sentinel */
 };
test.py +4 -0
index 7a2b078..940716b 100644
@@ -75,6 +75,10 @@ def create_delete_logical_volume(h):
                 #Remove tag
                 lv.removeTag(t)
 
+            #Try to rename
+            lv.rename("python_lvm_ok_to_be_removed_shortly")
+            print 'LV name= ', lv.getName()
+
             lv.deactivate()
             lv.remove()
 
Signature · 1 line
1.7.11.4
#3 lore raw

PATCH 3/12 Add support for lvm_percent_to_float

In reply to Tony Asleson
---
 liblvm.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
liblvm.c +16 -0
index 4c6dcec..a5bd730 100644
@@ -192,6 +192,21 @@ liblvm_lvm_list_vg_uuids(lvmobject *self)
 }
 
 static PyObject *
+liblvm_lvm_percent_to_float(lvmobject *self, PyObject *arg)
+{
+    double converted;
+    int percent;
+
+    LVM_VALID(self);
+
+    if (!PyArg_ParseTuple(arg, "i", &percent))
+        return NULL;
+
+    converted = lvm_percent_to_float(percent);
+    return Py_BuildValue("d", converted);
+}
+
+static PyObject *
 liblvm_lvm_vgname_from_pvid(lvmobject *self, PyObject *arg)
 {
     const char *pvid;
@@ -1082,6 +1097,7 @@ static PyMethodDef Liblvm_methods[] = {
     { "scan",                   (PyCFunction)liblvm_lvm_scan, METH_NOARGS },
     { "listVgNames",          (PyCFunction)liblvm_lvm_list_vg_names, METH_NOARGS },
     { "listVgUuids",          (PyCFunction)liblvm_lvm_list_vg_uuids, METH_NOARGS },
+    { "percentToFloat",        (PyCFunction)liblvm_lvm_percent_to_float, METH_VARARGS },
     { "vgNameFromPvid",        (PyCFunction)liblvm_lvm_vgname_from_pvid, METH_VARARGS },
     { "vgNameFromDevice",        (PyCFunction)liblvm_lvm_vgname_from_device, METH_VARARGS },
 
Signature · 1 line
1.7.11.4
#4 lore raw

PATCH 4/12 Add support for lvm_vg_[get|set]_property

In reply to Tony Asleson
---
 liblvm.c | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 143 insertions(+)
liblvm.c +143 -0
index a5bd730..0b2a436 100644
@@ -618,6 +618,147 @@ liblvm_lvm_vg_get_free_extent_count(vgobject *self)
     return Py_BuildValue("l", lvm_vg_get_free_extent_count(self->vg));
 }
 
+
+/* This will return a tuple of (value, bool) with the value being a string or
+   integer and bool indicating if property is settable */
+static PyObject *
+liblvm_lvm_vg_get_property(vgobject *self,  PyObject *args)
+{
+    const char *name;
+    struct lvm_property_value prop_value;
+    PyObject * pytuple;
+
+    VG_VALID(self);
+
+     if (!PyArg_ParseTuple(args, "s", &name))
+        return NULL;
+
+    prop_value = lvm_vg_get_property(self->vg, name);
+
+    if( !prop_value.is_valid ) {
+        PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj));
+        return NULL;
+    } else {
+        PyObject *setable;
+
+        pytuple = PyTuple_New(2);
+        if (!pytuple)
+            return NULL;
+
+        if( prop_value.is_integer ) {
+            PyTuple_SET_ITEM(pytuple, 0, Py_BuildValue("K", prop_value.value.integer));
+        } else {
+            PyTuple_SET_ITEM(pytuple, 0, PyString_FromString(prop_value.value.string));
+        }
+
+        if (prop_value.is_settable) {
+            setable = Py_True;
+        } else {
+            setable = Py_False;
+        }
+
+        Py_INCREF(setable);
+        PyTuple_SET_ITEM(pytuple, 1, setable);
+    }
+
+    return pytuple;
+}
+
+static PyObject *
+liblvm_lvm_vg_set_property(vgobject *self,  PyObject *args)
+{
+    const char *property_name = NULL;
+    PyObject *variant_type_arg = NULL;
+    struct lvm_property_value lvm_property;
+    char *string_value = NULL;
+
+    VG_VALID(self);
+
+    if (!PyArg_ParseTuple(args, "sO", &property_name, &variant_type_arg))
+        return NULL;
+
+    lvm_property = lvm_vg_get_property(self->vg, property_name);
+
+    if( !lvm_property.is_valid ) {
+        goto lvmerror;
+    }
+
+    if(PyObject_IsInstance(variant_type_arg, (PyObject*)&PyString_Type)) {
+
+        if (!lvm_property.is_string) {
+            PyErr_Format(PyExc_ValueError, "Property requires string value");
+            goto bail;
+        }
+
+        /* Based on cursory code inspection this path may cause a memory
+            leak when calling into set_property, need to verify*/
+        string_value = strdup(PyString_AsString(variant_type_arg));
+        lvm_property.value.string = string_value;
+        if(!lvm_property.value.string) {
+            PyErr_NoMemory();
+            goto bail;
+        }
+
+    } else {
+
+        if (!lvm_property.is_integer) {
+            PyErr_Format(PyExc_ValueError, "Property requires numeric value");
+            goto bail;
+        }
+
+        if(PyObject_IsInstance(variant_type_arg, (PyObject*)&PyInt_Type)) {
+            int temp_py_int = PyInt_AsLong(variant_type_arg);
+
+            /* -1 could be valid, need to see if an exception was gen. */
+            if( -1 == temp_py_int ) {
+                if( PyErr_Occurred() ) {
+                    goto bail;
+                }
+            }
+
+            if (temp_py_int < 0) {
+                PyErr_Format(PyExc_ValueError, "Positive integers only!");
+                goto bail;
+            }
+
+            lvm_property.value.integer = temp_py_int;
+        } else if(PyObject_IsInstance(variant_type_arg, (PyObject*)&PyLong_Type)){
+            /* This will fail on negative numbers */
+            unsigned long long temp_py_long = PyLong_AsUnsignedLongLong(variant_type_arg);
+            if( (unsigned long long)-1 == temp_py_long ) {
+                goto bail;
+            }
+
+            lvm_property.value.integer = temp_py_long;
+        } else {
+            PyErr_Format(PyExc_ValueError, "supported value types are numeric and string");
+            goto bail;
+        }
+    }
+
+    if( -1 == lvm_vg_set_property(self->vg, property_name, &lvm_property) ) {
+        goto lvmerror;
+    }
+
+    if( -1 == lvm_vg_write(self->vg)) {
+        goto lvmerror;
+    }
+
+    Py_DECREF(variant_type_arg);
+    Py_INCREF(Py_None);
+    return Py_None;
+
+ lvmerror:
+    PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj));
+ bail:
+    free(string_value);
+    if( variant_type_arg ) {
+        Py_DECREF(variant_type_arg);
+        variant_type_arg = NULL;
+    }
+    return NULL;
+}
+
 static PyObject *
 liblvm_lvm_vg_get_pv_count(vgobject *self)
 {
@@ -1124,6 +1265,8 @@ static PyMethodDef liblvm_vg_methods[] = {
     { "getExtentSize",          (PyCFunction)liblvm_lvm_vg_get_extent_size, METH_NOARGS },
     { "getExtentCount",          (PyCFunction)liblvm_lvm_vg_get_extent_count, METH_NOARGS },
     { "getFreeExtentCount",  (PyCFunction)liblvm_lvm_vg_get_free_extent_count, METH_NOARGS },
+    { "getProperty",          (PyCFunction)liblvm_lvm_vg_get_property, METH_VARARGS },
+    { "setProperty",          (PyCFunction)liblvm_lvm_vg_set_property, METH_VARARGS },
     { "getPvCount",          (PyCFunction)liblvm_lvm_vg_get_pv_count, METH_NOARGS },
     { "getMaxPv",          (PyCFunction)liblvm_lvm_vg_get_max_pv, METH_NOARGS },
     { "getMaxLv",          (PyCFunction)liblvm_lvm_vg_get_max_lv, METH_NOARGS },
Signature · 1 line
1.7.11.4
#5 lore raw

PATCH 5/12 Add support for lvm_lv_from_name

In reply to Tony Asleson
---
 liblvm.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
liblvm.c +29 -0
index 0b2a436..b61f3da 100644
@@ -938,6 +938,34 @@ liblvm_lvm_vg_list_pvs(vgobject *vg)
     return pytuple;
 }
 
+static PyObject *
+liblvm_lvm_lv_from_name(vgobject *self, PyObject *arg)
+{
+    const char *name;
+    lvobject *rc;
+    lv_t lv = NULL;
+
+    VG_VALID(self);
+
+    if (!PyArg_ParseTuple(arg, "s", &name))
+        return NULL;
+
+    lv = lvm_lv_from_name(self->vg, name);
+    if( !lv ) {
+        PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj));
+        return NULL;
+    }
+
+    rc = PyObject_New(lvobject, &LibLVMlvType);
+    if( !rc ) {
+        return NULL;
+    }
+
+    rc->lv = lv;
+    rc->lvm_obj = self->lvm_obj;
+    return (PyObject *)rc;
+}
+
 static void
 liblvm_pv_dealloc(pvobject *self)
 {
@@ -1272,6 +1300,7 @@ static PyMethodDef liblvm_vg_methods[] = {
     { "getMaxLv",          (PyCFunction)liblvm_lvm_vg_get_max_lv, METH_NOARGS },
     { "listLVs",          (PyCFunction)liblvm_lvm_vg_list_lvs, METH_NOARGS },
     { "listPVs",          (PyCFunction)liblvm_lvm_vg_list_pvs, METH_NOARGS },
+    { "lvFromName",       (PyCFunction)liblvm_lvm_lv_from_name, METH_VARARGS },
     { "getTags",          (PyCFunction)liblvm_lvm_vg_get_tags, METH_NOARGS },
     { "createLvLinear",          (PyCFunction)liblvm_lvm_vg_create_lv_linear, METH_VARARGS },
     { NULL,             NULL}   /* sentinel */
Signature · 1 line
1.7.11.4
#6 lore raw

PATCH 6/12 Add support for lvm_lv_from_uuid

In reply to Tony Asleson
---
 liblvm.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
liblvm.c +19 -4
index b61f3da..2ead1ce 100644
@@ -938,19 +938,21 @@ liblvm_lvm_vg_list_pvs(vgobject *vg)
     return pytuple;
 }
 
+typedef lv_t (*lv_fetch_by_N)(vg_t vg, const char *id);
+
 static PyObject *
-liblvm_lvm_lv_from_name(vgobject *self, PyObject *arg)
+liblvm_lvm_lv_from_N(vgobject *self, PyObject *arg, lv_fetch_by_N method)
 {
-    const char *name;
+    const char *id;
     lvobject *rc;
     lv_t lv = NULL;
 
     VG_VALID(self);
 
-    if (!PyArg_ParseTuple(arg, "s", &name))
+    if (!PyArg_ParseTuple(arg, "s", &id))
         return NULL;
 
-    lv = lvm_lv_from_name(self->vg, name);
+    lv = method(self->vg, id);
     if( !lv ) {
         PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj));
         return NULL;
@@ -966,6 +968,18 @@ liblvm_lvm_lv_from_name(vgobject *self, PyObject *arg)
     return (PyObject *)rc;
 }
 
+static PyObject *
+liblvm_lvm_lv_from_name(vgobject *self, PyObject *arg)
+{
+    return liblvm_lvm_lv_from_N(self, arg, lvm_lv_from_name);
+}
+
+static PyObject *
+liblvm_lvm_lv_from_uuid(vgobject *self, PyObject *arg)
+{
+    return liblvm_lvm_lv_from_N(self, arg, lvm_lv_from_uuid);
+}
+
 static void
 liblvm_pv_dealloc(pvobject *self)
 {
@@ -1301,6 +1315,7 @@ static PyMethodDef liblvm_vg_methods[] = {
     { "listLVs",          (PyCFunction)liblvm_lvm_vg_list_lvs, METH_NOARGS },
     { "listPVs",          (PyCFunction)liblvm_lvm_vg_list_pvs, METH_NOARGS },
     { "lvFromName",       (PyCFunction)liblvm_lvm_lv_from_name, METH_VARARGS },
+    { "lvFromUuid",       (PyCFunction)liblvm_lvm_lv_from_uuid, METH_VARARGS },
     { "getTags",          (PyCFunction)liblvm_lvm_vg_get_tags, METH_NOARGS },
     { "createLvLinear",          (PyCFunction)liblvm_lvm_vg_create_lv_linear, METH_VARARGS },
     { NULL,             NULL}   /* sentinel */
Signature · 1 line
1.7.11.4
#7 lore raw

PATCH 7/12 Add support for lvm_pv_from_[name|uuid]

In reply to Tony Asleson
---
 liblvm.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
liblvm.c +43 -0
index 2ead1ce..cccc35a 100644
@@ -939,6 +939,7 @@ liblvm_lvm_vg_list_pvs(vgobject *vg)
 }
 
 typedef lv_t (*lv_fetch_by_N)(vg_t vg, const char *id);
+typedef pv_t (*pv_fetch_by_N)(vg_t vg, const char *id);
 
 static PyObject *
 liblvm_lvm_lv_from_N(vgobject *self, PyObject *arg, lv_fetch_by_N method)
@@ -980,6 +981,46 @@ liblvm_lvm_lv_from_uuid(vgobject *self, PyObject *arg)
     return liblvm_lvm_lv_from_N(self, arg, lvm_lv_from_uuid);
 }
 
+static PyObject *
+liblvm_lvm_pv_from_N(vgobject *self, PyObject *arg, pv_fetch_by_N method)
+{
+    const char *id;
+    pvobject *rc;
+    pv_t pv = NULL;
+
+    VG_VALID(self);
+
+    if (!PyArg_ParseTuple(arg, "s", &id))
+        return NULL;
+
+    pv = method(self->vg, id);
+    if( !pv ) {
+        PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj));
+        return NULL;
+    }
+
+    rc = PyObject_New(pvobject, &LibLVMpvType);
+    if( !rc ) {
+        return NULL;
+    }
+
+    rc->pv = pv;
+    rc->lvm_obj = self->lvm_obj;
+    return (PyObject *)rc;
+}
+
+static PyObject *
+liblvm_lvm_pv_from_name(vgobject *self, PyObject *arg)
+{
+    return liblvm_lvm_pv_from_N(self, arg, lvm_pv_from_name);
+}
+
+static PyObject *
+liblvm_lvm_pv_from_uuid(vgobject *self, PyObject *arg)
+{
+    return liblvm_lvm_pv_from_N(self, arg, lvm_pv_from_uuid);
+}
+
 static void
 liblvm_pv_dealloc(pvobject *self)
 {
@@ -1316,6 +1357,8 @@ static PyMethodDef liblvm_vg_methods[] = {
     { "listPVs",          (PyCFunction)liblvm_lvm_vg_list_pvs, METH_NOARGS },
     { "lvFromName",       (PyCFunction)liblvm_lvm_lv_from_name, METH_VARARGS },
     { "lvFromUuid",       (PyCFunction)liblvm_lvm_lv_from_uuid, METH_VARARGS },
+    { "pvFromName",       (PyCFunction)liblvm_lvm_pv_from_name, METH_VARARGS },
+    { "pvFromUuid",       (PyCFunction)liblvm_lvm_pv_from_uuid, METH_VARARGS },
     { "getTags",          (PyCFunction)liblvm_lvm_vg_get_tags, METH_NOARGS },
     { "createLvLinear",          (PyCFunction)liblvm_lvm_vg_create_lv_linear, METH_VARARGS },
     { NULL,             NULL}   /* sentinel */
Signature · 1 line
1.7.11.4
#8 lore raw

PATCH 8/12 Add support for lvm_lv_get_property

In reply to Tony Asleson
---
 liblvm.c | 80 +++++++++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 51 insertions(+), 29 deletions(-)
liblvm.c +51 -29
index cccc35a..1fa13fe 100644
@@ -618,6 +618,38 @@ liblvm_lvm_vg_get_free_extent_count(vgobject *self)
     return Py_BuildValue("l", lvm_vg_get_free_extent_count(self->vg));
 }
 
+/* Builds a python tuple ([string|number], bool) from a struct lvm_property_value */
+static PyObject *
+get_property(lvmobject *h, struct lvm_property_value *prop)
+{
+    PyObject *pytuple;
+    PyObject *setable;
+
+    if( !prop->is_valid ) {
+        PyErr_SetObject(LibLVMError, liblvm_get_last_error(h));
+        return NULL;
+    }
+
+    pytuple = PyTuple_New(2);
+    if (!pytuple)
+        return NULL;
+
+    if( prop->is_integer ) {
+        PyTuple_SET_ITEM(pytuple, 0, Py_BuildValue("K", prop->value.integer));
+    } else {
+        PyTuple_SET_ITEM(pytuple, 0, PyString_FromString(prop->value.string));
+    }
+
+    if (prop->is_settable) {
+        setable = Py_True;
+    } else {
+        setable = Py_False;
+    }
+
+    Py_INCREF(setable);
+    PyTuple_SET_ITEM(pytuple, 1, setable);
+    return pytuple;
+}
 
 /* This will return a tuple of (value, bool) with the value being a string or
    integer and bool indicating if property is settable */
@@ -626,7 +658,6 @@ liblvm_lvm_vg_get_property(vgobject *self,  PyObject *args)
 {
     const char *name;
     struct lvm_property_value prop_value;
-    PyObject * pytuple;
 
     VG_VALID(self);
 
@@ -634,34 +665,7 @@ liblvm_lvm_vg_get_property(vgobject *self,  PyObject *args)
         return NULL;
 
     prop_value = lvm_vg_get_property(self->vg, name);
-
-    if( !prop_value.is_valid ) {
-        PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj));
-        return NULL;
-    } else {
-        PyObject *setable;
-
-        pytuple = PyTuple_New(2);
-        if (!pytuple)
-            return NULL;
-
-        if( prop_value.is_integer ) {
-            PyTuple_SET_ITEM(pytuple, 0, Py_BuildValue("K", prop_value.value.integer));
-        } else {
-            PyTuple_SET_ITEM(pytuple, 0, PyString_FromString(prop_value.value.string));
-        }
-
-        if (prop_value.is_settable) {
-            setable = Py_True;
-        } else {
-            setable = Py_False;
-        }
-
-        Py_INCREF(setable);
-        PyTuple_SET_ITEM(pytuple, 1, setable);
-    }
-
-    return pytuple;
+    return get_property(self->lvm_obj, &prop_value);
 }
 
 static PyObject *
@@ -1104,6 +1108,23 @@ liblvm_lvm_vg_remove_lv(lvobject *self)
     return Py_None;
 }
 
+/* This will return a tuple of (value, bool) with the value being a string or
+   integer and bool indicating if property is settable */
+static PyObject *
+liblvm_lvm_lv_get_property(lvobject *self,  PyObject *args)
+{
+    const char *name;
+    struct lvm_property_value prop_value;
+
+    LV_VALID(self);
+
+     if (!PyArg_ParseTuple(args, "s", &name))
+        return NULL;
+
+    prop_value = lvm_lv_get_property(self->lv, name);
+    return get_property(self->lvm_obj, &prop_value);
+}
+
 static PyObject *
 liblvm_lvm_lv_get_size(lvobject *self)
 {
@@ -1371,6 +1392,7 @@ static PyMethodDef liblvm_lv_methods[] = {
     { "activate",          (PyCFunction)liblvm_lvm_lv_activate, METH_NOARGS },
     { "deactivate",          (PyCFunction)liblvm_lvm_lv_deactivate, METH_NOARGS },
     { "remove",          (PyCFunction)liblvm_lvm_vg_remove_lv, METH_NOARGS },
+    { "getProperty",        (PyCFunction)liblvm_lvm_lv_get_property, METH_VARARGS },
     { "getSize",          (PyCFunction)liblvm_lvm_lv_get_size, METH_NOARGS },
     { "isActive",          (PyCFunction)liblvm_lvm_lv_is_active, METH_NOARGS },
     { "isSuspended",          (PyCFunction)liblvm_lvm_lv_is_suspended, METH_NOARGS },
Signature · 1 line
1.7.11.4
#9 lore raw

PATCH 9/12 Add support for lvm_pv_get_property

In reply to Tony Asleson
---
 liblvm.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
liblvm.c +24 -0
index 1fa13fe..7af81bd 100644
@@ -1272,6 +1272,14 @@ liblvm_lvm_lv_resize(lvobject *self, PyObject *args)
 
 /* PV Methods */
 
+#define PV_VALID(pvobject) \
+    do {                   \
+    if (!pvobject->pv || !pvobject->lvm_obj) {                                               \
+        PyErr_SetString(PyExc_UnboundLocalError, "PV object invalid"); \
+        return NULL; \
+        } \
+    } while (0)
+
 static PyObject *
 liblvm_lvm_pv_get_name(pvobject *self)
 {
@@ -1291,6 +1299,21 @@ liblvm_lvm_pv_get_mda_count(pvobject *self)
 }
 
 static PyObject *
+liblvm_lvm_pv_get_property(pvobject *self,  PyObject *args)
+{
+    const char *name;
+    struct lvm_property_value prop_value;
+
+    PV_VALID(self);
+
+     if (!PyArg_ParseTuple(args, "s", &name))
+        return NULL;
+
+    prop_value = lvm_pv_get_property(self->pv, name);
+    return get_property(self->lvm_obj, &prop_value);
+}
+
+static PyObject *
 liblvm_lvm_pv_get_dev_size(pvobject *self)
 {
     return Py_BuildValue("l", lvm_pv_get_dev_size(self->pv));
@@ -1409,6 +1432,7 @@ static PyMethodDef liblvm_pv_methods[] = {
     { "getName",          (PyCFunction)liblvm_lvm_pv_get_name, METH_NOARGS },
     { "getUuid",          (PyCFunction)liblvm_lvm_pv_get_uuid, METH_NOARGS },
     { "getMdaCount",          (PyCFunction)liblvm_lvm_pv_get_mda_count, METH_NOARGS },
+    { "getProperty",        (PyCFunction)liblvm_lvm_pv_get_property, METH_VARARGS },
     { "getSize",          (PyCFunction)liblvm_lvm_pv_get_size, METH_NOARGS },
     { "getDevSize",          (PyCFunction)liblvm_lvm_pv_get_dev_size, METH_NOARGS },
     { "getFree",          (PyCFunction)liblvm_lvm_pv_get_free, METH_NOARGS },
Signature · 1 line
1.7.11.4
#10 lore raw

PATCH 10/12 Add support for lvm_config_find_bool

In reply to Tony Asleson
---
 liblvm.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
liblvm.c +26 -0
index 7af81bd..e44b43d 100644
@@ -244,6 +244,31 @@ liblvm_lvm_vgname_from_device(lvmobject *self, PyObject *arg)
     return Py_BuildValue("s", vgname);
 }
 
+
+static PyObject *
+liblvm_lvm_config_find_bool(lvmobject *self, PyObject *arg)
+{
+    const char *config;
+    int rval;
+    PyObject *rc;
+
+    LVM_VALID(self);
+
+    if (!PyArg_ParseTuple(arg, "s", &config))
+       return NULL;
+
+    if ((rval = lvm_config_find_bool(self->libh, config, -10)) == -10) {
+        /*Retrieving error information yields no error in this case*/
+        PyErr_Format(PyExc_ValueError, "config path not found");
+        return NULL;
+    }
+
+    rc = (rval) ? Py_True: Py_False;
+
+    Py_INCREF(rc);
+    return rc;
+}
+
 static PyObject *
 liblvm_lvm_config_reload(lvmobject *self)
 {
@@ -1360,6 +1385,7 @@ static PyMethodDef Liblvm_methods[] = {
     { "vgOpen",                (PyCFunction)liblvm_lvm_vg_open, METH_VARARGS },
     { "vgCreate",                (PyCFunction)liblvm_lvm_vg_create, METH_VARARGS },
     { "close",                  (PyCFunction)liblvm_close, METH_NOARGS },
+    { "configFindBool",        (PyCFunction)liblvm_lvm_config_find_bool, METH_VARARGS },
     { "configReload",          (PyCFunction)liblvm_lvm_config_reload, METH_NOARGS },
     { "configOverride",        (PyCFunction)liblvm_lvm_config_override, METH_VARARGS },
     { "scan",                   (PyCFunction)liblvm_lvm_scan, METH_NOARGS },
Signature · 1 line
1.7.11.4
#11 lore raw

PATCH 11/12 Add support for lvm_lv_list_lvsegs, lvm_lvseg_get_property

In reply to Tony Asleson
---
 liblvm.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 83 insertions(+)
liblvm.c +83 -0
index e44b43d..62521fd 100644
@@ -48,9 +48,16 @@ typedef struct {
     lvmobject *lvm_obj;
 } pvobject;
 
+typedef struct {
+    PyObject_HEAD
+    lvseg_t    lv_seg;              /* lv segment handle */
+    lvmobject *lvm_obj;
+} lvsegobject;
+
 static PyTypeObject LibLVMvgType;
 static PyTypeObject LibLVMlvType;
 static PyTypeObject LibLVMpvType;
+static PyTypeObject LibLVMlvsegType;
 
 static PyObject *LibLVMError;
 
@@ -1295,6 +1302,43 @@ liblvm_lvm_lv_resize(lvobject *self, PyObject *args)
     return Py_None;
 }
 
+static PyObject *
+liblvm_lvm_lv_list_lvsegs(lvobject *lv)
+{
+    struct dm_list  *lvsegs;
+    lvseg_list_t    *lvsegl;
+    PyObject * pytuple;
+    lvsegobject *self;
+    int i = 0;
+
+    LV_VALID(lv);
+
+    lvsegs = lvm_lv_list_lvsegs(lv->lv);
+    if(!lvsegs) {
+        return Py_BuildValue("()");
+    }
+
+    pytuple = PyTuple_New(dm_list_size(lvsegs));
+    if (!pytuple)
+        return NULL;
+
+    dm_list_iterate_items(lvsegl, lvsegs) {
+        /* Create and initialize the object */
+        self = PyObject_New(lvsegobject, &LibLVMlvsegType);
+        if (!self) {
+            Py_DECREF(pytuple);
+            return NULL;
+        }
+
+        self->lv_seg = lvsegl->lvseg;
+        self->lvm_obj = lv->lvm_obj;
+        PyTuple_SET_ITEM(pytuple, i, (PyObject *) self);
+        i++;
+    }
+
+    return pytuple;
+}
+
 /* PV Methods */
 
 #define PV_VALID(pvobject) \
@@ -1375,6 +1419,27 @@ liblvm_lvm_pv_resize(pvobject *self, PyObject *args)
     return Py_None;
 }
 
+/* LV seg methods */
+
+static void
+liblvm_lvseg_dealloc(lvsegobject *self)
+{
+    PyObject_Del(self);
+}
+
+static PyObject *
+liblvm_lvm_lvseg_get_property(lvsegobject *self,  PyObject *args)
+{
+    const char *name;
+    struct lvm_property_value prop_value;
+
+     if (!PyArg_ParseTuple(args, "s", &name))
+        return NULL;
+
+    prop_value = lvm_lvseg_get_property(self->lv_seg, name);
+    return get_property(self->lvm_obj, &prop_value);
+}
+
 /* ----------------------------------------------------------------------
  * Method tables and other bureaucracy
  */
@@ -1450,6 +1515,7 @@ static PyMethodDef liblvm_lv_methods[] = {
     { "getTags",          (PyCFunction)liblvm_lvm_lv_get_tags, METH_NOARGS },
     { "rename",         (PyCFunction)liblvm_lvm_lv_rename, METH_VARARGS },
     { "resize",          (PyCFunction)liblvm_lvm_lv_resize, METH_VARARGS },
+    { "listLVsegs",          (PyCFunction)liblvm_lvm_lv_list_lvsegs, METH_NOARGS },
     { NULL,             NULL}   /* sentinel */
 };
 
@@ -1466,6 +1532,10 @@ static PyMethodDef liblvm_pv_methods[] = {
     { NULL,             NULL}   /* sentinel */
 };
 
+static PyMethodDef liblvm_lvseg_methods[] = {
+    { "getProperty",        (PyCFunction)liblvm_lvm_lvseg_get_property, METH_VARARGS }
+};
+
 static PyTypeObject LiblvmType = {
     PyObject_HEAD_INIT(&PyType_Type)
     .tp_name = "liblvm.Liblvm",
@@ -1511,6 +1581,17 @@ static PyTypeObject LibLVMpvType = {
     .tp_methods = liblvm_pv_methods,
 };
 
+static PyTypeObject LibLVMlvsegType = {
+    PyObject_HEAD_INIT(&PyType_Type)
+    .tp_name = "liblvm.Liblvm_lvseg",
+    .tp_basicsize = sizeof(lvsegobject),
+    .tp_new = PyType_GenericNew,
+    .tp_dealloc = (destructor)liblvm_lvseg_dealloc,
+    .tp_flags = Py_TPFLAGS_DEFAULT,
+    .tp_doc = "LVM Logical Volume Segment object",
+    .tp_methods = liblvm_lvseg_methods,
+};
+
 PyMODINIT_FUNC
 initlvm(void)
 {
@@ -1524,6 +1605,8 @@ initlvm(void)
         return;
     if (PyType_Ready(&LibLVMpvType) < 0)
         return;
+    if (PyType_Ready(&LibLVMlvsegType) < 0)
+        return;
 
     m = Py_InitModule3("lvm", Liblvm_methods, "Liblvm module");
     if (m == NULL)
Signature · 1 line
1.7.11.4
#12 lore raw

PATCH 12/12 Add support for lvm_pv_list_pvsegs, lvm_pvseg_get_property

In reply to Tony Asleson
---
 liblvm.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 83 insertions(+)
liblvm.c +83 -0
index 62521fd..a492fb6 100644
@@ -54,10 +54,17 @@ typedef struct {
     lvmobject *lvm_obj;
 } lvsegobject;
 
+typedef struct {
+    PyObject_HEAD
+    pvseg_t    pv_seg;              /* pv segment handle */
+    lvmobject *lvm_obj;
+} pvsegobject;
+
 static PyTypeObject LibLVMvgType;
 static PyTypeObject LibLVMlvType;
 static PyTypeObject LibLVMpvType;
 static PyTypeObject LibLVMlvsegType;
+static PyTypeObject LibLVMpvsegType;
 
 static PyObject *LibLVMError;
 
@@ -1419,6 +1426,43 @@ liblvm_lvm_pv_resize(pvobject *self, PyObject *args)
     return Py_None;
 }
 
+static PyObject *
+liblvm_lvm_lv_list_pvsegs(pvobject *pv)
+{
+    struct dm_list *pvsegs;
+    pvseg_list_t *pvsegl;
+    PyObject *pytuple;
+    pvsegobject *self;
+    int i = 0;
+
+    PV_VALID(pv);
+
+    pvsegs = lvm_pv_list_pvsegs(pv->pv);
+    if(!pvsegs) {
+        return Py_BuildValue("()");
+    }
+
+    pytuple = PyTuple_New(dm_list_size(pvsegs));
+    if (!pytuple)
+        return NULL;
+
+    dm_list_iterate_items(pvsegl, pvsegs) {
+        /* Create and initialize the object */
+        self = PyObject_New(pvsegobject, &LibLVMpvsegType);
+        if (!self) {
+            Py_DECREF(pytuple);
+            return NULL;
+        }
+
+        self->pv_seg = pvsegl->pvseg;
+        self->lvm_obj = pv->lvm_obj;
+        PyTuple_SET_ITEM(pytuple, i, (PyObject *) self);
+        i++;
+    }
+
+    return pytuple;
+}
+
 /* LV seg methods */
 
 static void
@@ -1440,6 +1484,27 @@ liblvm_lvm_lvseg_get_property(lvsegobject *self,  PyObject *args)
     return get_property(self->lvm_obj, &prop_value);
 }
 
+/* PV seg methods */
+
+static void
+liblvm_pvseg_dealloc(pvsegobject *self)
+{
+    PyObject_Del(self);
+}
+
+static PyObject *
+liblvm_lvm_pvseg_get_property(pvsegobject *self,  PyObject *args)
+{
+    const char *name;
+    struct lvm_property_value prop_value;
+
+     if (!PyArg_ParseTuple(args, "s", &name))
+        return NULL;
+
+    prop_value = lvm_pvseg_get_property(self->pv_seg, name);
+    return get_property(self->lvm_obj, &prop_value);
+}
+
 /* ----------------------------------------------------------------------
  * Method tables and other bureaucracy
  */
@@ -1529,6 +1594,7 @@ static PyMethodDef liblvm_pv_methods[] = {
     { "getDevSize",          (PyCFunction)liblvm_lvm_pv_get_dev_size, METH_NOARGS },
     { "getFree",          (PyCFunction)liblvm_lvm_pv_get_free, METH_NOARGS },
     { "resize",          (PyCFunction)liblvm_lvm_pv_resize, METH_VARARGS },
+    { "listPVsegs",     (PyCFunction)liblvm_lvm_lv_list_pvsegs, METH_NOARGS },
     { NULL,             NULL}   /* sentinel */
 };
 
@@ -1536,6 +1602,10 @@ static PyMethodDef liblvm_lvseg_methods[] = {
     { "getProperty",        (PyCFunction)liblvm_lvm_lvseg_get_property, METH_VARARGS }
 };
 
+static PyMethodDef liblvm_pvseg_methods[] = {
+    { "getProperty",        (PyCFunction)liblvm_lvm_pvseg_get_property, METH_VARARGS }
+};
+
 static PyTypeObject LiblvmType = {
     PyObject_HEAD_INIT(&PyType_Type)
     .tp_name = "liblvm.Liblvm",
@@ -1592,6 +1662,17 @@ static PyTypeObject LibLVMlvsegType = {
     .tp_methods = liblvm_lvseg_methods,
 };
 
+static PyTypeObject LibLVMpvsegType = {
+    PyObject_HEAD_INIT(&PyType_Type)
+    .tp_name = "liblvm.Liblvm_pvseg",
+    .tp_basicsize = sizeof(pvsegobject),
+    .tp_new = PyType_GenericNew,
+    .tp_dealloc = (destructor)liblvm_pvseg_dealloc,
+    .tp_flags = Py_TPFLAGS_DEFAULT,
+    .tp_doc = "LVM Physical Volume Segment object",
+    .tp_methods = liblvm_pvseg_methods,
+};
+
 PyMODINIT_FUNC
 initlvm(void)
 {
@@ -1607,6 +1688,8 @@ initlvm(void)
         return;
     if (PyType_Ready(&LibLVMlvsegType) < 0)
         return;
+    if (PyType_Ready(&LibLVMpvsegType) < 0)
+        return;
 
     m = Py_InitModule3("lvm", Liblvm_methods, "Liblvm module");
     if (m == NULL)
Signature · 1 line
1.7.11.4