Skip to contentSkip to conversation

Add lvm2app functions to lookup an lv or pv handle

PATCH New Dave Wysochanski 1 participant 4 messages Nov 20, 2010

lore
Share & tools
lore
#0 lore raw

cover Add lvm2app functions to lookup an lv or pv handle

This patchset implements helper functions to lookup an lv or pv handle from vg handle and the pv|lv name or uuid.

Until now, users of lvm2app that wished to obtain an LV or PV handle from an existing VG must do so by the following sequence:
1) Call lvm_vg_open() to open an existing VG
2) Call lvm_vg_list_{pvs|lvs}() to list all PVs or LVs
3) Find the PV or LV of interest by comparing uuids or names

The above method is awkward for callers of the API that wish to operate on a single LV or PV. This patchset should make it easier for callers to do operations on a single LV or PV.

#1 lore raw

PATCH 1/3 Add lvm2app helper functions to lookup an lv and pv handle by name.

This patch adds helpers to allow users to lookup a lv or pv handle by name (given a vg_t of course).

---
 liblvm/lvm2app.h |   34 ++++++++++++++++++++++++++++++++++
 liblvm/lvm_lv.c  |   11 +++++++++++
 liblvm/lvm_pv.c  |   12 ++++++++++++
 3 files changed, 57 insertions(+), 0 deletions(-)
liblvm/lvm2app.h +34 -0
index d6378ac..4750211 100644
@@ -1015,6 +1015,23 @@ lv_t lvm_vg_create_lv_linear(vg_t vg, const char *name, uint64_t size);
 struct dm_list *lvm_lv_list_lvsegs(lv_t lv);
 
 /**
+ * Lookup an LV handle in a VG by the LV name.
+ *
+ * \memberof lv_t
+ *
+ * \param   vg
+ * VG handle obtained from lvm_vg_create() or lvm_vg_open().
+ *
+ * \param   name
+ * Name of LV to lookup.
+ *
+ * \return
+ * non-NULL handle to the LV 'name' attached to the VG.
+ * NULL is returned if the LV name is not associated with the VG handle.
+ */
+lv_t lvm_lv_from_name(vg_t vg, const char *name);
+
+/**
  * Activate a logical volume.
  *
  * \memberof lv_t
@@ -1489,6 +1506,23 @@ struct lvm_property_value lvm_pvseg_get_property(const pvseg_t pvseg,
 struct dm_list *lvm_pv_list_pvsegs(pv_t pv);
 
 /**
+ * Lookup an PV handle in a VG by the PV name.
+ *
+ * \memberof pv_t
+ *
+ * \param   vg
+ * VG handle obtained from lvm_vg_create() or lvm_vg_open().
+ *
+ * \param   name
+ * Name of PV to lookup.
+ *
+ * \return
+ * non-NULL handle to the PV 'name' attached to the VG.
+ * NULL is returned if the PV name is not associated with the VG handle.
+ */
+pv_t lvm_pv_from_name(vg_t vg, const char *name);
+
+/**
  * Resize physical volume to new_size bytes.
  *
  * \memberof pv_t
liblvm/lvm_lv.c +11 -0
index 432f5b7..5e8322f 100644
@@ -249,6 +249,17 @@ struct dm_list *lvm_lv_list_lvsegs(lv_t lv)
 	return list;
 }
 
+lv_t lvm_lv_from_name(vg_t vg, const char *name)
+{
+	struct lv_list *lvl;
+
+	dm_list_iterate_items(lvl, &vg->lvs) {
+		if (!strcmp(name, lvl->lv->name))
+			return lvl->lv;
+	}
+	return NULL;
+}
+
 int lvm_lv_resize(const lv_t lv, uint64_t new_size)
 {
 	/* FIXME: add lv resize code here */
liblvm/lvm_pv.c +12 -0
index 9ca92d5..3372fde 100644
@@ -87,6 +87,18 @@ struct dm_list *lvm_pv_list_pvsegs(pv_t pv)
 	return list;
 }
 
