Skip to contentSkip to conversation

lvm2app: Add lv shapshot support

PATCH New Tony Asleson 1 participant 2 messages Nov 16, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 lvm2app: Add lv shapshot support

---
 liblvm/lvm2app.h | 21 +++++++++++++++++++++
 liblvm/lvm_lv.c  | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 76 insertions(+), 2 deletions(-)
liblvm/lvm2app.h +21 -0
index d99742c..7515973 100644
@@ -1377,6 +1377,27 @@ int lvm_lv_rename(lv_t lv, const char *new_name);
  */
 int lvm_lv_resize(const lv_t lv, uint64_t new_size);
 
+/**
+ * Create a snapshot of a logical volume
+ *
+ * \memberof lv_t
+ *
+ * \param   lv
+ * Logical volume handle.
+ *
+ * \param   snap_name
+ * Name of the snapshot.
+ *
+ * \param   max_snap_size
+ * Max snapshot space to use. If you pass zero the same amount of space as
+ * the origin will be used.
+ *
+ * \return
+ * Valid lv pointer on success, else NULL on error.
+ *
+ */
+lv_t lvm_lv_snapshot(const lv_t lv, const char *snap_name, uint64_t max_snap_size);
+
 /************************** physical volume handling ************************/
 
 /**
liblvm/lvm_lv.c +55 -2

70 lines not on this page · raw message

Signature · 1 line
1.7.11.7
#1 lore raw

PATCH 2/2 python-lvm: Add lv snapshot

---
 python/liblvm.c | 68 ++++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 51 insertions(+), 17 deletions(-)
python/liblvm.c +51 -17
index 831493e..c31a62f 100644
@@ -72,6 +72,15 @@ static PyObject *LibLVMError;
 		}							\
 	} while (0)
 
+#define LV_VALID(lvobject)						\
+	do {								\
+		VG_VALID(lvobject->parent_vgobj);			\
+		if (!lvobject->lv) {					\
+			PyErr_SetString(PyExc_UnboundLocalError, "LV object invalid"); \
+			return NULL;					\
+		}							\
+	} while (0)
+
 static PyObject *
 liblvm_get_last_error(void)
 {
@@ -862,16 +871,26 @@ liblvm_lvm_vg_get_tags(vgobject *self)
 	return pytuple;
 }
 
+enum lv_create { PY_LV_LINEAR, PY_LV_SNAPSHOT };
+
 static PyObject *
-liblvm_lvm_vg_create_lv_linear(vgobject *self, PyObject *args)
+_liblvm_lv_create(void *self, PyObject *args, enum lv_create op)
 {
-	const char *vgname;
+	const char *name;
 	uint64_t size;
 	lvobject *lvobj;
+	vgobject *vgp = NULL;
+	lvobject *lvp = NULL;
 
-	VG_VALID(self);
+	if (op == PY_LV_LINEAR) {
+		vgp = (vgobject *)self;
+		VG_VALID(vgp);
+	} else {
+		lvp = (lvobject *)self;
+		LV_VALID(lvp);
+	}
 
-	if (!PyArg_ParseTuple(args, "sl", &vgname, &size)) {
+	if (!PyArg_ParseTuple(args, "sl", &name, &size)) {
 		return NULL;
 	}
 
@@ -881,23 +900,47 @@ liblvm_lvm_vg_create_lv_linear(vgobject *self, PyObject *args)
 	/* 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)) == NULL) {
+	if (op == PY_LV_LINEAR) {
+		lvobj->lv = lvm_vg_create_lv_linear(vgp->vg, name, size);
+
+		if(lvobj->lv) {
+			lvobj->parent_vgobj = self;
+		}
+	} else {
+		lvobj->lv = lvm_lv_snapshot(lvp->lv, name, size);
+		if(lvobj->lv) {
+			lvobj->parent_vgobj = lvp->parent_vgobj;
+		}
+	}
+
+	if(!lvobj->lv) {
 		PyErr_SetObject(LibLVMError, liblvm_get_last_error());
 		Py_DECREF(lvobj);
 		return NULL;
 	}
 
-	lvobj->parent_vgobj = self;
 	Py_INCREF(lvobj->parent_vgobj);
 
 	return (PyObject *)lvobj;
 }
 
+static PyObject *
+liblvm_lvm_vg_create_lv_linear(vgobject *self, PyObject *args)
+{
+	return _liblvm_lv_create(self, args, PY_LV_LINEAR);
+}
+
+static PyObject *
+liblvm_lvm_lv_snapshot(lvobject *self, PyObject *args)
+{
+	return _liblvm_lv_create(self, args, PY_LV_SNAPSHOT);
+}
+
 static void
 liblvm_lv_dealloc(lvobject *self)
 {
 	/* We can dealloc an object that didn't get fully created */
-	if (self->parent_vgobj)	
+	if (self->parent_vgobj)
 		Py_DECREF(self->parent_vgobj);
 	PyObject_Del(self);
 }
@@ -1035,16 +1078,6 @@ liblvm_pv_dealloc(pvobject *self)
 
 /* LV Methods */
 
-#define LV_VALID(lvobject)						\
-	do {								\
-		VG_VALID(lvobject->parent_vgobj);			\
-		if (!lvobject->lv) {					\
-			PyErr_SetString(PyExc_UnboundLocalError, "LV object invalid"); \
-			return NULL;					\
-		}							\
-	} while (0)
-
-
 static PyObject *
 liblvm_lvm_lv_get_name(lvobject *self)
 {
@@ -1580,6 +1613,7 @@ static PyMethodDef liblvm_lv_methods[] = {
 	{ "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 },
+	{ "snapshot",		(PyCFunction)liblvm_lvm_lv_snapshot, METH_VARARGS },
 	{ NULL,	     NULL}   /* sentinel */
 };
 
Signature · 1 line
1.7.11.7