Skip to contentSkip to conversation

: Refactor lvcreate and add lvm_vg_create_lv_linear

PATCH New Dave Wysochanski 1 participant 12 messages Jul 25, 2009

lore
Share & tools
lore
#0 lore raw

cover : Refactor lvcreate and add lvm_vg_create_lv_linear

This patch set does some refactoring of lvcreate then adds the liblvm function lvm_vg_create_lv_linear, as well as updates the interactive unit test to call the API.

I haven't broken the testsuite and applies on top of:
commit 333091a0d31d8f710bac60ab7f6392fe8dcb1c92

Author: Dave Wysochanski <[email protected]>
Date:   Fri Jul 24 15:15:26 2009 +0000

Eliminate compile warning introduced by previous commit.

#1 lore raw

PATCH 1/11 Refactor _lvcreate - move *_ARG into _lvcreate_params and get 'cmd' from 'vg'.

A couple simple refactorings of _lvcreate - should be no functional change. Move tags_ARG parsing into _lvcreate_params. Also use lp->voriginsize instread of arg_count(). These refactorings make it easier to move the bulk of _lvcreate into the library.

---
 tools/lvcreate.c |   26 ++++++++++++++------------
 1 files changed, 14 insertions(+), 12 deletions(-)
tools/lvcreate.c +14 -12
index 165bf7c..af18ca8 100644
@@ -53,6 +53,7 @@ struct lvcreate_params {
 
 	int pv_count;
 	char **pvs;
+	const char *tag;
 };
 
 static int _lvcreate_name_params(struct lvcreate_params *lp,
@@ -525,6 +526,12 @@ static int _lvcreate_params(struct lvcreate_params *lp, struct cmd_context *cmd,
 		return 0;
 	}
 
+	if (arg_count(cmd, addtag_ARG) &&
+	    !(lp->tag = arg_str_value(cmd, addtag_ARG, NULL))) {
+		log_error("Failed to get tag");
+		return 0;
+	}
+
 	lp->pv_count = argc;
 	lp->pvs = argv;
 
@@ -593,14 +600,14 @@ static struct logical_volume *_create_virtual_origin(struct cmd_context *cmd,
 	return lv;
 }
 
-static int _lvcreate(struct cmd_context *cmd, struct volume_group *vg,
+static int _lvcreate(struct volume_group *vg,
 		     struct lvcreate_params *lp)
 {
+	struct cmd_context *cmd = vg->cmd;
 	uint32_t size_rest;
 	uint32_t status = 0;
 	struct logical_volume *lv, *org = NULL;
 	struct dm_list *pvh;
-	const char *tag = NULL;
 	int origin_active = 0;
 	char lv_name_buf[128];
 	const char *lv_name;
@@ -722,7 +729,7 @@ static int _lvcreate(struct cmd_context *cmd, struct volume_group *vg,
 		/* Must zero cow */
 		status |= LVM_WRITE;
 
-		if (arg_count(cmd, virtualsize_ARG))
+		if (lp->voriginsize)
 			origin_active = 1;
 		else {
 
@@ -806,12 +813,7 @@ static int _lvcreate(struct cmd_context *cmd, struct volume_group *vg,
 		lv_name = &lv_name_buf[0];
 	}
 
-	if (arg_count(cmd, addtag_ARG)) {
-		if (!(tag = arg_str_value(cmd, addtag_ARG, NULL))) {
-			log_error("Failed to get tag");
-			return 0;
-		}
-
+	if (lp->tag) {
 		if (!(vg->fid->fmt->features & FMT_TAGS)) {
 			log_error("Volume group %s does not support tags",
 				  vg->name);
@@ -846,9 +848,9 @@ static int _lvcreate(struct cmd_context *cmd, struct volume_group *vg,
 			    lv->minor);
 	}
 
-	if (tag && !str_list_add(cmd->mem, &lv->tags, tag)) {
+	if (lp->tag && !str_list_add(cmd->mem, &lv->tags, lp->tag)) {
 		log_error("Failed to add tag %s to %s/%s",
-			  tag, lv->vg->name, lv->name);
+			  lp->tag, lv->vg->name, lv->name);
 		return 0;
 	}
 
@@ -1000,7 +1002,7 @@ int lvcreate(struct cmd_context *cmd, int argc, char **argv)
 		return ECMD_FAILED;
 	}
 
-	if (!_lvcreate(cmd, vg, &lp))
+	if (!_lvcreate(vg, &lp))
 		r = ECMD_FAILED;
 
 	unlock_and_release_vg(cmd, vg, lp.vg_name);
Signature · 1 line
1.6.0.6
#2 lore raw

PATCH 2/11 Refactor extents calculations / updates in _lvcreate.

Move extents calculation adjustments into their own local functions right after we read the vg. This calculation really is not part of the LV create function but is rather an adjustment to the parameters based on what is given on the cmdline. So we move it outside the main _lvcreate.

Should be no functional change.

---
 tools/lvcreate.c |  119 +++++++++++++++++++++++++++++++----------------------
 1 files changed, 70 insertions(+), 49 deletions(-)
tools/lvcreate.c +70 -49
index af18ca8..fbf155c 100644
@@ -18,6 +18,7 @@
 
 #include <fcntl.h>
 
+/* FIXME: refactor and reduce the size of this struct! */
 struct lvcreate_params {
 	/* flags */
 	int snapshot;
@@ -46,6 +47,7 @@ struct lvcreate_params {
 	uint32_t voriginextents;
 	uint64_t voriginsize;
 	percent_t percent;
+	struct dm_list *pvh;
 
 	uint32_t permission;
 	uint32_t read_ahead;
@@ -56,6 +58,9 @@ struct lvcreate_params {
 	const char *tag;
 };
 
+static uint64_t _extents_from_size(struct cmd_context *cmd, uint64_t size,
+				   uint32_t extent_size);
+
 static int _lvcreate_name_params(struct lvcreate_params *lp,
 				 struct cmd_context *cmd,
 				 int *pargc, char ***pargv)
@@ -151,6 +156,64 @@ static int _lvcreate_name_params(struct lvcreate_params *lp,
 	return 1;
 }
 
+/*
+ * Update extents parameters based on other parameters which affect the size
+ * calcuation.
+ * NOTE: We must do this here because of the percent_t typedef and because we
+ * need the vg.
+ */
+static int _update_extents_params(struct volume_group *vg,
+			       struct lvcreate_params *lp)
+{
+	uint32_t pv_extent_count;
+
+	if (lp->size &&
+	    !(lp->extents = _extents_from_size(vg->cmd, lp->size,
+					       vg->extent_size)))
+		return_0;
+
+	if (lp->voriginsize &&
+	    !(lp->voriginextents = _extents_from_size(vg->cmd, lp->voriginsize,
+						      vg->extent_size)))
+		return_0;
+
+	/*
+	 * Create the pv list before we parse lp->percent - might be
+	 * PERCENT_PVSs
+	 */
+	if (lp->pv_count) {
+		if (!(lp->pvh = create_pv_list(vg->cmd->mem, vg,
+					   lp->pv_count, lp->pvs, 1)))
+			return_0;
+	} else
+		lp->pvh = &vg->pvs;
+
+	switch(lp->percent) {
+		case PERCENT_VG:
+			lp->extents = lp->extents * vg->extent_count / 100;
+			break;
+		case PERCENT_FREE:
+			lp->extents = lp->extents * vg->free_count / 100;
+			break;
+		case PERCENT_PVS:
+			if (!lp->pv_count) {
+				log_error("Please specify physical volume(s) "
+					  "with %%PVS");
+				return 0;
+			}
+			pv_extent_count = pv_list_extents_free(lp->pvh);
+			lp->extents = lp->extents * pv_extent_count / 100;
+			break;
+		case PERCENT_LV:
+			log_error("Please express size as %%VG, %%PVS, or "
+				  "%%FREE.");
+			return 0;
+		case PERCENT_NONE:
+			break;
+	}
+	return 1;
+}
+
 static int _read_size_params(struct lvcreate_params *lp,
 			     struct cmd_context *cmd)
 {
@@ -607,12 +670,10 @@ static int _lvcreate(struct volume_group *vg,
 	uint32_t size_rest;
 	uint32_t status = 0;
 	struct logical_volume *lv, *org = NULL;
-	struct dm_list *pvh;
 	int origin_active = 0;
 	char lv_name_buf[128];
 	const char *lv_name;
 	struct lvinfo info;
-	uint32_t pv_extent_count;
 
 	if (lp->lv_name && find_lv_in_vg(vg, lp->lv_name)) {
 		log_error("Logical volume \"%s\" already exists in "
@@ -656,49 +717,6 @@ static int _lvcreate(struct volume_group *vg,
 		return 0;
 	}
 
-	if (lp->size &&
-	    !(lp->extents = _extents_from_size(cmd, lp->size, vg->extent_size)))
-		return_0;
-
-	if (lp->voriginsize &&
-	    !(lp->voriginextents = _extents_from_size(cmd, lp->voriginsize,
-						      vg->extent_size)))
-		return_0;
-
-	/*
-	 * Create the pv list.
-	 */
-	if (lp->pv_count) {
-		if (!(pvh = create_pv_list(cmd->mem, vg,
-					   lp->pv_count, lp->pvs, 1)))
-			return_0;
-	} else
-		pvh = &vg->pvs;
-
-	switch(lp->percent) {
-		case PERCENT_VG:
-			lp->extents = lp->extents * vg->extent_count / 100;
-			break;
-		case PERCENT_FREE:
-			lp->extents = lp->extents * vg->free_count / 100;
-			break;
-		case PERCENT_PVS:
-			if (!lp->pv_count) {
-				log_error("Please specify physical volume(s) "
-					  "with %%PVS");
-				return 0;
-			}
-			pv_extent_count = pv_list_extents_free(pvh);
-			lp->extents = lp->extents * pv_extent_count / 100;
-			break;
-		case PERCENT_LV:
-			log_error("Please express size as %%VG, %%PVS, or "
-				  "%%FREE.");
-			return 0;
-		case PERCENT_NONE:
-			break;
-	}
-
 	if ((size_rest = lp->extents % lp->stripes)) {
 		log_print("Rounding size (%d extents) up to stripe boundary "
 			  "size (%d extents)", lp->extents,
@@ -782,10 +800,10 @@ static int _lvcreate(struct volume_group *vg,
 		return 0;
 	}
 
-	if (lp->stripes > dm_list_size(pvh) && lp->alloc != ALLOC_ANYWHERE) {
+	if (lp->stripes > dm_list_size(lp->pvh) && lp->alloc != ALLOC_ANYWHERE) {
 		log_error("Number of stripes (%u) must not exceed "
 			  "number of physical volumes (%d)", lp->stripes,
-			  dm_list_size(pvh));
+			  dm_list_size(lp->pvh));
 		return 0;
 	}
 
@@ -855,7 +873,7 @@ static int _lvcreate(struct volume_group *vg,
 	}
 
 	if (!lv_extend(lv, lp->segtype, lp->stripes, lp->stripe_size,
-		       1, lp->extents, NULL, 0u, 0u, pvh, lp->alloc))
+		       1, lp->extents, NULL, 0u, 0u, lp->pvh, lp->alloc))
 		return_0;
 
 	if (lp->mirrors > 1) {
@@ -864,7 +882,7 @@ static int _lvcreate(struct volume_group *vg,
 						vg->extent_size,
 						lv->le_count,
 						lp->region_size),
-				    lp->corelog ? 0U : 1U, pvh, lp->alloc,
+				    lp->corelog ? 0U : 1U, lp->pvh, lp->alloc,
 				    MIRROR_BY_LV |
 				    (lp->nosync ? MIRROR_SKIP_INIT_SYNC : 0))) {
 			stack;
@@ -1002,6 +1020,9 @@ int lvcreate(struct cmd_context *cmd, int argc, char **argv)
 		return ECMD_FAILED;
 	}
 
+	if (!_update_extents_params(vg, &lp))
+		return ECMD_FAILED;
+
 	if (!_lvcreate(vg, &lp))
 		r = ECMD_FAILED;
 
Signature · 1 line
1.6.0.6
#3 lore raw

PATCH 3/11 Move percent_t from struct lvcreate_params to struct lvcreate_cmdline_params.

Create a new structure, lvcreate_cmdline_params, to store parameters only relevant for the cmdline, not the library call to lvcreate. Should be no functional change.

---
 tools/lvcreate.c |   29 +++++++++++++++++++----------
 1 files changed, 19 insertions(+), 10 deletions(-)
tools/lvcreate.c +19 -10
index fbf155c..f921d40 100644
@@ -18,6 +18,10 @@
 
 #include <fcntl.h>
 
+struct lvcreate_cmdline_params {
+	percent_t percent;
+};
+
 /* FIXME: refactor and reduce the size of this struct! */
 struct lvcreate_params {
 	/* flags */
@@ -46,7 +50,6 @@ struct lvcreate_params {
 	uint64_t size;
 	uint32_t voriginextents;
 	uint64_t voriginsize;
-	percent_t percent;
 	struct dm_list *pvh;
 
 	uint32_t permission;
@@ -163,7 +166,8 @@ static int _lvcreate_name_params(struct lvcreate_params *lp,
  * need the vg.
  */
 static int _update_extents_params(struct volume_group *vg,
-			       struct lvcreate_params *lp)
+				  struct lvcreate_params *lp,
+				  struct lvcreate_cmdline_params *lcp)
 {
 	uint32_t pv_extent_count;
 
@@ -178,7 +182,7 @@ static int _update_extents_params(struct volume_group *vg,
 		return_0;
 
 	/*
-	 * Create the pv list before we parse lp->percent - might be
+	 * Create the pv list before we parse lcp->percent - might be
 	 * PERCENT_PVSs
 	 */
 	if (lp->pv_count) {
@@ -188,7 +192,7 @@ static int _update_extents_params(struct volume_group *vg,
 	} else
 		lp->pvh = &vg->pvs;
 
-	switch(lp->percent) {
+	switch(lcp->percent) {
 		case PERCENT_VG:
 			lp->extents = lp->extents * vg->extent_count / 100;
 			break;
@@ -215,6 +219,7 @@ static int _update_extents_params(struct volume_group *vg,
 }
 
 static int _read_size_params(struct lvcreate_params *lp,
+			     struct lvcreate_cmdline_params *lcp,
 			     struct cmd_context *cmd)
 {
 	if (arg_count(cmd, extents_ARG) + arg_count(cmd, size_ARG) != 1) {
@@ -228,7 +233,7 @@ static int _read_size_params(struct lvcreate_params *lp,
 			return 0;
 		}
 		lp->extents = arg_uint_value(cmd, extents_ARG, 0);
-		lp->percent = arg_percent_value(cmd, extents_ARG, PERCENT_NONE);
+		lcp->percent = arg_percent_value(cmd, extents_ARG, PERCENT_NONE);
 	}
 
 	/* Size returned in kilobyte units; held in sectors */
@@ -238,7 +243,7 @@ static int _read_size_params(struct lvcreate_params *lp,
 			return 0;
 		}
 		lp->size = arg_uint64_value(cmd, size_ARG, UINT64_C(0));
-		lp->percent = PERCENT_NONE;
+		lcp->percent = PERCENT_NONE;
 	}
 
 	/* Size returned in kilobyte units; held in sectors */
@@ -407,13 +412,16 @@ static int _read_mirror_params(struct lvcreate_params *lp,
 	return 1;
 }
 
-static int _lvcreate_params(struct lvcreate_params *lp, struct cmd_context *cmd,
+static int _lvcreate_params(struct lvcreate_params *lp,
+			    struct lvcreate_cmdline_params *lcp,
+			    struct cmd_context *cmd,
 			    int argc, char **argv)
 {
 	int contiguous;
 	unsigned pagesize;
 
 	memset(lp, 0, sizeof(*lp));
+	memset(lcp, 0, sizeof(*lcp));
 
 	/*
 	 * Check selected options are compatible and determine segtype
@@ -508,7 +516,7 @@ static int _lvcreate_params(struct lvcreate_params *lp, struct cmd_context *cmd,
 	}
 
 	if (!_lvcreate_name_params(lp, cmd, &argc, &argv) ||
-	    !_read_size_params(lp, cmd) ||
+	    !_read_size_params(lp, lcp, cmd) ||
 	    !_read_stripe_params(lp, cmd) ||
 	    !_read_mirror_params(lp, cmd))
 		return_0;
@@ -1006,11 +1014,12 @@ int lvcreate(struct cmd_context *cmd, int argc, char **argv)
 {
 	int r = ECMD_PROCESSED;
 	struct lvcreate_params lp;
+	struct lvcreate_cmdline_params lcp;
 	struct volume_group *vg;
 
 	memset(&lp, 0, sizeof(lp));
 
-	if (!_lvcreate_params(&lp, cmd, argc, argv))
+	if (!_lvcreate_params(&lp, &lcp, cmd, argc, argv))
 		return EINVALID_CMD_LINE;
 
 	log_verbose("Finding volume group \"%s\"", lp.vg_name);
@@ -1020,7 +1029,7 @@ int lvcreate(struct cmd_context *cmd, int argc, char **argv)
 		return ECMD_FAILED;
 	}
 
-	if (!_update_extents_params(vg, &lp))
+	if (!_update_extents_params(vg, &lp, &lcp))
 		return ECMD_FAILED;
 
 	if (!_lvcreate(vg, &lp))
Signature · 1 line
1.6.0.6
#4 lore raw

PATCH 4/11 Move 'size' from lvcreate_params into lvcreate_cmdline_params.

The main _lvcreate function should deal with extents - the 'size' parameter is just an intermediate step.
Should be no functional change.

---
 tools/lvcreate.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
tools/lvcreate.c +4 -4
index f921d40..cca8fc8 100644
@@ -20,6 +20,7 @@
 
 struct lvcreate_cmdline_params {
 	percent_t percent;
+	uint64_t size;
 };
 
 /* FIXME: refactor and reduce the size of this struct! */
@@ -47,7 +48,6 @@ struct lvcreate_params {
 
 	/* size */
 	uint32_t extents;
-	uint64_t size;
 	uint32_t voriginextents;
 	uint64_t voriginsize;
 	struct dm_list *pvh;
@@ -171,8 +171,8 @@ static int _update_extents_params(struct volume_group *vg,
 {
 	uint32_t pv_extent_count;
 
-	if (lp->size &&
-	    !(lp->extents = _extents_from_size(vg->cmd, lp->size,
+	if (lcp->size &&
+	    !(lp->extents = _extents_from_size(vg->cmd, lcp->size,
 					       vg->extent_size)))
 		return_0;
 
@@ -242,7 +242,7 @@ static int _read_size_params(struct lvcreate_params *lp,
 			log_error("Negative size is invalid");
 			return 0;
 		}
-		lp->size = arg_uint64_value(cmd, size_ARG, UINT64_C(0));
+		lcp->size = arg_uint64_value(cmd, size_ARG, UINT64_C(0));
 		lcp->percent = PERCENT_NONE;
 	}
 
Signature · 1 line
1.6.0.6
#5 lore raw

PATCH 5/11 Comment lvcreate_params struct.

---
 tools/lvcreate.c |   50 +++++++++++++++++++++++++-------------------------
 1 files changed, 25 insertions(+), 25 deletions(-)
tools/lvcreate.c +28 -28
index cca8fc8..b6558f8 100644
@@ -26,39 +26,39 @@ struct lvcreate_cmdline_params {
 /* FIXME: refactor and reduce the size of this struct! */
 struct lvcreate_params {
 	/* flags */
-	int snapshot;
-	int zero;
-	int major;
-	int minor;
-	int corelog;
-	int nosync;
-
-	char *origin;
-	const char *vg_name;
-	const char *lv_name;
+	int snapshot; /* snap */
+	int zero; /* all */
+	int major; /* all */
+	int minor; /* all */
+	int corelog; /* mirror */
+	int nosync; /* mirror */
 
-	uint32_t stripes;
-	uint32_t stripe_size;
-	uint32_t chunk_size;
-	uint32_t region_size;
+	char *origin; /* snap */
+	const char *vg_name; /* all */
+	const char *lv_name; /* all */
 
-	uint32_t mirrors;
+	uint32_t stripes; /* striped */
+	uint32_t stripe_size; /* striped */
+	uint32_t chunk_size; /* snapshot */
+	uint32_t region_size; /* mirror */
 
-	const struct segment_type *segtype;
+	uint32_t mirrors; /* mirror */
+
+	const struct segment_type *segtype; /* all */
 
 	/* size */
-	uint32_t extents;
-	uint32_t voriginextents;
-	uint64_t voriginsize;
-	struct dm_list *pvh;
-
-	uint32_t permission;
-	uint32_t read_ahead;
-	alloc_policy_t alloc;
-
-	int pv_count;
-	char **pvs;
-	const char *tag;
+	uint32_t extents; /* all */
+	uint32_t voriginextents; /* snapshot */
+	uint64_t voriginsize; /* snapshot */
+	struct dm_list *pvh; /* all */
+
+	uint32_t permission; /* all */
+	uint32_t read_ahead; /* all */
+	alloc_policy_t alloc; /* all */
+
+	int pv_count; /* all; redundant? */
+	char **pvs; /* all; redundant? */
+	const char *tag; /* all */
 };
 
 static uint64_t _extents_from_size(struct cmd_context *cmd, uint64_t size,
Signature · 1 line
1.6.0.6
#6 lore raw

PATCH 6/11 Move pvs and pv_count fields from lvcreate_params to lvcreate_cmdline_params.

No functional change.

---
 tools/lvcreate.c |   14 +++++++-------
 1 files changed, 7 insertions(+), 7 deletions(-)
tools/lvcreate.c +7 -7
index b6558f8..aadf519 100644
@@ -21,6 +21,8 @@
 struct lvcreate_cmdline_params {
 	percent_t percent;
 	uint64_t size;
+	char **pvs;
+	int pv_count;
 };
 
 /* FIXME: refactor and reduce the size of this struct! */
@@ -56,8 +58,6 @@ struct lvcreate_params {
 	uint32_t read_ahead; /* all */
 	alloc_policy_t alloc; /* all */
 
-	int pv_count; /* all; redundant? */
-	char **pvs; /* all; redundant? */
 	const char *tag; /* all */
 };
 
@@ -185,9 +185,9 @@ static int _update_extents_params(struct volume_group *vg,
 	 * Create the pv list before we parse lcp->percent - might be
 	 * PERCENT_PVSs
 	 */
-	if (lp->pv_count) {
+	if (lcp->pv_count) {
 		if (!(lp->pvh = create_pv_list(vg->cmd->mem, vg,
-					   lp->pv_count, lp->pvs, 1)))
+					   lcp->pv_count, lcp->pvs, 1)))
 			return_0;
 	} else
 		lp->pvh = &vg->pvs;
@@ -200,7 +200,7 @@ static int _update_extents_params(struct volume_group *vg,
 			lp->extents = lp->extents * vg->free_count / 100;
 			break;
 		case PERCENT_PVS:
-			if (!lp->pv_count) {
+			if (!lcp->pv_count) {
 				log_error("Please specify physical volume(s) "
 					  "with %%PVS");
 				return 0;
@@ -603,8 +603,8 @@ static int _lvcreate_params(struct lvcreate_params *lp,
 		return 0;
 	}
 
-	lp->pv_count = argc;
-	lp->pvs = argv;
+	lcp->pv_count = argc;
+	lcp->pvs = argv;
 
 	return 1;
 }
Signature · 1 line
1.6.0.6
#7 lore raw

PATCH 7/11 Move _lvcreate into the internal library and rename to lv_create_single.

After some refactorings, we can now move the bulk of _lvcreate into the internal library, and we can call from liblvm. In the future, we should refactor lv_create_single further, probably by segtype, to reduce the size of struct lvcreate_params. For now this is a reasonable refactor and allows us to re-use the function from liblvm.

---
 lib/metadata/lv_manip.c          |  382 ++++++++++++++++++++++++++++++++++
 lib/metadata/metadata-exported.h |   39 ++++
 tools/lvcreate.c                 |  417 +-------------------------------------
 3 files changed, 422 insertions(+), 416 deletions(-)
lib/metadata/lv_manip.c +382 -0
index 30dcdd3..70d9da2 100644
@@ -25,6 +25,7 @@
 #include "segtype.h"
 #include "archiver.h"
 #include "activate.h"
+#include "str_list.h"
 
 struct lv_names {
 	const char *old;
@@ -2772,3 +2773,384 @@ int set_lv(struct cmd_context *cmd, struct logical_volume *lv,
 
 	return 1;
 }
+
+
+static struct logical_volume *_create_virtual_origin(struct cmd_context *cmd,
+						     struct volume_group *vg,
+						     const char *lv_name,
+						     uint32_t permission,
+						     uint64_t voriginextents)
+{
+	const struct segment_type *segtype;
+	size_t len;
+	char *vorigin_name;
+	struct logical_volume *lv;
+
+	if (!(segtype = get_segtype_from_string(cmd, "zero"))) {
+		log_error("Zero segment type for virtual origin not found");
+		return NULL;
+	}
+
+	len = strlen(lv_name) + 32;
+	if (!(vorigin_name = alloca(len)) ||
+	    dm_snprintf(vorigin_name, len, "%s_vorigin", lv_name) < 0) {
+		log_error("Virtual origin name allocation failed.");
+		return NULL;
+	}
+
+	if (!(lv = lv_create_empty(vorigin_name, NULL, permission,
+				   ALLOC_INHERIT, vg)))
+		return_NULL;
+
+	if (!lv_extend(lv, segtype, 1, 0, 1, voriginextents, NULL, 0u, 0u,
+		       NULL, ALLOC_INHERIT))
+		return_NULL;
+
+	/* store vg on disk(s) */
+	if (!vg_write(vg) || !vg_commit(vg))
+		return_NULL;
+
+	backup(vg);
+
+	return lv;
+}
+
+int lv_create_single(struct volume_group *vg,
+		     struct lvcreate_params *lp)
+{
+	struct cmd_context *cmd = vg->cmd;
+	uint32_t size_rest;
+	uint32_t status = 0;
+	struct logical_volume *lv, *org = NULL;
+	int origin_active = 0;
+	char lv_name_buf[128];
+	const char *lv_name;
+	struct lvinfo info;
+
+	if (lp->lv_name && find_lv_in_vg(vg, lp->lv_name)) {
+		log_error("Logical volume \"%s\" already exists in "
+			  "volume group \"%s\"", lp->lv_name, lp->vg_name);
+		return 0;
+	}
+
+	if (vg_max_lv_reached(vg)) {
+		log_error("Maximum number of logical volumes (%u) reached "
+			  "in volume group %s", vg->max_lv, vg->name);
+		return 0;
+	}
+
+	if (lp->mirrors > 1 && !(vg->fid->fmt->features & FMT_SEGMENTS)) {
+		log_error("Metadata does not support mirroring.");
+		return 0;
+	}
+
+	if (lp->read_ahead != DM_READ_AHEAD_AUTO &&
+	    lp->read_ahead != DM_READ_AHEAD_NONE &&
+	    (vg->fid->fmt->features & FMT_RESTRICTED_READAHEAD) &&
+	    (lp->read_ahead < 2 || lp->read_ahead > 120)) {
+		log_error("Metadata only supports readahead values between 2 and 120.");
+		return 0;
+	}
+
+	if (lp->stripe_size > vg->extent_size) {
+		log_error("Reducing requested stripe size %s to maximum, "
+			  "physical extent size %s",
+			  display_size(cmd, (uint64_t) lp->stripe_size),
+			  display_size(cmd, (uint64_t) vg->extent_size));
+		lp->stripe_size = vg->extent_size;
+	}
+
+	/* Need to check the vg's format to verify this - the cmd format isn't setup properly yet */
+	if (lp->stripes > 1 &&
+	    !(vg->fid->fmt->features & FMT_UNLIMITED_STRIPESIZE) &&
+	    (lp->stripe_size > STRIPE_SIZE_MAX)) {
+		log_error("Stripe size may not exceed %s",
+			  display_size(cmd, (uint64_t) STRIPE_SIZE_MAX));
+		return 0;
+	}
+
+	if ((size_rest = lp->extents % lp->stripes)) {
+		log_print("Rounding size (%d extents) up to stripe boundary "
+			  "size (%d extents)", lp->extents,
+			  lp->extents - size_rest + lp->stripes);
+		lp->extents = lp->extents - size_rest + lp->stripes;
+	}
+
+	if (lp->zero && !activation()) {
+		log_error("Can't wipe start of new LV without using "
+			  "device-mapper kernel driver");
+		return 0;
+	}
+
+	status |= lp->permission | VISIBLE_LV;
+
+	if (lp->snapshot) {
+		if (!activation()) {
+			log_error("Can't create snapshot without using "
+				  "device-mapper kernel driver");
+			return 0;
+		}
+		/* FIXME Allow exclusive activation. */
+		if (vg_is_clustered(vg)) {
+			log_error("Clustered snapshots are not yet supported.");
+			return 0;
+		}
+
+		/* Must zero cow */
+		status |= LVM_WRITE;
+
+		if (lp->voriginsize)
+			origin_active = 1;
+		else {
+
+			if (!(org = find_lv(vg, lp->origin))) {
+				log_error("Couldn't find origin volume '%s'.",
+					  lp->origin);
+				return 0;
+			}
+			if (lv_is_virtual_origin(org)) {
+				log_error("Can't share virtual origins. "
+					  "Use --virtualsize.");
+				return 0;
+			}
+			if (lv_is_cow(org)) {
+				log_error("Snapshots of snapshots are not "
+					  "supported yet.");
+				return 0;
+			}
+			if (org->status & LOCKED) {
+				log_error("Snapshots of locked devices are not "
+					  "supported yet");
+				return 0;
+			}
+			if (org->status & MIRROR_IMAGE ||
+			    org->status & MIRROR_LOG ||
+			    org->status & MIRRORED) {
+				log_error("Snapshots and mirrors may not yet "
+					  "be mixed.");
+				return 0;
+			}
+
+			if (!lv_info(cmd, org, &info, 0, 0)) {
+				log_error("Check for existence of snapshot "
+					  "origin '%s' failed.", org->name);
+				return 0;
+			}
+			origin_active = info.exists;
+		}
+	}
+
+	if (!lp->extents) {
+		log_error("Unable to create new logical volume with no extents");
+		return 0;
+	}
+
+	if (!seg_is_virtual(lp) &&
+	    vg->free_count < lp->extents) {
+		log_error("Insufficient free extents (%u) in volume group %s: "
+			  "%u required", vg->free_count, vg->name, lp->extents);
+		return 0;
+	}
+
+	if (lp->stripes > dm_list_size(lp->pvh) && lp->alloc != ALLOC_ANYWHERE) {
+		log_error("Number of stripes (%u) must not exceed "
+			  "number of physical volumes (%d)", lp->stripes,
+			  dm_list_size(lp->pvh));
+		return 0;
+	}
+
+	if (lp->mirrors > 1 && !activation()) {
+		log_error("Can't create mirror without using "
+			  "device-mapper kernel driver.");
+		return 0;
+	}
+
+	/* The snapshot segment gets created later */
+	if (lp->snapshot &&
+	    !(lp->segtype = get_segtype_from_string(cmd, "striped")))
+		return_0;
+
+	if (!archive(vg))
+		return 0;
+
+	if (lp->lv_name)
+		lv_name = lp->lv_name;
+	else {
+		if (!generate_lv_name(vg, "lvol%d", lv_name_buf, sizeof(lv_name_buf))) {
+			log_error("Failed to generate LV name.");
+			return 0;
+		}
+		lv_name = &lv_name_buf[0];
+	}
+
+	if (lp->tag) {
+		if (!(vg->fid->fmt->features & FMT_TAGS)) {
+			log_error("Volume group %s does not support tags",
+				  vg->name);
+			return 0;
+		}
+	}
+
+	if (lp->mirrors > 1) {
+		init_mirror_in_sync(lp->nosync);
+
+		if (lp->nosync) {
+			log_warn("WARNING: New mirror won't be synchronised. "
+				  "Don't read what you didn't write!");
+			status |= MIRROR_NOTSYNCED;
+		}
+	}
+
+	if (!(lv = lv_create_empty(lv_name ? lv_name : "lvol%d", NULL,
+				   status, lp->alloc, vg)))
+		return_0;
+
+	if (lp->read_ahead) {
+		log_verbose("Setting read ahead sectors");
+		lv->read_ahead = lp->read_ahead;
+	}
+
+	if (lp->minor >= 0) {
+		lv->major = lp->major;
+		lv->minor = lp->minor;
+		lv->status |= FIXED_MINOR;
+		log_verbose("Setting device number to (%d, %d)", lv->major,
+			    lv->minor);
+	}
+
+	if (lp->tag && !str_list_add(cmd->mem, &lv->tags, lp->tag)) {
+		log_error("Failed to add tag %s to %s/%s",
+			  lp->tag, lv->vg->name, lv->name);
+		return 0;
+	}
+
+	if (!lv_extend(lv, lp->segtype, lp->stripes, lp->stripe_size,
+		       1, lp->extents, NULL, 0u, 0u, lp->pvh, lp->alloc))
+		return_0;
+
+	if (lp->mirrors > 1) {
+		if (!lv_add_mirrors(cmd, lv, lp->mirrors - 1, lp->stripes,
+				    adjusted_mirror_region_size(
+						vg->extent_size,
+						lv->le_count,
+						lp->region_size),
+				    lp->corelog ? 0U : 1U, lp->pvh, lp->alloc,
+				    MIRROR_BY_LV |
+				    (lp->nosync ? MIRROR_SKIP_INIT_SYNC : 0))) {
+			stack;
+			goto revert_new_lv;
+		}
+	}
+
+	/* store vg on disk(s) */
+	if (!vg_write(vg) || !vg_commit(vg))
+		return_0;
+
+	backup(vg);
+
+	if (lp->snapshot) {
+		if (!activate_lv_excl(cmd, lv)) {
+			log_error("Aborting. Failed to activate snapshot "
+				  "exception store.");
+			goto revert_new_lv;
+		}
+	} else if (!activate_lv(cmd, lv)) {
+		if (lp->zero) {
+			log_error("Aborting. Failed to activate new LV to wipe "
+				  "the start of it.");
+			goto deactivate_and_revert_new_lv;
+		}
+		log_error("Failed to activate new LV.");
+		return 0;
+	}
+
+	if (!lp->zero && !lp->snapshot)
+		log_error("WARNING: \"%s\" not zeroed", lv->name);
+	else if (!set_lv(cmd, lv, UINT64_C(0), 0)) {
+		log_error("Aborting. Failed to wipe %s.",
+			  lp->snapshot ? "snapshot exception store" :
+					 "start of new LV");
+		goto deactivate_and_revert_new_lv;
+	}
+
+	if (lp->snapshot) {
+		/* Reset permission after zeroing */
+		if (!(lp->permission & LVM_WRITE))
+			lv->status &= ~LVM_WRITE;
+
+		/* COW area must be deactivated if origin is not active */
+		if (!origin_active && !deactivate_lv(cmd, lv)) {
+			log_error("Aborting. Couldn't deactivate snapshot "
+				  "COW area. Manual intervention required.");
+			return 0;
+		}
+
+		/* A virtual origin must be activated explicitly. */
+		if (lp->voriginsize &&
+		    (!(org = _create_virtual_origin(cmd, vg, lv->name,
+						    lp->permission,
+						    lp->voriginextents)) ||
+		     !activate_lv(cmd, org))) {
+			log_error("Couldn't create virtual origin for LV %s",
+				  lv->name);
+			if (org && !lv_remove(org))
+				stack;
+			goto deactivate_and_revert_new_lv;
+		}
+
+		/* cow LV remains active and becomes snapshot LV */
+
+		if (!vg_add_snapshot(org, lv, NULL,
+				     org->le_count, lp->chunk_size)) {
+			log_error("Couldn't create snapshot.");
+			goto deactivate_and_revert_new_lv;
+		}
+
+		/* store vg on disk(s) */
+		if (!vg_write(vg))
+			return_0;
+
+		if (!suspend_lv(cmd, org)) {
+			log_error("Failed to suspend origin %s", org->name);
+			vg_revert(vg);
+			return 0;
+		}
+
+		if (!vg_commit(vg))
+			return_0;
+
+		if (!resume_lv(cmd, org)) {
+			log_error("Problem reactivating origin %s", org->name);
+			return 0;
+		}
+	}
+	/* FIXME out of sequence */
+	backup(vg);
+
+	log_print("Logical volume \"%s\" created", lv->name);
+
+	/*
+	 * FIXME: as a sanity check we could try reading the
+	 * last block of the device ?
+	 */
+
+	return 1;
+
+deactivate_and_revert_new_lv:
+	if (!deactivate_lv(cmd, lv)) {
+		log_error("Unable to deactivate failed new LV. "
+			  "Manual intervention required.");
+		return 0;
+	}
+
+revert_new_lv:
+	/* FIXME Better to revert to backup of metadata? */
+	if (!lv_remove(lv) || !vg_write(vg) || !vg_commit(vg))
+		log_error("Manual intervention may be required to remove "
+			  "abandoned LV(s) before retrying.");
+	else
+		backup(vg);
+
+	return 0;
+}
+
lib/metadata/metadata-exported.h +39 -0
index 9d8d542..345992f 100644
@@ -475,6 +475,45 @@ int lv_remove_with_dependencies(struct cmd_context *cmd, struct logical_volume *
 int lv_rename(struct cmd_context *cmd, struct logical_volume *lv,
 	      const char *new_name);
 
+/* FIXME: refactor and reduce the size of this struct! */
+struct lvcreate_params {
+	/* flags */
+	int snapshot; /* snap */
+	int zero; /* all */
+	int major; /* all */
+	int minor; /* all */
+	int corelog; /* mirror */
+	int nosync; /* mirror */
+
+	char *origin; /* snap */
+	const char *vg_name; /* all */
+	const char *lv_name; /* all */
+
+	uint32_t stripes; /* striped */
+	uint32_t stripe_size; /* striped */
+	uint32_t chunk_size; /* snapshot */
+	uint32_t region_size; /* mirror */
+
+	uint32_t mirrors; /* mirror */
+
+	const struct segment_type *segtype; /* all */
+
+	/* size */
+	uint32_t extents; /* all */
+	uint32_t voriginextents; /* snapshot */
+	uint64_t voriginsize; /* snapshot */
+	struct dm_list *pvh; /* all */
+
+	uint32_t permission; /* all */
+	uint32_t read_ahead; /* all */
+	alloc_policy_t alloc; /* all */
+
+	const char *tag; /* all */
+};
+
+int lv_create_single(struct volume_group *vg,
+		     struct lvcreate_params *lp);
+
 /*
  * Functions for layer manipulation
  */
tools/lvcreate.c +1 -416

435 lines not on this page · raw message

Signature · 1 line
1.6.0.6
#8 lore raw

PATCH 8/11 Move extents_from_size from lvcreate into internal library so we can reuse.

---
 lib/metadata/metadata-exported.h |    3 +++
 lib/metadata/metadata.c          |   22 ++++++++++++++++++++++
 tools/lvcreate.c                 |   29 ++---------------------------
 3 files changed, 27 insertions(+), 27 deletions(-)
lib/metadata/metadata-exported.h +3 -0
index 345992f..370be25 100644
@@ -475,6 +475,9 @@ int lv_remove_with_dependencies(struct cmd_context *cmd, struct logical_volume *
 int lv_rename(struct cmd_context *cmd, struct logical_volume *lv,
 	      const char *new_name);
 
+uint64_t extents_from_size(struct cmd_context *cmd, uint64_t size,
+			   uint32_t extent_size);
+
 /* FIXME: refactor and reduce the size of this struct! */
 struct lvcreate_params {
 	/* flags */
lib/metadata/metadata.c +22 -0
index 3a59cf2..2871a93 100644
@@ -686,6 +686,28 @@ bad:
 	return NULL;
 }
 
+uint64_t extents_from_size(struct cmd_context *cmd, uint64_t size,
+			   uint32_t extent_size)
+{
+	if (size % extent_size) {
+		size += extent_size - size % extent_size;
+		log_print("Rounding up size to full physical extent %s",
+			  display_size(cmd, size));
+	}
+
+	if (size > (uint64_t) UINT32_MAX * extent_size) {
+		log_error("Volume too large (%s) for extent size %s. "
+			  "Upper limit is %s.",
+			  display_size(cmd, size),
+			  display_size(cmd, (uint64_t) extent_size),
+			  display_size(cmd, (uint64_t) UINT32_MAX *
+				       extent_size));
+		return 0;
+	}
+
+	return (uint64_t) size / extent_size;
+}
+
 static int _recalc_extents(uint32_t *extents, const char *desc1,
 			   const char *desc2, uint32_t old_size,
 			   uint32_t new_size)
tools/lvcreate.c +2 -27
index 8c13954..53943d7 100644
@@ -25,9 +25,6 @@ struct lvcreate_cmdline_params {
 	int pv_count;
 };
 
-static uint64_t _extents_from_size(struct cmd_context *cmd, uint64_t size,
-				   uint32_t extent_size);
-
 static int _lvcreate_name_params(struct lvcreate_params *lp,
 				 struct cmd_context *cmd,
 				 int *pargc, char ***pargv)
@@ -136,12 +133,12 @@ static int _update_extents_params(struct volume_group *vg,
 	uint32_t pv_extent_count;
 
 	if (lcp->size &&
-	    !(lp->extents = _extents_from_size(vg->cmd, lcp->size,
+	    !(lp->extents = extents_from_size(vg->cmd, lcp->size,
 					       vg->extent_size)))
 		return_0;
 
 	if (lp->voriginsize &&
-	    !(lp->voriginextents = _extents_from_size(vg->cmd, lp->voriginsize,
+	    !(lp->voriginextents = extents_from_size(vg->cmd, lp->voriginsize,
 						      vg->extent_size)))
 		return_0;
 
@@ -573,28 +570,6 @@ static int _lvcreate_params(struct lvcreate_params *lp,
 	return 1;
 }
 
-static uint64_t _extents_from_size(struct cmd_context *cmd, uint64_t size,
-				   uint32_t extent_size)
-{
-	if (size % extent_size) {
-		size += extent_size - size % extent_size;
-		log_print("Rounding up size to full physical extent %s",
-			  display_size(cmd, size));
-	}
-
-	if (size > (uint64_t) UINT32_MAX * extent_size) {
-		log_error("Volume too large (%s) for extent size %s. "
-			  "Upper limit is %s.",
-			  display_size(cmd, size),
-			  display_size(cmd, (uint64_t) extent_size),
-			  display_size(cmd, (uint64_t) UINT32_MAX *
-				       extent_size));
-		return 0;
-	}
-
-	return (uint64_t) size / extent_size;
-}
-
 int lvcreate(struct cmd_context *cmd, int argc, char **argv)
 {
 	int r = ECMD_PROCESSED;
Signature · 1 line
1.6.0.6
#9 lore raw

PATCH 9/11 Add lvm_vg_create_lv_* liblvm functions.

Create a default linear or striped logical volume from a volume group.

---
 liblvm/.exported_symbols |    2 +
 liblvm/lvm.h             |   32 ++++++++++++++++++
 liblvm/lvm_lv.c          |   83 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 117 insertions(+), 0 deletions(-)
liblvm/.exported_symbols +2 -0
index 08d5a60..257a13b 100644
@@ -21,3 +21,5 @@ lvm_vg_list_pvs
 lvm_vg_list_lvs
 lvm_list_vg_names
 lvm_list_vg_ids
+lvm_vg_create_lv_linear
+lvm_vg_create_lv_striped
liblvm/lvm.h +32 -0
index c2157a8..d906170 100644
@@ -97,6 +97,38 @@ void lvm_destroy(lvm_t libh);
 int lvm_reload_config(lvm_t libh);
 
 /**
+ * Create a linear logical volume.
+ *
+ * \param   vg
+ *          VG handle obtained from lvm_vg_create or lvm_vg_open.
+ *
+ * \param   name
+ *          Name of logical volume to create.
+ *
+ * \param   size
+ *          Size of logical volume in extents.
+ *
+ */
+lv_t *lvm_vg_create_lv_linear(vg_t *vg, const char *name, uint64_t size);
+
+/**
+ * Create a striped logical volume.
+ * The striped LV defaults to the following:
+ * - stripes == 1
+ * - stripe_size == 64KB
+ *
+ * \param   vg
+ *          VG handle obtained from lvm_vg_create or lvm_vg_open.
+ *
+ * \param   name
+ *          Name of logical volume to create.
+ *
+ * \param   size
+ *          Size of logical volume in extents.
+ */
+lv_t *lvm_vg_create_lv_striped(vg_t *vg, const char *name, uint64_t size);
+
+/**
  * Return stored error no describing last LVM API error.
  *
  * Users of liblvm should use lvm_errno to determine success or failure
liblvm/lvm_lv.c +83 -0

92 lines not on this page · raw message

Signature · 1 line
1.6.0.6
#10 lore raw

PATCH 10/11 Update test/api/test.c to handle read/write open modes.

---
 test/api/test.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
test/api/test.c +4 -2
index c9500ff..a7d6bc6 100644
@@ -162,8 +162,10 @@ static void _vg_open(char **argv, int argc, lvm_t libh)
 		printf ("VG already open\n");
 		return;
 	}
-	/* FIXME: allow different open modes */
-	vg = lvm_vg_open(libh, argv[1], "r", 0);
+	if (argc < 3)
+		vg = lvm_vg_open(libh, argv[1], "r", 0);
+	else
+		vg = lvm_vg_open(libh, argv[1], argv[2], 0);
 	if (!vg || !lvm_vg_get_name(vg)) {
 		printf("Error opening %s\n", argv[1]);
 		return;
Signature · 1 line
1.6.0.6
#11 lore raw

PATCH 11/11 Update test/api/test.c to call lvm_vg_create_lv_linear.

---
 test/api/test.c |   20 ++++++++++++++++++++
 1 files changed, 20 insertions(+), 0 deletions(-)
test/api/test.c +20 -0
index a7d6bc6..38222fa 100644
@@ -50,6 +50,8 @@ static int lvm_split(char *str, int *argc, char **argv, int max)
 
 static void _show_help(void)
 {
+	printf("'vg_create_lv_linear vgname size_in_sectors': "
+	       "Create a linear LV\n");
 	printf("'scan_vgs': "
 	       "Scan the system for LVM metadata\n");
 	printf("'vg_list_names': "
@@ -292,6 +294,22 @@ static void _lvs_in_vg(char **argv, int argc)
 	}
 }
 
+static void _vg_create_lv_linear(char **argv, int argc)
+{
+	vg_t *vg;
+	lv_t *lv;
+
+	if (!(vg = _lookup_vg_by_name(argv, argc)))
+		return;
+	lv = lvm_vg_create_lv_linear(vg, argv[1], atol(argv[2]));
+	if (!lv)
+		printf("Error ");
+	else
+		printf("Success ");
+	printf("creating LV %s in VG %s\n",
+		argv[1], lvm_vg_get_name(vg));
+}
+
 static int lvmapi_test_shell(lvm_t libh)
 {
 	int argc;
@@ -349,6 +367,8 @@ static int lvmapi_test_shell(lvm_t libh)
 			_vg_list_ids(libh);
 		} else if (!strcmp(argv[0], "scan_vgs")) {
 			_scan_vgs(libh);
+		} else if (!strcmp(argv[0], "vg_create_lv_linear")) {
+			_vg_create_lv_linear(argv, argc);
 		} else {
 			printf ("Unrecognized command %s\n", argv[0]);
 		}
Signature · 1 line
1.6.0.6