+pv_t lvm_pv_from_name(vg_t vg, const char *name)
+{
+	struct pv_list *pvl;
+
+	dm_list_iterate_items(pvl, &vg->pvs) {
+		if (!strcmp(name, pv_dev_name(pvl->pv)))
+			return pvl->pv;
+	}
+	return NULL;
+}
+
+
 int lvm_pv_resize(const pv_t pv, uint64_t new_size)
 {
 	/* FIXME: add pv resize code here */
Signature · 1 line
1.7.2.2
#2 lore raw

PATCH 2/3 Add lvm2app helper functions to lookup an lv and pv handle by uuid.

In reply to Dave Wysochanski

This patch adds helpers to allow users to lookup a lv or pv handle by uuid (given a vg_t of course).

---
 liblvm/lvm2app.h |   38 ++++++++++++++++++++++++++++++++++++++
 liblvm/lvm_lv.c  |   22 ++++++++++++++++++++++
 liblvm/lvm_pv.c  |   22 ++++++++++++++++++++++
 3 files changed, 82 insertions(+), 0 deletions(-)
liblvm/lvm2app.h +38 -0
index 4750211..1fff08a 100644
@@ -1032,6 +1032,25 @@ struct dm_list *lvm_lv_list_lvsegs(lv_t lv);
 lv_t lvm_lv_from_name(vg_t vg, const char *name);
 
 /**
+ * Lookup an LV handle in a VG by the LV uuid.
+ * The form of the uuid may be either the formatted, human-readable form,
+ * or the non-formatted form.
+ *
+ * \memberof lv_t
+ *
+ * \param   vg
+ * VG handle obtained from lvm_vg_create() or lvm_vg_open().
+ *
+ * \param   uuid
+ * UUID of LV to lookup.
+ *
+ * \return
+ * non-NULL handle to the LV with 'uuid' attached to the VG.
+ * NULL is returned if the LV uuid is not associated with the VG handle.
+ */
+lv_t lvm_lv_from_uuid(vg_t vg, const char *uuid);
+
+/**
  * Activate a logical volume.
  *
  * \memberof lv_t
@@ -1523,6 +1542,25 @@ struct dm_list *lvm_pv_list_pvsegs(pv_t pv);
 pv_t lvm_pv_from_name(vg_t vg, const char *name);
 
 /**
+ * Lookup an PV handle in a VG by the PV uuid.
+ * The form of the uuid may be either the formatted, human-readable form,
+ * or the non-formatted form.
+ *
+ * \memberof pv_t
+ *
+ * \param   vg
+ * VG handle obtained from lvm_vg_create() or lvm_vg_open().
+ *
+ * \param   uuid
+ * UUID of PV to lookup.
+ *
+ * \return
+ * non-NULL handle to the PV with 'uuid' attached to the VG.
+ * NULL is returned if the PV uuid is not associated with the VG handle.
+ */
+pv_t lvm_pv_from_uuid(vg_t vg, const char *uuid);
+
+/**
  * Resize physical volume to new_size bytes.
  *
  * \memberof pv_t
liblvm/lvm_lv.c +22 -0
index 5e8322f..85d536c 100644
@@ -260,6 +260,28 @@ lv_t lvm_lv_from_name(vg_t vg, const char *name)
 	return NULL;
 }
 
+lv_t lvm_lv_from_uuid(vg_t vg, const char *uuid)
+{
+	struct lv_list *lvl;
+	struct id id;
+
+	if (strlen(uuid) < ID_LEN) {
+		log_errno (EINVAL, "Invalid UUID string length");
+		return NULL;
+	}
+	if (strlen(uuid) >= ID_LEN) {
+		if (!id_read_format(&id, uuid)) {
+			log_errno(EINVAL, "Invalid UUID format");
+			return NULL;
+		}
+	}
+	dm_list_iterate_items(lvl, &vg->lvs) {
+		if (id_equal(&vg->id, &lvl->lv->lvid.id[0]) &&
+		    id_equal(&id, &lvl->lv->lvid.id[1]))
+			return lvl->lv;
+	}
+	return NULL;
+}
 int lvm_lv_resize(const lv_t lv, uint64_t new_size)
 {
 	/* FIXME: add lv resize code here */
liblvm/lvm_pv.c +22 -0
index 3372fde..5f2014d 100644
@@ -98,6 +98,28 @@ pv_t lvm_pv_from_name(vg_t vg, const char *name)
 	return NULL;
 }
 
