Skip to contentSkip to conversation

liblvm patch series for review

PATCH New Tony Asleson 1 participant 14 messages Nov 14, 2013

lore
Share & tools
lore
#0 lore raw

cover liblvm patch series for review

This patch contains a number of library updates for review. Main points of interest include:

* Name corrections for python bindings, so that the names actually match how you reference class name spaces in python.

* Add checks so if a user recycles the library handle any left over references to old libraries are caught.

* Re-factor vg reduce to have one code path, addresses:
https://bugzilla.redhat.com/show_bug.cgi?id=989174

* Add ability to create PV with arguments to configure:
- size
- pvmetadatacopies
- pvmetadatasize
- data_alignment
- data_alignment_offset
- zero

* Update unit tests and _enable_ them.

* Add VG/LV name validation function ref. bug:
https://bugzilla.redhat.com/show_bug.cgi?id=883689

* Reset buffer after retrieving error message in library.

* Add LVM2APP_DEBUG env variable for debug messages to stderr.

* Remove the forward struct declaration. This isn't needed for implementing opaque data pointers.

Tony Asleson (13):
python-lvm: Correct names
python-lvm: Robustness improvements.
vgreduce: Move _vgreduce_single functionality metadata.c: Call refactored vgreduce_single
python-lvm: Test case change for vg.reduce
lvm2app: Add ability to create PV with args
python-lvm: Add addl. PV create arguments
python-lvm: Update and enable unit test case
lvm2app: Add VG/LV name validation
python-lvm: VG/LV name validation.
lvm2app: Reset buffer after retrieving error message
lvm2app: Add LVM2APP_DEBUG env variable for debug
lvm2app: Remove forward declarations.

 lib/log/log.c                    |  18 +++
 lib/log/log.h                    |   2 +
 lib/log/lvm-logging.h            |   1 +
 lib/metadata/metadata-exported.h |   5 +
 lib/metadata/metadata.c          |  67 ++++----
 lib/metadata/vg.c                |  93 +++++++++++
 lib/misc/lvm-string.c            |  68 ++++++--
 lib/misc/lvm-string.h            |   1 +
 liblvm/lvm2app.h                 | 117 ++++++++++++--
 liblvm/lvm_base.c                |  24 ++-
 liblvm/lvm_lv.c                  |   8 +-
 liblvm/lvm_misc.c                |  10 ++
 liblvm/lvm_misc.h                |   9 +-
 liblvm/lvm_prop.c                |  31 ++++
 liblvm/lvm_prop.h                |  14 ++
 liblvm/lvm_prop_fields.h         |   7 +
 liblvm/lvm_pv.c                  | 136 ++++++++++++++--
 liblvm/lvm_vg.c                  |  25 ++-
 python/liblvm.c                  | 329 +++++++++++++++++++++++----------------
 test/api/pytest.sh               |   4 -
 test/api/python_lvm_unit.py      | 101 +++++++++++-
 tools/toollib.c                  |   6 -
 tools/vgreduce.c                 |  81 +---------
 23 files changed, 855 insertions(+), 302 deletions(-)
Signature · 1 line
1.8.2.1
#1 lore raw

PATCH 1/13 python-lvm: Correct names

The existing names do not follow accepted guidelines.

---
 python/liblvm.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