+pv_t lvm_pv_from_uuid(vg_t vg, const char *uuid)
+{
+	struct pv_list *pvl;
+	struct id id;
+
+	if (strlen(uuid) < ID_LEN) {
+		log_errno (EINVAL, "Invalid UUID string length");
+		return NULL;
+	}
+	if (strlen(uuid) >= ID_LEN) {
+		if (!id_read_format(&id, uuid)) {
+			log_errno(EINVAL, "Invalid UUID format");
+			return NULL;
+		}
+	}
+	dm_list_iterate_items(pvl, &vg->pvs) {
+		if (id_equal(&id, &pvl->pv->id))
+			return pvl->pv;
+	}
+	return NULL;
+}
+
 
 int lvm_pv_resize(const pv_t pv, uint64_t new_size)
 {
Signature · 1 line
1.7.2.2
#3 lore raw

PATCH 3/3 Add interactive tests for functions to lookup a pv|lv by name|uuid.

In reply to Dave Wysochanski
---
 test/api/test.c |  107 ++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 95 insertions(+), 12 deletions(-)
test/api/test.c +95 -12
index f19268f..434baea 100644
@@ -113,6 +113,14 @@ static void _show_help(void)
 	       "Lookup a vgname from a device name\n");
 	printf("'vgname_from_pvid pvid': "
 	       "Lookup a vgname from a pvid\n");
+	printf("'lv_from_uuid vgname lvuuid': "
+	       "Lookup an LV from an LV uuid\n");
+	printf("'lv_from_name vgname lvname': "
+	       "Lookup an LV from an LV name\n");
+	printf("'pv_from_uuid vgname pvuuid': "
+	       "Lookup an LV from an LV uuid\n");
+	printf("'pv_from_name vgname pvname': "
+	       "Lookup an LV from an LV name\n");
 	printf("'quit': exit the program\n");
 }
 
@@ -468,6 +476,30 @@ static void _show_one_vg(vg_t vg)
 		lvm_vg_get_pv_count(vg), lvm_vg_get_seqno(vg));
 }
 
+static void _print_pv(pv_t pv)
+{
+	if (!pv)
+		return;
+	printf("%s (%s): size=%"PRIu64", free=%"PRIu64
+	       ", dev_size=%"PRIu64", mda_count=%"PRIu64"\n",
+	       lvm_pv_get_name(pv), lvm_pv_get_uuid(pv),
+	       lvm_pv_get_size(pv), lvm_pv_get_free(pv),
+	       lvm_pv_get_dev_size(pv),
+	       lvm_pv_get_mda_count(pv));
+}
+
+static void _print_lv(vg_t vg, lv_t lv)
+{
+	if (!lv)
+		return;
+	printf("%s/%s (%s): size=%"PRIu64", %sACTIVE / %sSUSPENDED\n",
+	       lvm_vg_get_name(vg),
+	       lvm_lv_get_name(lv), lvm_lv_get_uuid(lv),
+	       lvm_lv_get_size(lv),
+	       lvm_lv_is_active(lv) ? "" : "IN",
+	       lvm_lv_is_suspended(lv) ? "" : "NOT ");
+}
+
 static void _list_open_vgs(void)
 {
 	dm_hash_iter(_vgid_hash, (dm_hash_iterate_fn) _show_one_vg);
@@ -488,12 +520,7 @@ static void _pvs_in_vg(char **argv, int argc)
 	}
 	printf("PVs in VG %s:\n", lvm_vg_get_name(vg));
 	dm_list_iterate_items(pvl, pvs) {
-		printf("%s (%s): size=%"PRIu64", free=%"PRIu64
-			", dev_size=%"PRIu64", mda_count=%"PRIu64"\n",
-			lvm_pv_get_name(pvl->pv), lvm_pv_get_uuid(pvl->pv),
-			lvm_pv_get_size(pvl->pv), lvm_pv_get_free(pvl->pv),
-			lvm_pv_get_dev_size(pvl->pv),
-			lvm_pv_get_mda_count(pvl->pv));
+		_print_pv(pvl->pv);
 	}
 }
 
@@ -715,6 +742,59 @@ static void _lv_tag(char **argv, int argc, int add)
 	printf("%s tag %s to LV %s\n",
 	       add ? "adding":"removing", argv[3], argv[2]);
 }
+
+static void _lv_from_uuid(char **argv, int argc)
+{
+	vg_t vg;
+
+	if (argc < 3) {
+		printf("Please enter vgname, lv_uuid\n");
+		return;
+	}
+	if (!(vg = _lookup_vg_by_name(argv, argc)))
+		return;
+	_print_lv(vg, lvm_lv_from_uuid(vg, argv[2]));
+}
+
+static void _lv_from_name(char **argv, int argc)
+{
+	vg_t vg;
+
+	if (argc < 3) {
+		printf("Please enter vgname, lv_uuid\n");
+		return;
+	}
+	if (!(vg = _lookup_vg_by_name(argv, argc)))
+		return;
+	_print_lv(vg, lvm_lv_from_name(vg, argv[2]));
+}
+
+static void _pv_from_uuid(char **argv, int argc)
+{
+	vg_t vg;
+
+	if (argc < 3) {
+		printf("Please enter vgname, pv_uuid\n");
+		return;
+	}
+	if (!(vg = _lookup_vg_by_name(argv, argc)))
+		return;
+	_print_pv(lvm_pv_from_uuid(vg, argv[2]));
+}
+
+static void _pv_from_name(char **argv, int argc)
+{
+	vg_t vg;
+
+	if (argc < 3) {
+		printf("Please enter vgname, pv_uuid\n");
+		return;
+	}
+	if (!(vg = _lookup_vg_by_name(argv, argc)))
+		return;
+	_print_pv(lvm_pv_from_name(vg, argv[2]));
+}
+
 static void _vgname_from_pvid(char **argv, int argc, lvm_t libh)
 {
 	const char *vgname;
@@ -762,12 +842,7 @@ static void _lvs_in_vg(char **argv, int argc)
 	}
 	printf("LVs in VG %s:\n", lvm_vg_get_name(vg));
 	dm_list_iterate_items(lvl, lvs) {
-		printf("%s/%s (%s): size=%"PRIu64", %sACTIVE / %sSUSPENDED\n",
-			lvm_vg_get_name(vg),
-			lvm_lv_get_name(lvl->lv), lvm_lv_get_uuid(lvl->lv),
-			lvm_lv_get_size(lvl->lv),
-			lvm_lv_is_active(lvl->lv) ? "" : "IN",
-			lvm_lv_is_suspended(lvl->lv) ? "" : "NOT ");
+		_print_lv(vg, lvl->lv);
 	}
 }
 
@@ -978,6 +1053,14 @@ static int lvmapi_test_shell(lvm_t libh)
 			_vgname_from_devname(argv, argc, libh);
 		} else if (!strcmp(argv[0], "vgname_from_pvid")) {
 			_vgname_from_pvid(argv, argc, libh);
+		} else if (!strcmp(argv[0], "lv_from_uuid")) {
+			_lv_from_uuid(argv, argc);
+		} else if (!strcmp(argv[0], "lv_from_name")) {
+			_lv_from_name(argv, argc);
+		} else if (!strcmp(argv[0], "pv_from_uuid")) {
+			_pv_from_uuid(argv, argc);
+		} else if (!strcmp(argv[0], "pv_from_name")) {
+			_pv_from_name(argv, argc);
 		} else {
 			printf ("Unrecognized command %s\n", argv[0]);
 		}
Signature · 1 line
1.7.2.2