python/liblvm.c +7 -7
index a9de817..42a327e 100644
@@ -1846,7 +1846,7 @@ static PyMethodDef _liblvm_pvseg_methods[] = {
 
 static PyTypeObject _LibLVMvgType = {
 	PyObject_HEAD_INIT(&PyType_Type)
-	.tp_name = "liblvm.Liblvm_vg",
+	.tp_name = "lvm.Liblvm_vg",
 	.tp_basicsize = sizeof(vgobject),
 	.tp_new = PyType_GenericNew,
 	.tp_dealloc = (destructor)liblvm_vg_dealloc,
@@ -1857,7 +1857,7 @@ static PyTypeObject _LibLVMvgType = {
 
 static PyTypeObject _LibLVMlvType = {
 	PyObject_HEAD_INIT(&PyType_Type)
-	.tp_name = "liblvm.Liblvm_lv",
+	.tp_name = "lvm.Liblvm_lv",
 	.tp_basicsize = sizeof(lvobject),
 	.tp_new = PyType_GenericNew,
 	.tp_dealloc = (destructor)liblvm_lv_dealloc,
@@ -1868,7 +1868,7 @@ static PyTypeObject _LibLVMlvType = {
 
 static PyTypeObject _LibLVMpvlistType = {
 	PyObject_HEAD_INIT(&PyType_Type)
-	.tp_name = "liblvm.Liblvm_pvlist",
+	.tp_name = "lvm.Liblvm_pvlist",
 	.tp_basicsize = sizeof(pvslistobject),
 	.tp_new = PyType_GenericNew,
 	.tp_dealloc = (destructor)_liblvm_pvlist_dealloc,
@@ -1879,7 +1879,7 @@ static PyTypeObject _LibLVMpvlistType = {
 
 static PyTypeObject _LibLVMpvType = {
 	PyObject_HEAD_INIT(&PyType_Type)
-	.tp_name = "liblvm.Liblvm_pv",
+	.tp_name = "lvm.Liblvm_pv",
 	.tp_basicsize = sizeof(pvobject),
 	.tp_new = PyType_GenericNew,
 	.tp_dealloc = (destructor)_liblvm_pv_dealloc,
@@ -1890,7 +1890,7 @@ static PyTypeObject _LibLVMpvType = {
 
 static PyTypeObject _LibLVMlvsegType = {
 	PyObject_HEAD_INIT(&PyType_Type)
-	.tp_name = "liblvm.Liblvm_lvseg",
+	.tp_name = "lvm.Liblvm_lvseg",
 	.tp_basicsize = sizeof(lvsegobject),
 	.tp_new = PyType_GenericNew,
 	.tp_dealloc = (destructor)_liblvm_lvseg_dealloc,
@@ -1901,7 +1901,7 @@ static PyTypeObject _LibLVMlvsegType = {
 
 static PyTypeObject _LibLVMpvsegType = {
 	PyObject_HEAD_INIT(&PyType_Type)
-	.tp_name = "liblvm.Liblvm_pvseg",
+	.tp_name = "lvm.Liblvm_pvseg",
 	.tp_basicsize = sizeof(pvsegobject),
 	.tp_new = PyType_GenericNew,
 	.tp_dealloc = (destructor)_liblvm_pvseg_dealloc,
@@ -1952,7 +1952,7 @@ PyMODINIT_FUNC initlvm(void)
 				    LVM_THIN_DISCARDS_PASSDOWN) < 0)
 		return;
 
-	if ((_LibLVMError = PyErr_NewException("Liblvm._LibLVMError", NULL, NULL))) {
+	if ((_LibLVMError = PyErr_NewException("lvm.LibLVMError", NULL, NULL))) {
 		/* Each call to PyModule_AddObject decrefs it; compensate: */
 		Py_INCREF(_LibLVMError);
 		Py_INCREF(_LibLVMError);
Signature · 1 line
1.8.2.1
#2 lore raw

PATCH 2/13 python-lvm: Robustness improvements.

In reply to Tony Asleson

Add checks so if a user recycles the library handle any left over references to old libraries are caught.

---
 python/liblvm.c | 219 ++++++++++++++++++++++++++------------------------------
 1 file changed, 103 insertions(+), 116 deletions(-)
python/liblvm.c +103 -116
index 42a327e..36b64e4 100644
@@ -30,11 +30,13 @@ static lvm_t _libh;
 typedef struct {
 	PyObject_HEAD
 	vg_t vg;		/* vg handle */
+	lvm_t libh_copy;
 } vgobject;
 
 typedef struct {
 	PyObject_HEAD
 	struct dm_list *pvslist;
+	lvm_t libh_copy;
 } pvslistobject;
 
 typedef struct {
@@ -71,9 +73,14 @@ static PyTypeObject _LibLVMpvsegType;
 
 static PyObject *_LibLVMError;
 
-#define LVM_VALID() \
+#define LVM_VALID(ptr) \
 	do { \
-		if (!_libh) { \
+		if (ptr && _libh) { \
+			if (ptr != _libh) { \
+				PyErr_SetString(PyExc_UnboundLocalError, "LVM handle reference stale"); \
+				return NULL; \
+			} \
+		} else if (!_libh) { \
 			PyErr_SetString(PyExc_UnboundLocalError, "LVM handle invalid"); \
 			return NULL; \
 		} \
@@ -99,17 +106,42 @@ static vgobject *_create_py_vg(void)
 {
 	vgobject *vgobj = PyObject_New(vgobject, &_LibLVMvgType);
 
-	if (vgobj)
+	if (vgobj) {
 		vgobj->vg = NULL;
+		vgobj->libh_copy = _libh;
+	}
 
 	return vgobj;
 }
 
+static pvslistobject *_create_py_pvlist(void)
+{
+	pvslistobject *pvlistobj = PyObject_New(pvslistobject, &_LibLVMpvlistType);
+
+	if (pvlistobj) {
+		pvlistobj->pvslist = NULL;
+		pvlistobj->libh_copy = _libh;
+	}
+
+	return pvlistobj;
+}
+
+static lvobject *_create_py_lv(vgobject *parent, lv_t lv)
+{
+	lvobject * lvobj = PyObject_New(lvobject, &_LibLVMlvType);
+	if (lvobj) {
+		lvobj->parent_vgobj = parent;
+		Py_INCREF(lvobj->parent_vgobj);
+		lvobj->lv = lv;
+	}
+	return lvobj;
+}
+
 static PyObject *_liblvm_get_last_error(void)
 {
 	PyObject *info;
 
-	LVM_VALID();
+	LVM_VALID(NULL);
 
 	if (!(info = PyTuple_New(2)))
 		return NULL;
@@ -122,7 +154,7 @@ static PyObject *_liblvm_get_last_error(void)
 
 static PyObject *_liblvm_library_get_version(void)
 {
-	LVM_VALID();
+	LVM_VALID(NULL);
 
 	return Py_BuildValue("s", lvm_library_get_version());
 }
@@ -131,7 +163,7 @@ static const char _gc_doc[] = "Garbage collect the C library";
 
 static PyObject *_liblvm_lvm_gc(void)
 {
-	LVM_VALID();
+	LVM_VALID(NULL);
 
 	lvm_quit(_libh);
 
@@ -152,7 +184,7 @@ static PyObject *_liblvm_lvm_list_vg_names(void)
 	PyObject * pytuple;
 	int i = 0;
 
-	LVM_VALID();
+	LVM_VALID(NULL);
 
 	if (!(vgnames = lvm_list_vg_names(_libh))) {
 		PyErr_SetObject(_LibLVMError, _liblvm_get_last_error());
@@ -177,7 +209,7 @@ static PyObject *_liblvm_lvm_list_vg_uuids(void)
 	PyObject * pytuple;
 	int i = 0;
 
-	LVM_VALID();
+	LVM_VALID(NULL);
 
 	if (!(uuids = lvm_list_vg_uuids(_libh))) {
 		PyErr_SetObject(_LibLVMError, _liblvm_get_last_error());
@@ -261,23 +293,16 @@ static PyObject *_liblvm_pvlist_dealloc(pvslistobject *self)
 
 static PyObject *_liblvm_lvm_list_pvs(void)
 {
-	pvslistobject *pvslistobj;
-
-	LVM_VALID();
-
-	if (!(pvslistobj = PyObject_New(pvslistobject, &_LibLVMpvlistType)))
-		return NULL;
+	LVM_VALID(NULL);
 
-	pvslistobj->pvslist = NULL;
-
-	return (PyObject *)pvslistobj;
+	return (PyObject *)_create_py_pvlist();
 }
 
 static PyObject *_liblvm_lvm_pv_remove(PyObject *self, PyObject *arg)
 {
 	const char *pv_name;
 
-	LVM_VALID();
+	LVM_VALID(NULL);
 
 	if (!PyArg_ParseTuple(arg, "s", &pv_name))
 		return NULL;
@@ -297,7 +322,7 @@ static PyObject *_liblvm_lvm_pv_create(PyObject *self, PyObject *arg)
 	const char *pv_name;
 	unsigned long long size;
 
-	LVM_VALID();
+	LVM_VALID(NULL);
 
 	if (!PyArg_ParseTuple(arg, "sK", &pv_name, &size))
 		return NULL;
@@ -317,7 +342,7 @@ static PyObject *_liblvm_lvm_percent_to_float(PyObject *self, PyObject *arg)
 	double converted;
 	int percent;
 
-	LVM_VALID();
+	LVM_VALID(NULL);
 
 	if (!PyArg_ParseTuple(arg, "i", &percent))
 		return NULL;
@@ -332,7 +357,7 @@ static PyObject *_liblvm_lvm_vgname_from_pvid(PyObject *self, PyObject *arg)
 	const char *pvid;
 	const char *vgname;
 
-	LVM_VALID();
+	LVM_VALID(NULL);
 
 	if (!PyArg_ParseTuple(arg, "s", &pvid))
 		return NULL;
@@ -350,7 +375,7 @@ static PyObject *_liblvm_lvm_vgname_from_device(PyObject *self, PyObject *arg)
 	const char *device;
 	const char *vgname;
 
-	LVM_VALID();
+	LVM_VALID(NULL);
 
 	if (!PyArg_ParseTuple(arg, "s", &device))
 		return NULL;
@@ -370,7 +395,7 @@ static PyObject *_liblvm_lvm_config_find_bool(PyObject *self, PyObject *arg)
 	int rval;
 	PyObject *rc;
 
-	LVM_VALID();
+	LVM_VALID(NULL);
 
 	if (!PyArg_ParseTuple(arg, "s", &config))
 		return NULL;
@@ -390,7 +415,7 @@ static PyObject *_liblvm_lvm_config_find_bool(PyObject *self, PyObject *arg)
 
 static PyObject *_liblvm_lvm_config_reload(void)
 {
-	LVM_VALID();
+	LVM_VALID(NULL);
 
 	if (lvm_config_reload(_libh) == -1) {
 		PyErr_SetObject(_LibLVMError, _liblvm_get_last_error());
@@ -405,7 +430,7 @@ static PyObject *_liblvm_lvm_config_reload(void)
 
 static PyObject *_liblvm_lvm_scan(void)
 {
-	LVM_VALID();
+	LVM_VALID(NULL);
 
 	if (lvm_scan(_libh) == -1) {
 		PyErr_SetObject(_LibLVMError, _liblvm_get_last_error());
@@ -421,7 +446,7 @@ static PyObject *_liblvm_lvm_config_override(PyObject *self, PyObject *arg)
 {
 	const char *config;
 
-	LVM_VALID();
+	LVM_VALID(NULL);
 
 	if (!PyArg_ParseTuple(arg, "s", &config))
 		return NULL;
@@ -447,7 +472,7 @@ static PyObject *_liblvm_lvm_vg_open(PyObject *self, PyObject *args)
 
 	vgobject *vgobj;
 
-	LVM_VALID();
+	LVM_VALID(NULL);
 
 	if (!PyArg_ParseTuple(args, "s|s", &vgname, &mode))
 		return NULL;
@@ -472,7 +497,7 @@ static PyObject *_liblvm_lvm_vg_create(PyObject *self, PyObject *args)
 	const char *vgname;
 	vgobject *vgobj;
 
-	LVM_VALID();
+	LVM_VALID(NULL);
 
 	if (!PyArg_ParseTuple(args, "s", &vgname))
 		return NULL;
@@ -495,6 +520,7 @@ static void liblvm_vg_dealloc(vgobject *self)
 	if (self->vg != NULL) {
 		lvm_vg_close(self->vg);
 		self->vg = NULL;
+		self->libh_copy = NULL;
 	}
 
 	PyObject_Del(self);
@@ -504,20 +530,20 @@ static void liblvm_vg_dealloc(vgobject *self)
 
 #define VG_VALID(vgobject) \
 	do { \
-		LVM_VALID(); \
-		if (!vgobject->vg) { \
+		if (!vgobject || !vgobject->vg) { \
 			PyErr_SetString(PyExc_UnboundLocalError, "VG object invalid"); \
 			return NULL; \
 		} \
+		LVM_VALID(vgobject->libh_copy); \
 	} while (0)
 
 #define PVSLIST_VALID(pvslistobject) \
 	do { \
-		LVM_VALID(); \
-		if (!pvslistobject->pvslist) { \
+		if (!pvslistobject || !pvslistobject->pvslist) { \
 			PyErr_SetString(PyExc_UnboundLocalError, "PVS object invalid"); \
 			return NULL; \
 		} \
+		LVM_VALID(pvslistobject->libh_copy); \
 	} while (0)
 
 static PyObject *_liblvm_lvm_vg_close(vgobject *self)
@@ -526,6 +552,7 @@ static PyObject *_liblvm_lvm_vg_close(vgobject *self)
 	if (self->vg) {
 		lvm_vg_close(self->vg);
 		self->vg = NULL;
+		self->libh_copy = NULL;
 	}
 
 	Py_INCREF(Py_None);
@@ -941,15 +968,11 @@ static PyObject *_liblvm_lvm_vg_list_lvs(vgobject *self)
 
 	dm_list_iterate_items(lvl, lvs) {
 		/* Create and initialize the object */
-		if (!(lvobj = PyObject_New(lvobject, &_LibLVMlvType))) {
+		if (!(lvobj = _create_py_lv(self, lvl->lv))) {
 			Py_DECREF(pytuple);
 			return NULL;
 		}
 
-		lvobj->parent_vgobj = self;
-		Py_INCREF(lvobj->parent_vgobj);
-
-		lvobj->lv = lvl->lv;
 		PyTuple_SET_ITEM(pytuple, i, (PyObject *) lvobj);
 		i++;
 	}
@@ -986,29 +1009,19 @@ static PyObject *_liblvm_lvm_vg_create_lv_linear(vgobject *self, PyObject *args)
 {
 	const char *vgname;
 	unsigned long long size;
-	lvobject *lvobj;
+	lv_t lv;
 
 	VG_VALID(self);
 
 	if (!PyArg_ParseTuple(args, "sK", &vgname, &size))
 		return NULL;
 
-	if (!(lvobj = PyObject_New(lvobject, &_LibLVMlvType)))
-		return NULL;
-
-	/* Initialize the parent ptr in case lv create fails and we dealloc lvobj */
-	lvobj->parent_vgobj = NULL;
-
-	if (!(lvobj->lv = lvm_vg_create_lv_linear(self->vg, vgname, size))) {
+	if (!(lv = lvm_vg_create_lv_linear(self->vg, vgname, size))) {
 		PyErr_SetObject(_LibLVMError, _liblvm_get_last_error());
-		Py_DECREF(lvobj);
 		return NULL;
 	}
 
-	lvobj->parent_vgobj = self;
-	Py_INCREF(lvobj->parent_vgobj);
-
-	return (PyObject *)lvobj;
+	return (PyObject *)_create_py_lv(self, lv);
 }
 
 static PyObject *_liblvm_lvm_vg_create_lv_thinpool(vgobject *self, PyObject *args)
@@ -1019,7 +1032,7 @@ static PyObject *_liblvm_lvm_vg_create_lv_thinpool(vgobject *self, PyObject *arg
 	unsigned long chunk_size = 0;
 	int skip_zero = 0;
 	lvm_thin_discards_t discard = LVM_THIN_DISCARDS_PASSDOWN;
-	lvobject *lvobj;
+	lv_t lv;
 	lv_create_params_t lvp = NULL;
 	struct lvm_property_value prop_value;
 
@@ -1029,17 +1042,10 @@ static PyObject *_liblvm_lvm_vg_create_lv_thinpool(vgobject *self, PyObject *arg
 			      &meta_size, &discard, &skip_zero))
 		return NULL;
 
-	if (!(lvobj = PyObject_New(lvobject, &_LibLVMlvType)))
-		return NULL;
-
-	/* Initialize the parent ptr in case lv create fails and we dealloc lvobj */
-	lvobj->parent_vgobj = NULL;
-
 	if (!(lvp = lvm_lv_params_create_thin_pool(self->vg, pool_name, size, chunk_size,
 						   meta_size, discard))) {
 
 		PyErr_SetObject(_LibLVMError, _liblvm_get_last_error());
-		Py_DECREF(lvobj);
 		return NULL;
 	}
 
@@ -1051,23 +1057,20 @@ static PyObject *_liblvm_lvm_vg_create_lv_thinpool(vgobject *self, PyObject *arg
 
 			if (lvm_lv_params_set_property(lvp, "skip_zero",
 						       &prop_value) == -1) {
-				PyErr_SetObject(_LibLVMError, _liblvm_get_last_error());
-				Py_DECREF(lvobj);
-				return NULL;
+				goto error;
 			}
 		}
 	}
 
-	if (!(lvobj->lv = lvm_lv_create(lvp))) {
-		PyErr_SetObject(_LibLVMError, _liblvm_get_last_error());
-		Py_DECREF(lvobj);
-		return NULL;
+	if (!(lv = lvm_lv_create(lvp))) {
+		goto error;
 	}
 
-	lvobj->parent_vgobj = self;
-	Py_INCREF(lvobj->parent_vgobj);
+	return (PyObject *)_create_py_lv(self, lv);
 
-	return (PyObject *)lvobj;
+error:
+	PyErr_SetObject(_LibLVMError, _liblvm_get_last_error());
+	return NULL;
 }
 
 static PyObject *_liblvm_lvm_vg_create_lv_thin(vgobject *self, PyObject *args)
@@ -1075,7 +1078,7 @@ static PyObject *_liblvm_lvm_vg_create_lv_thin(vgobject *self, PyObject *args)
 	const char *pool_name;
 	const char *lv_name;
 	unsigned long long size = 0;
-	lvobject *lvobj;
+	lv_t lv;
 	lv_create_params_t lvp = NULL;
 
 	VG_VALID(self);
@@ -1083,28 +1086,17 @@ static PyObject *_liblvm_lvm_vg_create_lv_thin(vgobject *self, PyObject *args)
 	if (!PyArg_ParseTuple(args, "ssK", &pool_name, &lv_name, &size))
 		return NULL;
 
-	if (!(lvobj = PyObject_New(lvobject, &_LibLVMlvType)))
-		return NULL;
-
-	/* Initialize the parent ptr in case lv create fails and we dealloc lvobj */
-	lvobj->parent_vgobj = NULL;
-
 	if (!(lvp = lvm_lv_params_create_thin(self->vg, pool_name, lv_name,size))) {
 		PyErr_SetObject(_LibLVMError, _liblvm_get_last_error());
-		Py_DECREF(lvobj);
 		return NULL;
 	}
 
-	if (!(lvobj->lv = lvm_lv_create(lvp))) {
+	if (!(lv = lvm_lv_create(lvp))) {
 		PyErr_SetObject(_LibLVMError, _liblvm_get_last_error());
-		Py_DECREF(lvobj);
 		return NULL;
 	}
 
-	lvobj->parent_vgobj = self;
-	Py_INCREF(lvobj->parent_vgobj);
-
-	return (PyObject *)lvobj;
+	return (PyObject *)_create_py_lv(self, lv);
 }
 
 static void liblvm_lv_dealloc(lvobject *self)
@@ -1157,7 +1149,6 @@ 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)
 {
 	const char *id;
-	lvobject *lvobj;
 	lv_t lv = NULL;
 
 	VG_VALID(self);
@@ -1170,15 +1161,7 @@ static PyObject *_liblvm_lvm_lv_from_N(vgobject *self, PyObject *arg, lv_fetch_b
 		return NULL;
 	}
 
-	if (!(lvobj = PyObject_New(lvobject, &_LibLVMlvType)))
-		return NULL;
-
-	lvobj->parent_vgobj = self;
-	Py_INCREF(lvobj->parent_vgobj);
-
-	lvobj->lv = lv;
-
-	return (PyObject *)lvobj;
+	return (PyObject *)_create_py_lv(self, lv);
 }
 
 static PyObject *_liblvm_lvm_lv_from_name(vgobject *self, PyObject *arg)
@@ -1242,12 +1225,12 @@ static void _liblvm_pv_dealloc(pvobject *self)
 /* LV Methods */
 
 #define LV_VALID(lvobject) \
-	do { \
-		VG_VALID(lvobject->parent_vgobj); \
-		if (!lvobject->lv) { \
+	do {					\
+		if (!lvobject || !lvobject->lv) { 	\
 			PyErr_SetString(PyExc_UnboundLocalError, "LV object invalid"); \
-			return NULL; \
-		} \
+			return NULL;	\
+		}\
+		VG_VALID(lvobject->parent_vgobj); \
 	} while (0)
 
 static PyObject *_liblvm_lvm_lv_get_attr(lvobject *self)
@@ -1510,7 +1493,7 @@ static PyObject *_liblvm_lvm_lv_snapshot(lvobject *self, PyObject *args)
 {
 	const char *snap_name;
 	unsigned long long size = 0;
-	lvobject *lvobj;
+	lv_t lv;
 	lv_create_params_t lvp = NULL;
 
 	LV_VALID(self);
@@ -1518,43 +1501,33 @@ static PyObject *_liblvm_lvm_lv_snapshot(lvobject *self, PyObject *args)
 	if (!PyArg_ParseTuple(args, "s|K", &snap_name, &size))
 		return NULL;
 
-	if (!(lvobj = PyObject_New(lvobject, &_LibLVMlvType)))
-		return NULL;
-
-	lvobj->parent_vgobj = NULL;
-
 	if (!(lvp = lvm_lv_params_create_snapshot(self->lv, snap_name, size))) {
 		PyErr_SetObject(_LibLVMError, _liblvm_get_last_error());
-		Py_DECREF(lvobj);
 		return NULL;
 	}
 
-	if (!(lvobj->lv = lvm_lv_create(lvp))) {
+	if (!(lv = lvm_lv_create(lvp))) {
 		PyErr_SetObject(_LibLVMError, _liblvm_get_last_error());
-		Py_DECREF(lvobj);
 		return NULL;
 	}
 
-	lvobj->parent_vgobj = self->parent_vgobj;
-	Py_INCREF(lvobj->parent_vgobj);
-
-	return (PyObject *)lvobj;
+	return (PyObject *)_create_py_lv(self->parent_vgobj, lv);
 }
 
 /* PV Methods */
 
 #define PV_VALID(pvobject) \
 	do { \
+		if (!pvobject || !pvobject->pv) { \
+			PyErr_SetString(PyExc_UnboundLocalError, "PV object invalid"); \
+			return NULL; \
+		} \
 		if (pvobject->parent_vgobj) { \
 			VG_VALID(pvobject->parent_vgobj); \
 		} \
 		if (pvobject->parent_pvslistobj) { \
 			PVSLIST_VALID(pvobject->parent_pvslistobj); \
 		} \
-		if (!pvobject->pv) { \
-			PyErr_SetString(PyExc_UnboundLocalError, "PV object invalid"); \
-			return NULL; \
-		} \
 	} while (0)
 
 static PyObject *_liblvm_lvm_pv_get_name(pvobject *self)
@@ -1673,7 +1646,14 @@ static PyObject *_liblvm_lvm_pv_list_pvsegs(pvobject *self)
  * No way to close/destroy an lvseg, just need to make sure parents are
  * still good
  */
-#define LVSEG_VALID(lvsegobject) LV_VALID(lvsegobject->parent_lvobj)
+#define LVSEG_VALID(lvsegobject) \
+	do { \
+		if ( !lvsegobject || !lvsegobject->parent_lvobj ) { \
+			PyErr_SetString(PyExc_UnboundLocalError, "LV segment object invalid"); \
+			return NULL; \
+		} \
+		LV_VALID(lvsegobject->parent_lvobj); \
+	} while(0)
 
 static void _liblvm_lvseg_dealloc(lvsegobject *self)
 {
@@ -1702,7 +1682,14 @@ static PyObject *_liblvm_lvm_lvseg_get_property(lvsegobject *self, PyObject *arg
  * No way to close/destroy a pvseg, just need to make sure parents are
  * still good
  */
-#define PVSEG_VALID(pvsegobject) PV_VALID(pvsegobject->parent_pvobj)
+#define PVSEG_VALID(pvsegobject) \
+	do { \
+		if (!pvsegobject || !pvsegobject->parent_pvobj) { \
+			PyErr_SetString(PyExc_UnboundLocalError, "PV segment object invalid"); \
+			return NULL; \
+		} \
+		PV_VALID(pvsegobject->parent_pvobj); \
+	} while(0)
 
 static void _liblvm_pvseg_dealloc(pvsegobject *self)
 {
Signature · 1 line
1.8.2.1
#3 lore raw

PATCH 3/13 vgreduce: Move _vgreduce_single functionality

In reply to Tony Asleson

Moving the core functionality of vgreduce single into lib/metadata/vg.c so that the command line and lvm2app library can call the same core functionality. New function is vgreduce_single.

---
 lib/metadata/metadata-exported.h |  4 ++
 lib/metadata/vg.c                | 87 ++++++++++++++++++++++++++++++++++++++++
 tools/vgreduce.c                 | 81 ++-----------------------------------
 3 files changed, 95 insertions(+), 77 deletions(-)
lib/metadata/metadata-exported.h +4 -0
index c00e4e5..4b193c2 100644
@@ -595,6 +595,10 @@ int vg_rename(struct cmd_context *cmd, struct volume_group *vg,
 int vg_extend(struct volume_group *vg, int pv_count, const char *const *pv_names,
 	      struct pvcreate_params *pp);
 int vg_reduce(struct volume_group *vg, const char *pv_name);
+
+int vgreduce_single(struct cmd_context *cmd, struct volume_group *vg,
+			    struct physical_volume *pv);
+
 int vg_change_tag(struct volume_group *vg, const char *tag, int add_tag);
 int vg_split_mdas(struct cmd_context *cmd, struct volume_group *vg_from,
 		  struct volume_group *vg_to);
lib/metadata/vg.c +87 -0

96 lines not on this page · raw message

tools/vgreduce.c +4 -77

92 lines not on this page · raw message

Signature · 1 line
1.8.2.1
#4 lore raw

PATCH 4/13 metadata.c: Call refactored vgreduce_single

In reply to Tony Asleson

Replace the code with the refactored vgreduce_single instead of calling its own implementation.

Corrects bug: https://bugzilla.redhat.com/show_bug.cgi?id=989174

---
 lib/metadata/metadata-exported.h |  2 +-
 lib/metadata/metadata.c          | 35 ++++++-----------------------------
 lib/metadata/vg.c                | 36 +++++++++++++++++++++---------------
 tools/vgreduce.c                 |  2 +-
 4 files changed, 29 insertions(+), 46 deletions(-)
lib/metadata/metadata-exported.h +1 -1
index 4b193c2..6d056a9 100644
@@ -597,7 +597,7 @@ int vg_extend(struct volume_group *vg, int pv_count, const char *const *pv_names
 int vg_reduce(struct volume_group *vg, const char *pv_name);
 
 int vgreduce_single(struct cmd_context *cmd, struct volume_group *vg,
-			    struct physical_volume *pv);
+			    struct physical_volume *pv, int commit);
 
 int vg_change_tag(struct volume_group *vg, const char *tag, int add_tag);
 int vg_split_mdas(struct cmd_context *cmd, struct volume_group *vg_from,
lib/metadata/metadata.c +6 -29
index 8571e0a..79b2668 100644
@@ -685,50 +685,27 @@ int vg_extend(struct volume_group *vg, int pv_count, const char *const *pv_names
 	return 1;
 }
 
-/* FIXME: use this inside vgreduce_single? */
 int vg_reduce(struct volume_group *vg, const char *pv_name)
 {
 	struct physical_volume *pv;
 	struct pv_list *pvl;
 
-	if (_vg_bad_status_bits(vg, RESIZEABLE_VG))
-		return 0;
-
-	if (!archive(vg))
-		goto bad;
-
-	/* remove each pv */
 	if (!(pvl = find_pv_in_vg(vg, pv_name))) {
 		log_error("Physical volume %s not in volume group %s.",
 			  pv_name, vg->name);
-		goto bad;
+		return 0;
 	}
 
 	pv = pvl->pv;
 
-	if (pv_pe_alloc_count(pv)) {
-		log_error("Physical volume %s still in use.",
-			  pv_name);
-		goto bad;
-	}
-
-	if (!dev_get_size(pv_dev(pv), &pv->size)) {
-		log_error("%s: Couldn't get size.", pv_name);
-		goto bad;
+	if (vgreduce_single(vg->cmd, vg, pv, 0)) {
+		dm_list_add(&vg->removed_pvs, &pvl->list);
+		return 1;
 	}
 
-	vg->free_count -= pv_pe_count(pv) - pv_pe_alloc_count(pv);
-	vg->extent_count -= pv_pe_count(pv);
-	del_pvl_from_vgs(vg, pvl);
-
-	/* add pv to the remove_pvs list */
-	dm_list_add(&vg->removed_pvs, &pvl->list);
-
-	return 1;
-
-      bad:
 	log_error("Unable to remove physical volume '%s' from "
-		  "volume group '%s'.", pv_name, vg->name);
+				"volume group '%s'.", pv_name, vg->name);
+
 	return 0;
 }
 
lib/metadata/vg.c +21 -15
index 75fc0c1..0de0847 100644
@@ -576,7 +576,7 @@ char *vg_attr_dup(struct dm_pool *mem, const struct volume_group *vg)
 }
 
 int vgreduce_single(struct cmd_context *cmd, struct volume_group *vg,
-			    struct physical_volume *pv)
+			    struct physical_volume *pv, int commit)
 {
 	struct pv_list *pvl;
 	struct volume_group *orphan_vg = NULL;
@@ -637,25 +637,31 @@ int vgreduce_single(struct cmd_context *cmd, struct volume_group *vg,
 		goto bad;
 	}
 
-	if (!vg_write(vg) || !vg_commit(vg)) {
-		log_error("Removal of physical volume \"%s\" from "
-			  "\"%s\" failed", name, vg->name);
-		goto bad;
-	}
+	/*
+	 * Only write out the needed changes if so requested by caller.
+	 */
+	if (commit) {
+		if (!vg_write(vg) || !vg_commit(vg)) {
+			log_error("Removal of physical volume \"%s\" from "
+				  "\"%s\" failed", name, vg->name);
+			goto bad;
+		}
 
-	if (!pv_write(cmd, pv, 0)) {
-		log_error("Failed to clear metadata from physical "
-			  "volume \"%s\" "
-			  "after removal from \"%s\"", name, vg->name);
-		goto bad;
-	}
+		if (!pv_write(cmd, pv, 0)) {
+			log_error("Failed to clear metadata from physical "
+				  "volume \"%s\" "
+				  "after removal from \"%s\"", name, vg->name);
+			goto bad;
+		}
 
-	backup(vg);
+		backup(vg);
 
-	log_print_unless_silent("Removed \"%s\" from volume group \"%s\"", name, vg->name);
+		log_print_unless_silent("Removed \"%s\" from volume group \"%s\"",
+				name, vg->name);
+	}
 	r = 1;
 bad:
-	if (pvl)
+	if (pvl && commit)
 		free_pv_fid(pvl->pv);
 	unlock_and_release_vg(cmd, orphan_vg, VG_ORPHANS);
 	return r;
tools/vgreduce.c +1 -1
index 534a4fb..0ade4fa 100644
@@ -125,7 +125,7 @@ static int _vgreduce_single(struct cmd_context *cmd, struct volume_group *vg,
 			    struct physical_volume *pv,
 			    void *handle __attribute__((unused)))
 {
-	int r = vgreduce_single(cmd, vg, pv);
+	int r = vgreduce_single(cmd, vg, pv, 1);
 
 	if (!r)
 		return ECMD_FAILED;
Signature · 1 line
1.8.2.1
#5 lore raw

PATCH 5/13 python-lvm: Test case change for vg.reduce

In reply to Tony Asleson

Fix reduce as newly changed vg reduce fails when you try to remove the last pv in the vg.

---
 test/api/python_lvm_unit.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
test/api/python_lvm_unit.py +1 -1
index bceb82c..eab9575 100755
@@ -185,7 +185,7 @@ class TestLvm(unittest.TestCase):
 		for p in pvs:
 			pe_devices.append(p.getName())
 
-		for pv in pe_devices:
+		for pv in pe_devices[:-1]:
 			vg.reduce(pv)
 
 		vg.remove()
Signature · 1 line
1.8.2.1
#6 lore raw

PATCH 6/13 lvm2app: Add ability to create PV with args

In reply to Tony Asleson

Add a PV create which takes a paramters object that has get/set method to configure PV creation.

Current get/set operations include:
- size
- pvmetadatacopies
- pvmetadatasize
- data_alignment
- data_alignment_offset
- zero

Reference: https://bugzilla.redhat.com/show_bug.cgi?id=880395

---
 lib/metadata/metadata.c  |  28 ++++++++++
 liblvm/lvm2app.h         |  64 ++++++++++++++++++++++
 liblvm/lvm_lv.c          |   8 +--
 liblvm/lvm_misc.c        |  10 ++++
 liblvm/lvm_misc.h        |   9 +++-
 liblvm/lvm_prop.c        |  31 +++++++++++
 liblvm/lvm_prop.h        |  14 +++++
 liblvm/lvm_prop_fields.h |   7 +++
 liblvm/lvm_pv.c          | 136 ++++++++++++++++++++++++++++++++++++++++++-----
 liblvm/lvm_vg.c          |   4 +-
 tools/toollib.c          |   6 ---
 11 files changed, 289 insertions(+), 28 deletions(-)
lib/metadata/metadata.c +28 -0
index 79b2668..5bb385d 100644
@@ -1463,6 +1463,30 @@ static int _pvcreate_write(struct cmd_context *cmd, struct pv_to_create *pvc)
 	return 1;
 }
 
+static int _verify_pv_create_params(struct pvcreate_params *pp)
+{
+	/*
+	 * FIXME: Some of these checks are duplicates in pvcreate_params_validate.
+	 */
+	if (pp->pvmetadatacopies > 2) {
+		log_error("Metadatacopies may only be 0, 1 or 2");
+		return 0;
+	}
+
+	if (pp->data_alignment > UINT32_MAX) {
+		log_error("Physical volume data alignment is too big.");
+		return 0;
+	}
+
+	if (pp->data_alignment_offset > UINT32_MAX) {
+		log_error("Physical volume data alignment offset is too big.");
+		return 0;
+	}
+
+	return 1;
+}
+
+
 /*
  * pvcreate_vol() - initialize a device with PV label and metadata area
  *
@@ -1487,6 +1511,10 @@ struct physical_volume *pvcreate_vol(struct cmd_context *cmd, const char *pv_nam
 	if (!pp)
 		pp = &default_pp;
 
+	if (!_verify_pv_create_params(pp)) {
+		goto bad;
+	}
+
 	if (pp->rp.idp) {
 		if ((dev = lvmcache_device_from_pvid(cmd, pp->rp.idp, NULL, NULL)) &&
 		    (dev != dev_cache_get(pv_name, cmd->filter))) {
liblvm/lvm2app.h +64 -0

76 lines not on this page · raw message

liblvm/lvm_lv.c +4 -4
index 46399e4..938a16b 100644
@@ -71,13 +71,13 @@ const char *lvm_lv_get_origin(const lv_t lv)
 
 struct lvm_property_value lvm_lv_get_property(const lv_t lv, const char *name)
 {
-	return get_property(NULL, NULL, lv, NULL, NULL, NULL, name);
+	return get_property(NULL, NULL, lv, NULL, NULL, NULL, NULL, name);
 }
 
 struct lvm_property_value lvm_lvseg_get_property(const lvseg_t lvseg,
 						 const char *name)
 {
-	return get_property(NULL, NULL, NULL, lvseg, NULL, NULL, name);
+	return get_property(NULL, NULL, NULL, lvseg, NULL, NULL, NULL, name);
 }
 
 uint64_t lvm_lv_is_active(const lv_t lv)
@@ -598,7 +598,7 @@ struct lvm_property_value lvm_lv_params_get_property(
 	};
 
 	if (params && params->magic == LV_CREATE_PARAMS_MAGIC) {
-		rc = get_property(NULL, NULL, NULL, NULL, NULL, &params->lvp, name);
+		rc = get_property(NULL, NULL, NULL, NULL, NULL, &params->lvp, NULL, name);
 	} else {
 		log_error("Invalid lv_create_params parameter");
 	}
@@ -612,7 +612,7 @@ int lvm_lv_params_set_property(lv_create_params_t params, const char *name,
 	int rc = -1;
 
 	if (params && params->magic == LV_CREATE_PARAMS_MAGIC) {
-		rc = set_property(NULL, NULL, NULL, &params->lvp, name, prop);
+		rc = set_property(NULL, NULL, NULL, &params->lvp, NULL, name, prop);
 	} else {
 		log_error("Invalid lv_create_params parameter");
 	}
liblvm/lvm_misc.c +10 -0
index c79af8f..ba77010 100644
@@ -51,6 +51,7 @@ struct lvm_property_value get_property(const pv_t pv, const vg_t vg,
 				       const lvseg_t lvseg,
 				       const pvseg_t pvseg,
 				       const struct lvcreate_params *lvcp,
+				       const struct pvcreate_params *pvcp,
 				       const char *name)
 {
 	struct lvm_property_type prop;
@@ -76,6 +77,9 @@ struct lvm_property_value get_property(const pv_t pv, const vg_t vg,
 	} else if (lvcp) {
 		if (!lv_create_param_get_property(lvcp, &prop))
 			return v;
+	} else if (pvcp) {
+		if (!pv_create_param_get_property(pvcp, &prop))
+			return v;
 	} else {
 		log_errno(EINVAL, "Invalid NULL handle passed to library function.");
 		return v;
@@ -95,6 +99,7 @@ struct lvm_property_value get_property(const pv_t pv, const vg_t vg,
 
 int set_property(const pv_t pv, const vg_t vg, const lv_t lv,
 		struct lvcreate_params *lvcp,
+		struct pvcreate_params *pvcp,
 		const char *name,
 		struct lvm_property_value *v)
 {
@@ -125,6 +130,11 @@ int set_property(const pv_t pv, const vg_t vg, const lv_t lv,
 			v->is_valid = 0;
 			return -1;
 		}
+	} else if (pvcp) {
+		if (!pv_create_param_set_property(pvcp, &prop)) {
+			v->is_valid = 0;
+			return -1;
+		}
 	} else {
 		return -1;
 	}
liblvm/lvm_misc.h +7 -2
index a84125a..b187021 100644
@@ -16,14 +16,19 @@
 
 #include "libdevmapper.h"
 #include "lvm2app.h"
+#include "metadata-exported.h"
 
 struct dm_list *tag_list_copy(struct dm_pool *p, struct dm_list *tag_list);
 struct lvm_property_value get_property(const pv_t pv, const vg_t vg,
 				       const lv_t lv, const lvseg_t lvseg,
-				       const pvseg_t pvseg, const struct lvcreate_params *lvcp,
+				       const pvseg_t pvseg,
+				       const struct lvcreate_params *lvcp,
+				       const struct pvcreate_params *pvcp,
 				       const char *name);
 int set_property(const pv_t pv, const vg_t vg, const lv_t lv,
-			struct lvcreate_params *lvcp, const char *name,
+			struct lvcreate_params *lvcp,
+			struct pvcreate_params *pvcp,
+			const char *name,
 			struct lvm_property_value *value);
 
 #endif
liblvm/lvm_prop.c +31 -0
index e25f03b..cbbd6f5 100644
@@ -21,6 +21,25 @@
 GET_LVCREATEPARAMS_NUM_PROPERTY_FN(skip_zero, lvcp->zero)
 SET_LVCREATEPARAMS_NUM_PROPERTY_FN(skip_zero, lvcp->zero)
 
+/* PV create parameters */
+GET_PVCREATEPARAMS_NUM_PROPERTY_FN(size, pvcp->size)
+SET_PVCREATEPARAMS_NUM_PROPERTY_FN(size, pvcp->size)
+
+GET_PVCREATEPARAMS_NUM_PROPERTY_FN(pvmetadatacopies, pvcp->pvmetadatacopies)
+SET_PVCREATEPARAMS_NUM_PROPERTY_FN(pvmetadatacopies, pvcp->pvmetadatacopies)
+
+GET_PVCREATEPARAMS_NUM_PROPERTY_FN(pvmetadatasize, pvcp->pvmetadatasize)
+SET_PVCREATEPARAMS_NUM_PROPERTY_FN(pvmetadatasize, pvcp->pvmetadatasize)
+
+GET_PVCREATEPARAMS_NUM_PROPERTY_FN(data_alignment, pvcp->data_alignment)
+SET_PVCREATEPARAMS_NUM_PROPERTY_FN(data_alignment, pvcp->data_alignment)
+
+GET_PVCREATEPARAMS_NUM_PROPERTY_FN(data_alignment_offset, pvcp->data_alignment_offset)
+SET_PVCREATEPARAMS_NUM_PROPERTY_FN(data_alignment_offset, pvcp->data_alignment_offset)
+
+GET_PVCREATEPARAMS_NUM_PROPERTY_FN(zero, pvcp->zero)
+SET_PVCREATEPARAMS_NUM_PROPERTY_FN(zero, pvcp->zero)
+
 struct lvm_property_type _lib_properties[] = {
 #include "lvm_prop_fields.h"
 	{ 0, "", 0, 0, 0, { .integer = 0 }, prop_not_implemented_get,
@@ -42,3 +61,15 @@ int lv_create_param_set_property(struct lvcreate_params *lvcp,
 {
 	return prop_set_property(_lib_properties, lvcp, prop, LV_CREATE_PARAMS);
 }
+
+int pv_create_param_get_property(const struct pvcreate_params *pvcp,
+		struct lvm_property_type *prop)
+{
+	return prop_get_property(_lib_properties, pvcp, prop, PV_CREATE_PARAMS);
+}
+
+int pv_create_param_set_property(struct pvcreate_params *pvcp,
+		    struct lvm_property_type *prop)
+{
+	return prop_set_property(_lib_properties, pvcp, prop, PV_CREATE_PARAMS);
+}
liblvm/lvm_prop.h +14 -0
index bedc078..db4cf8a 100644
@@ -17,8 +17,10 @@
 #define _LIB_LVM_PROP_H
 
 typedef struct lvcreate_params type_lvcreate_params;
+typedef struct pvcreate_params type_pvcreate_params;
 
 #define LV_CREATE_PARAMS 1
+#define PV_CREATE_PARAMS 2
 
 #define GET_LVCREATEPARAMS_NUM_PROPERTY_FN(NAME, VALUE)\
 	GET_NUM_PROPERTY_FN(NAME, VALUE, lvcreate_params, lvcp)
@@ -26,10 +28,22 @@ typedef struct lvcreate_params type_lvcreate_params;
 #define SET_LVCREATEPARAMS_NUM_PROPERTY_FN(NAME, VALUE) \
 	SET_NUM_PROPERTY(NAME, VALUE, lvcreate_params, lvcp)
 
+#define GET_PVCREATEPARAMS_NUM_PROPERTY_FN(NAME, VALUE)\
+	GET_NUM_PROPERTY_FN(NAME, VALUE, pvcreate_params, pvcp)
+
+#define SET_PVCREATEPARAMS_NUM_PROPERTY_FN(NAME, VALUE) \
+	SET_NUM_PROPERTY(NAME, VALUE, pvcreate_params, pvcp)
+
 int lv_create_param_get_property(const struct lvcreate_params *lvcp,
 		struct lvm_property_type *prop);
 
 int lv_create_param_set_property(struct lvcreate_params *lvcp,
 		    struct lvm_property_type *prop);
 
+int pv_create_param_get_property(const struct pvcreate_params *pvcp,
+		struct lvm_property_type *prop);
+
+int pv_create_param_set_property(struct pvcreate_params *pvcp,
+		    struct lvm_property_type *prop);
+
 #endif
liblvm/lvm_prop_fields.h +7 -0
index 99c322d..ba7dd18 100644
@@ -13,3 +13,10 @@
  */
 
 FIELD(LV_CREATE_PARAMS, lvcreate_params, NUM, "skip_zero", zero, 2, uint32, skip_zero, "Skip zeroing on lv creation", 1)
+
+FIELD(PV_CREATE_PARAMS, pvcreate_params, NUM, "size",      size, 2, uint64_t, size, "PV size", 1)
+FIELD(PV_CREATE_PARAMS, pvcreate_params, NUM, "pvmetadatacopies", pvmetadatacopies, 2, uint64_t, pvmetadatacopies, "PV Metadata copies", 1)
+FIELD(PV_CREATE_PARAMS, pvcreate_params, NUM, "pvmetadatasize", pvmetadatasize, 2, uint64_t, pvmetadatasize, "PV Metadata size", 1)
+FIELD(PV_CREATE_PARAMS, pvcreate_params, NUM, "data_alignment", data_alignment, 2, uint64_t, data_alignment, "Start data to a multiple of value", 1)
+FIELD(PV_CREATE_PARAMS, pvcreate_params, NUM, "data_alignment_offset", data_alignment_offset, 2, uint64_t, data_alignment_offset, "Shift the start of the data area", 1)
+FIELD(PV_CREATE_PARAMS, pvcreate_params, NUM, "zero", zero, 2, uint64_t, zero, "Zero first 2048 bytes of device", 1)
liblvm/lvm_pv.c +122 -14

187 lines not on this page · raw message

liblvm/lvm_vg.c +2 -2
index 3f4968e..214f459 100644
@@ -340,7 +340,7 @@ const char *lvm_vg_get_name(const vg_t vg)
 
 struct lvm_property_value lvm_vg_get_property(const vg_t vg, const char *name)
 {
-	return get_property(NULL, vg, NULL, NULL, NULL, NULL, name);
+	return get_property(NULL, vg, NULL, NULL, NULL, NULL, NULL, name);
 }
 
 int lvm_vg_set_property(const vg_t vg, const char *name,
@@ -357,7 +357,7 @@ int lvm_vg_set_property(const vg_t vg, const char *name,
 				strlen(value->value.string) + 1);
 	}
 
-	return set_property(NULL, vg, NULL, NULL, name, value);
+	return set_property(NULL, vg, NULL, NULL, NULL, name, value);
 }
 
 struct dm_list *lvm_list_vg_names(lvm_t libh)
tools/toollib.c +0 -6
index bc8b33c..14ac3dc 100644
@@ -1542,12 +1542,6 @@ int pvcreate_params_validate(struct cmd_context *cmd,
 		return 0;
 	}
 
-	if (arg_count(cmd, pvmetadatacopies_ARG) &&
-	    arg_int_value(cmd, pvmetadatacopies_ARG, -1) > 2) {
-		log_error("Metadatacopies may only be 0, 1 or 2");
-		return 0;
-	}
-
 	if (arg_count(cmd, metadataignore_ARG))
 		pp->metadataignore = arg_int_value(cmd, metadataignore_ARG,
 						   DEFAULT_PVMETADATAIGNORE);
Signature · 1 line
1.8.2.1
#7 lore raw

PATCH 7/13 python-lvm: Add addl. PV create arguments

In reply to Tony Asleson

Added overloaded PV create arguments with defaults for PV creation.

Addresses bug: https://bugzilla.redhat.com/show_bug.cgi?id=880395

---
 python/liblvm.c | 51 +++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 45 insertions(+), 6 deletions(-)
python/liblvm.c +45 -6
index 36b64e4..033de82 100644
@@ -23,6 +23,7 @@
 
 #include <Python.h>
 #include "lvm2app.h"
+#include "defaults.h"
 
 static lvm_t _libh;
 
@@ -317,24 +318,62 @@ static PyObject *_liblvm_lvm_pv_remove(PyObject *self, PyObject *arg)
 	return Py_None;
 }
 
+static int _set_pv_numeric_prop(pv_create_params_t pv_params, const char *name,
+								unsigned long long value)
+{
+	struct lvm_property_value prop_value;
+	prop_value.is_integer = 1;
+	prop_value.value.integer = value;
+
+	return lvm_pv_params_set_property(pv_params, name, &prop_value);
+}
+
+#define SET_PV_PROP(params, name, value) \
+	do { \
+		if (_set_pv_numeric_prop(params, name, value) == -1) \
+			goto error; \
+	} while(0)\
+
 static PyObject *_liblvm_lvm_pv_create(PyObject *self, PyObject *arg)
 {
 	const char *pv_name;
-	unsigned long long size;
+	unsigned long long size = 0;
+	unsigned long long pvmetadatacopies = DEFAULT_PVMETADATACOPIES;
+	unsigned long long pvmetadatasize = DEFAULT_PVMETADATASIZE;
+	unsigned long long data_alignment = 0;
+	unsigned long long data_alignment_offset = 0;
+	unsigned long long zero = 1;
+	pv_create_params_t pv_params = NULL;
 
 	LVM_VALID(NULL);
 
-	if (!PyArg_ParseTuple(arg, "sK", &pv_name, &size))
+	if (!PyArg_ParseTuple(arg, "s|KKKKKK", &pv_name, &size, &pvmetadatacopies,
+							&pvmetadatasize, &data_alignment,
+							&data_alignment_offset, &zero))
 		return NULL;
 
-	if (lvm_pv_create(_libh, pv_name, size) == -1) {
-		PyErr_SetObject(_LibLVMError, _liblvm_get_last_error());
-		return NULL;
+	pv_params = lvm_pv_params_create(_libh, pv_name);
+	if (!pv_params) {
+		goto error;
 	}
 
-	Py_INCREF(Py_None);
+	SET_PV_PROP(pv_params, "size", size);
+	SET_PV_PROP(pv_params, "pvmetadatacopies", pvmetadatacopies);
+	SET_PV_PROP(pv_params, "pvmetadatasize", pvmetadatasize);
+	SET_PV_PROP(pv_params, "data_alignment", data_alignment);
+	SET_PV_PROP(pv_params, "data_alignment_offset", data_alignment_offset);
+	SET_PV_PROP(pv_params, "zero", zero);
+
+	if (lvm_pv_create_adv(pv_params)) {
+		goto error;
+	}
 
+	Py_INCREF(Py_None);
 	return Py_None;
+
+error:
+	PyErr_SetObject(_LibLVMError, _liblvm_get_last_error());
+	return NULL;
 }
 
 static PyObject *_liblvm_lvm_percent_to_float(PyObject *self, PyObject *arg)
Signature · 1 line
1.8.2.1
#8 lore raw

PATCH 8/13 python-lvm: Update and enable unit test case

In reply to Tony Asleson

Added tests for lvm.pvCreate and enable the test suite.

---
 test/api/pytest.sh          |  4 --
 test/api/python_lvm_unit.py | 99 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 99 insertions(+), 4 deletions(-)
test/api/pytest.sh +0 -4
index 791c9dc..67e224c 100644
@@ -27,8 +27,4 @@ export PYTHONPATH=`dirname $python_lib`:$PYTHONPATH
 #Setup which devices the unit test can use.
 export PY_UNIT_PVS=$(cat DEVICES)
 
-
-#We will skip until we can ensure it is correct.
-skip
-
 python_lvm_unit.py -v -f
test/api/python_lvm_unit.py +99 -0

110 lines not on this page · raw message

Signature · 1 line
1.8.2.1
#9 lore raw

PATCH 9/13 lvm2app: Add VG/LV name validation

In reply to Tony Asleson

C library portion for
https://bugzilla.redhat.com/show_bug.cgi?id=883689

---
 lib/metadata/metadata-exported.h |  1 +
 lib/metadata/metadata.c          |  4 +--
 lib/misc/lvm-string.c            | 68 ++++++++++++++++++++++++++++++++--------
 lib/misc/lvm-string.h            |  1 +
 liblvm/lvm2app.h                 | 32 +++++++++++++++++++
 liblvm/lvm_base.c                |  1 +
 liblvm/lvm_vg.c                  | 21 +++++++++++++
 7 files changed, 113 insertions(+), 15 deletions(-)
lib/metadata/metadata-exported.h +1 -0
index 6d056a9..0f3cfce 100644
@@ -584,6 +584,7 @@ int pv_analyze(struct cmd_context *cmd, const char *pv_name,
 /* FIXME: move internal to library */
 uint32_t pv_list_extents_free(const struct dm_list *pvh);
 
+int validate_new_vg_name(struct cmd_context *cmd, const char *vg_name);
 int vg_validate(struct volume_group *vg);
 struct volume_group *vg_create(struct cmd_context *cmd, const char *vg_name);
 int vg_remove_mdas(struct volume_group *vg);
lib/metadata/metadata.c +2 -2
index 5bb385d..a2ff54a 100644
@@ -429,11 +429,11 @@ int move_pvs_used_by_lv(struct volume_group *vg_from,
 	return 1;
 }
 
-static int validate_new_vg_name(struct cmd_context *cmd, const char *vg_name)
+int validate_new_vg_name(struct cmd_context *cmd, const char *vg_name)
 {
 	static char vg_path[PATH_MAX];
 
-	if (!validate_name(vg_name)) {
+	if (!validate_name_detailed(vg_name)) {
 		log_error("New volume group name \"%s\" is invalid.",
 			  vg_name);
 		return 0;
lib/misc/lvm-string.c +55 -13

96 lines not on this page · raw message

lib/misc/lvm-string.h +1 -0
index 6be048d..1e2ecc5 100644
@@ -31,6 +31,7 @@ char *build_dm_uuid(struct dm_pool *mem, const char *lvid,
 		    const char *layer);
 
 int validate_name(const char *n);
+int validate_name_detailed(const char *n);
 int validate_tag(const char *n);
 
 int apply_lvname_restrictions(const char *name);
liblvm/lvm2app.h +32 -0
index afc6793..0940b6f 100644
@@ -510,6 +510,21 @@ vg_t lvm_vg_open(lvm_t libh, const char *vgname, const char *mode,
 		  uint32_t flags);
 
 /**
+ * Validate a name to be used for new VG construction.
+ *
+ * This function checks that the name has no invalid characters,
+ * the length doesn't exceed maximum and that the VG name isn't already in use
+ * and that the name adheres to any other limitations.
+ *
+ * \param libh
+ * Valid library handle
+ *
+ * \param name
+ * Name to validate for new VG create.
+ */
+int lvm_vg_name_validate(lvm_t libh, const char *vg_name);
+
+/**
  * Create a VG with default parameters.
  *
  * \memberof lvm_t
@@ -1554,6 +1569,23 @@ int lvm_lv_resize(const lv_t lv, uint64_t new_size);
 lv_t lvm_lv_snapshot(const lv_t lv, const char *snap_name, uint64_t max_snap_size);
 
 /**
+ * Validate a name to be used for LV creation.
+ *
+ * Validates that the name does not contain any invalid characters, max length
+ * and that the LV name doesn't already exist for this VG.
+ *
+ * Note: You can have the same LV name in different VGs, thus the reason this
+ * function requires that you specify a VG to check against.
+ *
+ * \param lv
+ * Volume group handle.
+ *
+ * \param name
+ * Name to validate
+ */
+int lvm_lv_name_validate(const vg_t vg, const char *lv_name);
+
+/**
  * Thin provisioning discard policies
  */
 typedef enum {
liblvm/lvm_base.c +1 -0
index b7603e3..139cc97 100644
@@ -18,6 +18,7 @@
 #include "lvm-version.h"
 #include "metadata-exported.h"
 #include "lvm2app.h"
+#include "lvm-string.h"
 
 const char *lvm_library_get_version(void)
 {
liblvm/lvm_vg.c +21 -0
index 214f459..ee7aa18 100644
@@ -385,3 +385,24 @@ int lvm_scan(lvm_t libh)
 		return -1;
 	return 0;
 }
+
+int lvm_lv_name_validate(const vg_t vg, const char *name)
+{
+	if (validate_name_detailed(name) && apply_lvname_restrictions(name)) {
+		if (!find_lv_in_vg(vg, name)) {
+			return 0;
+		} else {
+			log_errno(EINVAL, "LV name exists in VG");
+		}
+	}
+	return -1;
+}
+
+int lvm_vg_name_validate(lvm_t libh, const char *name)
+{
+	struct cmd_context *cmd = (struct cmd_context *)libh;
+
+	if (validate_new_vg_name(cmd, name))
+		return 0;
+	return -1;
+}
Signature · 1 line
1.8.2.1
#10 lore raw

PATCH 10/13 python-lvm: VG/LV name validation.

In reply to Tony Asleson

Python portion of
https://bugzilla.redhat.com/show_bug.cgi?id=883689

---
 python/liblvm.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
python/liblvm.c +40 -0
index 033de82..330ef14 100644
@@ -391,6 +391,25 @@ static PyObject *_liblvm_lvm_percent_to_float(PyObject *self, PyObject *arg)
 	return Py_BuildValue("d", converted);
 }
 
+static PyObject *_liblvm_lvm_vg_name_validate(PyObject *self, PyObject *arg)
+{
+	const char *name;
+
+	LVM_VALID(NULL);
+
+	if (!PyArg_ParseTuple(arg, "s", &name))
+		return NULL;
+
+	if (lvm_vg_name_validate(_libh, name) < 0) {
+		PyErr_SetObject(_LibLVMError, _liblvm_get_last_error());
+		return NULL;
+	}
+
+	Py_INCREF(Py_None);
+
+	return Py_None;
+}
+
 static PyObject *_liblvm_lvm_vgname_from_pvid(PyObject *self, PyObject *arg)
 {
 	const char *pvid;
@@ -1213,6 +1232,25 @@ static PyObject *_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_lv_name_validate(vgobject *self, PyObject *args)
+{
+	const char *name;
+
+	VG_VALID(self);
+
+	if (!PyArg_ParseTuple(args, "s", &name))
+		return NULL;
+
+	if (lvm_lv_name_validate(self->vg, name) < 0) {
+		PyErr_SetObject(_LibLVMError, _liblvm_get_last_error());
+		return NULL;
+	}
+
+	Py_INCREF(Py_None);
+
+	return Py_None;
+}
+
 static PyObject *_liblvm_lvm_pv_from_N(vgobject *self, PyObject *arg, pv_fetch_by_N method)
 {
 	const char *id;
@@ -1771,6 +1809,7 @@ static PyMethodDef _Liblvm_methods[] = {
 	{ "pvCreate",		(PyCFunction)_liblvm_lvm_pv_create, METH_VARARGS },
 	{ "pvRemove",		(PyCFunction)_liblvm_lvm_pv_remove, METH_VARARGS },
 	{ "percentToFloat",	(PyCFunction)_liblvm_lvm_percent_to_float, METH_VARARGS },
+	{ "vgNameValidate",	(PyCFunction)_liblvm_lvm_vg_name_validate, METH_VARARGS },
 	{ "vgNameFromPvid",	(PyCFunction)_liblvm_lvm_vgname_from_pvid, METH_VARARGS },
 	{ "vgNameFromDevice",	(PyCFunction)_liblvm_lvm_vgname_from_device, METH_VARARGS },
 	{ NULL, NULL }		/* sentinel */
@@ -1805,6 +1844,7 @@ 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 },
+	{ "lvNameValidate",	(PyCFunction)_liblvm_lvm_lv_name_validate, 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 },
Signature · 1 line
1.8.2.1
#11 lore raw

PATCH 11/13 lvm2app: Reset buffer after retrieving error message

In reply to Tony Asleson

The error buffer will stack error messages which is fine. However, once you retrieve the error messages it doesn't make sense to keep appending for each additional error message when running in the context of a library call.

This patch clears and resets the buffer after the user retrieves the error message.

---
 lib/log/log.c         | 7 +++++++
 lib/log/lvm-logging.h | 1 +
 liblvm/lvm_base.c     | 9 ++++++++-
 python/liblvm.c       | 5 ++++-
 4 files changed, 20 insertions(+), 2 deletions(-)
lib/log/log.c +7 -0
index e0c1e73..25f2264 100644
@@ -170,6 +170,13 @@ const char *stored_errmsg(void)
 	return _lvm_errmsg ? : "";
 }
 
+const char *stored_errmsg_and_clear(void)
+{
+	const char *rc = strdup(stored_errmsg());
+	reset_lvm_errno(1);
+	return rc;
+}
+
 static struct dm_hash_table *_duplicated = NULL;
 
 void reset_log_duplicated(void) {
lib/log/lvm-logging.h +1 -0
index 52cd895..2f0d843 100644
@@ -55,6 +55,7 @@ int error_message_produced(void);
 void reset_lvm_errno(int store_errmsg);
 int stored_errno(void);
 const char *stored_errmsg(void);
+const char *stored_errmsg_and_clear(void);
 
 /* Suppress messages to stdout/stderr (1) or everywhere (2) */
 /* Returns previous setting */
liblvm/lvm_base.c +8 -1
index 139cc97..891cdab 100644
@@ -115,7 +115,14 @@ int lvm_errno(lvm_t libh)
 
 const char *lvm_errmsg(lvm_t libh)
 {
-	return stored_errmsg();
+	const char *rc = NULL;
+	struct cmd_context *cmd = (struct cmd_context *)libh;
+	const char *msg = stored_errmsg_and_clear();
+	if (msg) {
+		rc = dm_pool_strdup(cmd->mem, msg);
+		free((void *)msg);
+	}
+	return rc;
 }
 
 const char *lvm_vgname_from_pvid(lvm_t libh, const char *pvid)
python/liblvm.c +4 -1
index 330ef14..6abd5ff 100644
@@ -141,6 +141,7 @@ static lvobject *_create_py_lv(vgobject *parent, lv_t lv)
 static PyObject *_liblvm_get_last_error(void)
 {
 	PyObject *info;
+	const char *msg = NULL;
 
 	LVM_VALID(NULL);
 
@@ -148,7 +149,9 @@ static PyObject *_liblvm_get_last_error(void)
 		return NULL;
 
 	PyTuple_SetItem(info, 0, PyInt_FromLong((long) lvm_errno(_libh)));
-	PyTuple_SetItem(info, 1, PyString_FromString(lvm_errmsg(_libh)));
+	msg = lvm_errmsg(_libh);
+	PyTuple_SetItem(info, 1, ((msg) ? PyString_FromString(msg) :
+			PyString_FromString("Memory error while retrieving error message")));
 
 	return info;
 }
Signature · 1 line
1.8.2.1
#12 lore raw

PATCH 12/13 lvm2app: Add LVM2APP_DEBUG env variable for debug

In reply to Tony Asleson

This patch allows a user to define the LVM2APP_DEBUG environmental variable and log debug messages to stderr for bug reports.

---
 lib/log/log.c     | 11 +++++++++++
 lib/log/log.h     |  2 ++
 liblvm/lvm_base.c | 14 +++++++++++---
 3 files changed, 24 insertions(+), 3 deletions(-)
lib/log/log.c +11 -0
index 25f2264..1de7a3e 100644
@@ -34,6 +34,7 @@ static int _log_suppress = 0;
 static char _msg_prefix[30] = "  ";
 static int _already_logging = 0;
 static int _abort_on_internal_errors = 0;
+static int _force_to_std_error = 0;
 
 static lvm2_log_fn_t _lvm2_log_fn = NULL;
 
@@ -44,6 +45,11 @@ static size_t _lvm_errmsg_size = 0;
 static size_t _lvm_errmsg_len = 0;
 #define MAX_ERRMSG_LEN (512 * 1024)  /* Max size of error buffer 512KB */
 
+void init_force_stderr(int yes_no)
+{
+	_force_to_std_error = yes_no;
+}
+
 void init_log_fn(lvm2_log_fn_t log_fn)
 {
 	if (log_fn)
@@ -202,6 +208,11 @@ void print_log(int level, const char *file, int line, int dm_errno_or_class,
 
 	level &= ~(_LOG_STDERR|_LOG_ONCE);
 
+	/* lvm2app debug will be outputting everything to STDERR */
+	if (_force_to_std_error) {
+		use_stderr = 1;
+	}
+
 	if (_abort_on_internal_errors &&
 	    !strncmp(format, INTERNAL_ERROR, sizeof(INTERNAL_ERROR) - 1)) {
 		fatal_internal_error = 1;
lib/log/log.h +2 -0
index 99e7623..6059663 100644
@@ -107,6 +107,8 @@
 #define log_sys_debug(x, y) \
 		log_debug("%s: %s failed: %s", y, x, strerror(errno))
 
+void init_force_stderr(int yes_no);
+
 #define return_0	do { stack; return 0; } while (0)
 #define return_NULL	do { stack; return NULL; } while (0)
 #define return_ECMD_FAILED do { stack; return ECMD_FAILED; } while (0)
liblvm/lvm_base.c +11 -3
index 891cdab..f7a71df 100644
@@ -68,10 +68,18 @@ lvm_t lvm_init(const char *system_dir)
 	cmd->cmd_line = "liblvm";
 
 	/*
-	 * Turn off writing to stdout/stderr.
-	 * FIXME Fix lib/ to support a non-interactive mode instead.
+	 * If a user defines LVM2APP_DEBUG we will log at a high level to stderr
 	 */
-	log_suppress(1);
+	if (getenv("LVM2APP_DEBUG")) {
+		init_verbose(_LOG_DEBUG);
+		init_force_stderr(1);
+	} else {
+		/*
+		 * Turn off writing to stdout/stderr.
+		 * FIXME Fix lib/ to support a non-interactive mode instead.
+		 */
+		log_suppress(1);
+	}
 
 	return (lvm_t) cmd;
 }
Signature · 1 line
1.8.2.1
#13 lore raw

PATCH 13/13 lvm2app: Remove forward declarations.

In reply to Tony Asleson

Remove the forward struct declaration. This isn't needed for implementing opaque data pointers.

---
 liblvm/lvm2app.h | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)
liblvm/lvm2app.h +7 -14
index 0940b6f..a91640a 100644
@@ -84,20 +84,13 @@ const char *lvm_library_get_version(void);
 /******************************** structures ********************************/
 
 /**
- * Opaque structures - do not use directly.  Internal structures may change
- * without notice between releases, whereas this API will be changed much less
- * frequently.  Backwards compatibility will normally be preserved in future
- * releases.  On any occasion when the developers do decide to break backwards
- * compatibility in any significant way, the LVM_LIBAPI number (included in
- * the library's soname) will be incremented.
- */
-struct lvm;
-struct physical_volume;
-struct volume_group;
-struct logical_volume;
-struct lv_segment;
-struct pv_segment;
-struct lvm_lv_create_params;
+ * Opaque C pointers - Internal structures may change without notice between
+ * releases, whereas this API will be changed much less frequently.  Backwards
+ * compatibility will normally be preserved in future releases.  On any occasion
+ * when the developers do decide to break backwards compatibility in any
+ * significant way, the LVM_LIBAPI number (included in the library's soname)
+ * will be incremented.
+ */
 
 /**
  * \class lvm_t
Signature · 1 line
1.8.2.1