Skip to contentSkip to conversation

Initial proof of concept code for libLVM vg attribute name enumeration.

RFC PATCH In review Dave Wysochanski 1 participant 4 messages Jan 20, 2009 – Jan 21, 2009 Library code

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 Initial proof of concept code for libLVM vg attribute name enumeration.

Main code is in libdm/libdevmapper.h, libdm/libdm-report.c, and lib/report/report.c. Other code was added just to get it to compile and is not the primary focus of this patch. The test/api/test.c file allows for a simple test as follows:
./test
lvm> vg_read vg1
Success reading vg vg1
lvm> vg_get_attr_list
VG attribute names:
vg_fmt
vg_uuid
vg_name
vg_attr
vg_size
vg_free
vg_sysid
vg_extent_size
vg_extent_count
vg_free_count
...

---
 lib/lvm2.h                       |   16 ++++++++++++++
 lib/metadata/metadata-exported.h |    2 +-
 lib/metadata/metadata.c          |    6 +++++
 lib/misc/lib.h                   |    1 +
 lib/report/report.c              |   28 ++++++++++++++++++++++++
 libdm/.exported_symbols          |    1 +
 libdm/libdevmapper.h             |   12 ++++++++++
 libdm/libdm-report.c             |   27 +++++++++++++++++++++++
 test/api/test.c                  |   43 ++++++++++++++++++++++++++++++++-----
 9 files changed, 129 insertions(+), 7 deletions(-)
lib/lvm2.h +16 -0
index b18cead..6689b37 100644
@@ -16,6 +16,7 @@
 #define _LIB_LVM2_H
 
 #include <stdint.h>
+#include "libdevmapper.h"
 
 /*
  * Library Initialisation
@@ -50,4 +51,19 @@ void lvm2_destroy(lvm_handle_t h);
  */
 #define lvm2_destroy(X) destroy_toolcontext(X)
 
+/* FIXME: this should be in the create/init routine(s) */
+int init_locking(int type, struct cmd_context *cmd);
+
+struct volume_group;
+typedef struct volume_group vg_t;
+
+/* FIXME: temporary simplistic vg_read interface */
+vg_t *vg_open_for_read(struct cmd_context *cmd, const char *vg_name);
+
+/*
+ * Get a list of VG attributes we can query.
+ * Returns a list of struct dm_report_field_ids_type 
+ */
+int vg_get_attr_list(vg_t *vg, struct dm_list *list);
+
 #endif
lib/metadata/metadata-exported.h +1 -1
index 40ece6f..bf8555f 100644
@@ -22,11 +22,11 @@
 #define _LVM_METADATA_EXPORTED_H
 
 #include "uuid.h"
+#include "lvm2.h"
 
 struct physical_volume;
 typedef struct physical_volume pv_t;
 struct volume_group;
-typedef struct volume_group vg_t;
 
 struct logical_volume;
 
lib/metadata/metadata.c +6 -0
index a49c4d8..19ba8f3 100644
@@ -2461,6 +2461,12 @@ vg_t *vg_lock_and_read(struct cmd_context *cmd, const char *vg_name,
 	return vg;
 }
 
+vg_t *vg_open_for_read(struct cmd_context *cmd, const char *vg_name)
+{
+	return vg_lock_and_read(cmd, vg_name, NULL, LCK_VG_READ,
+				CLUSTERED, 0);
+}
+
 /*
  * Gets/Sets for external LVM library
  */
lib/misc/lib.h +1 -0
index 100827d..188f79e 100644
@@ -31,6 +31,7 @@
 #include "lvm-wrappers.h"
 #include "lvm-types.h"
 #include "util.h"
+#include "lvm2.h"
 
 #ifdef DM
 #  include "dm-logging.h"
lib/report/report.c +28 -0
index 8439b2b..edd3453 100644
@@ -1191,3 +1191,31 @@ int report_object(void *handle, struct volume_group *vg,
 
 	return dm_report_object(handle, &obj);
 }
+
+/*
+ * Get a list of attributes for a vg
+ */
+int vg_get_attr_list(vg_t *vg, struct dm_list *list)
+{
+	void *rh;
+	report_type_t report_type = VGS;
+
+	dm_list_init(list);
+
+	/*
+	 * Create a report so we can return the headings.
+	 */
+	if (!(rh = report_init(vg->cmd, "all", "", &report_type,
+			       " ", 1, 1, 1, 0, 0, 0))) {
+		stack;
+		return 0;
+	}
+
+	if (!dm_report_get_field_ids(rh, report_type, list)) {
+		dm_report_free(rh);
+		return 0;
+	}
+
+	dm_report_free(rh);
+	return 1;
+}
libdm/.exported_symbols +1 -0
index a6e04f8..aa70f29 100644
@@ -132,6 +132,7 @@ dm_report_field_int32
 dm_report_field_uint32
 dm_report_field_uint64
 dm_report_field_set_value
+dm_report_get_field_ids
 dm_report_set_output_field_name_prefix
 dm_regex_create
 dm_regex_match
libdm/libdevmapper.h +12 -0
index 93aecbb..cc8302a 100644
@@ -924,6 +924,17 @@ struct dm_report_field_type {
 	const char *desc;	/* description of the field */
 };
 
+struct dm_report_field_ids_type {
+	struct dm_list list;
+	const char *id;
+};
+
+/*
+ * Returns a list of all field ids for a specific report type.
+ */
+int dm_report_get_field_ids(struct dm_report *rh, uint32_t type,
+			    struct dm_list *list);
+
 /*
  * dm_report_init output_flags
  */
@@ -947,6 +958,7 @@ int dm_report_object(struct dm_report *rh, void *object);
 int dm_report_output(struct dm_report *rh);
 void dm_report_free(struct dm_report *rh);
 
+
 /*
  * Prefix added to each field name with DM_REPORT_OUTPUT_FIELD_NAME_PREFIX
  */
libdm/libdm-report.c +27 -0
index 0c5ee6d..a9db211 100644
@@ -1076,3 +1076,30 @@ int dm_report_output(struct dm_report *rh)
 	else
 		return _output_as_columns(rh);
 }
+
+/*
+ * Return a list of field IDs (names) for a specified report type.
+ */
+int dm_report_get_field_ids(struct dm_report *rh, uint32_t type,
+			    struct dm_list *list)
+{
+	uint32_t f;
+	struct dm_report_field_ids_type *field_id;
+
+	for (f = 0; rh->fields[f].report_fn; f++) {
+		if (rh->fields[f].type != type)
+			continue;
+
+		if (!(field_id = dm_pool_zalloc(rh->mem, sizeof(*field_id)))) {
+			log_error("dm_report_get_field_ids: struct "
+				  "dm_report_field_ids_type allocation failed");
+			return 0;
+		}
+		if (!(field_id->id = dm_pool_strdup(rh->mem, rh->fields[f].id))) {
+			log_error("dm_report_get_field_ids: dm_pool_strdup failed");
+			return 0;
+		}
+		dm_list_add(list, &field_id->list);
+	}
+	return 1;
+}
test/api/test.c +37 -6

63 lines not on this page · raw message

Signature · 1 line
1.5.5.1
#1 lore raw

[RFC] Add liblvm VG attribute name and value querying

The following 2 patches are my first stab at adding APIs to query VG attribute names and values, based on the existing reporting infrastructure. Feedback welcome.

#2 lore raw

PATCH 1/2 Add vg_get_attr_list() libLVM API to return a list of VG attribute names.

This API returns a list of attribute names for a VG. A later API will allow the values of the attributes to be queried. The API implementation relies on adding an exported call to libdevmapper, dm_report_get_field_ids(), which is the interface into dm's reporting infrastructure to retrieve the attribute/field names/ids.

Main code is in libdm/libdevmapper.h, libdm/libdm-report.c, and lib/report/report.c. Other supporting code was added just to get it to compile and is not the primary focus of this patch. The test/api/test.c file allows for a simple test as follows:
./test
lvm> vg_read vg1
Success reading vg vg1
lvm> vg_get_attr_list
VG attribute names:
vg_fmt
vg_uuid
vg_name
vg_attr
vg_size
vg_free
vg_sysid
vg_extent_size
vg_extent_count
vg_free_count
...

---
 lib/lvm2.h                       |   16 ++++++++++++++
 lib/metadata/metadata-exported.h |    2 +-
 lib/metadata/metadata.c          |    6 +++++
 lib/misc/lib.h                   |    1 +
 lib/report/report.c              |   28 ++++++++++++++++++++++++
 libdm/.exported_symbols          |    1 +
 libdm/libdevmapper.h             |   12 ++++++++++
 libdm/libdm-report.c             |   27 +++++++++++++++++++++++
 test/api/test.c                  |   43 ++++++++++++++++++++++++++++++++-----
 9 files changed, 129 insertions(+), 7 deletions(-)
lib/lvm2.h +16 -0
index b18cead..6689b37 100644
@@ -16,6 +16,7 @@
 #define _LIB_LVM2_H
 
 #include <stdint.h>
+#include "libdevmapper.h"
 
 /*
  * Library Initialisation
@@ -50,4 +51,19 @@ void lvm2_destroy(lvm_handle_t h);
  */
 #define lvm2_destroy(X) destroy_toolcontext(X)
 
+/* FIXME: this should be in the create/init routine(s) */
+int init_locking(int type, struct cmd_context *cmd);
+
+struct volume_group;
+typedef struct volume_group vg_t;
+
+/* FIXME: temporary simplistic vg_read interface */
+vg_t *vg_open_for_read(struct cmd_context *cmd, const char *vg_name);
+
+/*
+ * Get a list of VG attributes we can query.
+ * Returns a list of struct dm_report_field_ids_type 
+ */
+int vg_get_attr_list(vg_t *vg, struct dm_list *list);
+
 #endif
lib/metadata/metadata-exported.h +1 -1
index 40ece6f..bf8555f 100644
@@ -22,11 +22,11 @@
 #define _LVM_METADATA_EXPORTED_H
 
 #include "uuid.h"
+#include "lvm2.h"
 
 struct physical_volume;
 typedef struct physical_volume pv_t;
 struct volume_group;
-typedef struct volume_group vg_t;
 
 struct logical_volume;
 
lib/metadata/metadata.c +6 -0
index a49c4d8..19ba8f3 100644
@@ -2461,6 +2461,12 @@ vg_t *vg_lock_and_read(struct cmd_context *cmd, const char *vg_name,
 	return vg;
 }
 
+vg_t *vg_open_for_read(struct cmd_context *cmd, const char *vg_name)
+{
+	return vg_lock_and_read(cmd, vg_name, NULL, LCK_VG_READ,
+				CLUSTERED, 0);
+}
+
 /*
  * Gets/Sets for external LVM library
  */
lib/misc/lib.h +1 -0
index 100827d..188f79e 100644
@@ -31,6 +31,7 @@
 #include "lvm-wrappers.h"
 #include "lvm-types.h"
 #include "util.h"
+#include "lvm2.h"
 
 #ifdef DM
 #  include "dm-logging.h"
lib/report/report.c +28 -0
index 8439b2b..edd3453 100644
@@ -1191,3 +1191,31 @@ int report_object(void *handle, struct volume_group *vg,
 
 	return dm_report_object(handle, &obj);
 }
+
+/*
+ * Get a list of attributes for a vg
+ */
+int vg_get_attr_list(vg_t *vg, struct dm_list *list)
+{
+	void *rh;
+	report_type_t report_type = VGS;
+
+	dm_list_init(list);
+
+	/*
+	 * Create a report so we can return the headings.
+	 */
+	if (!(rh = report_init(vg->cmd, "all", "", &report_type,
+			       " ", 1, 1, 1, 0, 0, 0))) {
+		stack;
+		return 0;
+	}
+
+	if (!dm_report_get_field_ids(rh, report_type, list)) {
+		dm_report_free(rh);
+		return 0;
+	}
+
+	dm_report_free(rh);
+	return 1;
+}
libdm/.exported_symbols +1 -0
index a6e04f8..aa70f29 100644
@@ -132,6 +132,7 @@ dm_report_field_int32
 dm_report_field_uint32
 dm_report_field_uint64
 dm_report_field_set_value
+dm_report_get_field_ids
 dm_report_set_output_field_name_prefix
 dm_regex_create
 dm_regex_match
libdm/libdevmapper.h +12 -0
index 93aecbb..cc8302a 100644
@@ -924,6 +924,17 @@ struct dm_report_field_type {
 	const char *desc;	/* description of the field */
 };
 
+struct dm_report_field_ids_type {
+	struct dm_list list;
+	const char *id;
+};
+
+/*
+ * Returns a list of all field ids for a specific report type.
+ */
+int dm_report_get_field_ids(struct dm_report *rh, uint32_t type,
+			    struct dm_list *list);
+
 /*
  * dm_report_init output_flags
  */
@@ -947,6 +958,7 @@ int dm_report_object(struct dm_report *rh, void *object);
 int dm_report_output(struct dm_report *rh);
 void dm_report_free(struct dm_report *rh);
 
+
 /*
  * Prefix added to each field name with DM_REPORT_OUTPUT_FIELD_NAME_PREFIX
  */
libdm/libdm-report.c +27 -0
index 0c5ee6d..a9db211 100644
@@ -1076,3 +1076,30 @@ int dm_report_output(struct dm_report *rh)
 	else
 		return _output_as_columns(rh);
 }
+
+/*
+ * Return a list of field IDs (names) for a specified report type.
+ */
+int dm_report_get_field_ids(struct dm_report *rh, uint32_t type,
+			    struct dm_list *list)
+{
+	uint32_t f;
+	struct dm_report_field_ids_type *field_id;
+
+	for (f = 0; rh->fields[f].report_fn; f++) {
+		if (rh->fields[f].type != type)
+			continue;
+
+		if (!(field_id = dm_pool_zalloc(rh->mem, sizeof(*field_id)))) {
+			log_error("dm_report_get_field_ids: struct "
+				  "dm_report_field_ids_type allocation failed");
+			return 0;
+		}
+		if (!(field_id->id = dm_pool_strdup(rh->mem, rh->fields[f].id))) {
+			log_error("dm_report_get_field_ids: dm_pool_strdup failed");
+			return 0;
+		}
+		dm_list_add(list, &field_id->list);
+	}
+	return 1;
+}
test/api/test.c +37 -6

63 lines not on this page · raw message

Signature · 1 line
1.5.5.1
#3 lore raw

PATCH 2/2 Add vg_get_attr_value() libLVM API to query to value of a VG attribute.

This API may be used independently to query the value of a VG attribute, provided the name of the attribute is known. If the name of the attribute(s) are unknown, the previous API, vg_get_attr_list(), may be used.

Simple test code included. Run as follows:
./test
lvm> vg_read vg1
Success reading vg vg1
lvm> vg_get_attr_list
VG attribute names:
vg_fmt
vg_uuid
vg_name
vg_attr
vg_size
vg_free
vg_sysid
vg_extent_size
vg_extent_count
vg_free_count
max_lv
max_pv
pv_count
lv_count
snap_count
vg_seqno
vg_tags
vg_mda_count
vg_mda_free
vg_mda_size
lvm> vg_get_attr_value vg_name pv_count
vg_name = vg1
pv_count = 3

---
 lib/lvm2.h              |    2 +
 lib/report/report.c     |   28 +++++++++++++++++++++++++++
 libdm/.exported_symbols |    1 +
 libdm/libdevmapper.h    |   12 +++++++++++
 libdm/libdm-report.c    |   29 ++++++++++++++++++++++++++++
 test/api/test.c         |   48 ++++++++++++++++++++++++++++++++++++++++++----
 6 files changed, 115 insertions(+), 5 deletions(-)
lib/lvm2.h +2 -0
index 6689b37..4402eeb 100644
@@ -66,4 +66,6 @@ vg_t *vg_open_for_read(struct cmd_context *cmd, const char *vg_name);
  */
 int vg_get_attr_list(vg_t *vg, struct dm_list *list);
 
+int vg_get_attr_value(vg_t *vg, const char *attr_name,
+		      struct dm_report_field_value_type *value);
 #endif
lib/report/report.c +28 -0
index edd3453..6d9e83b 100644
@@ -1219,3 +1219,31 @@ int vg_get_attr_list(vg_t *vg, struct dm_list *list)
 	dm_report_free(rh);
 	return 1;
 }
+
+/*
+ * Get the value of the VG attribute 'attr_name'.
+ */
+int vg_get_attr_value(vg_t *vg, const char *attr_name,
+		      struct dm_report_field_value_type *value)
+{
+	void *rh;
+	report_type_t report_type = VGS;
+
+	if (!(rh = report_init(vg->cmd, attr_name, "", &report_type,
+			       " ", 1, 1, 1, 0, 0, 0))) {
+		stack;
+		return 0;
+	}
+
+	if (!report_object(rh, vg, NULL, NULL, NULL, NULL)) {
+		stack;
+		return 0;
+	}
+	
+	if (!dm_report_get_field_value(rh, value)) {
+		stack;
+		return 0;
+	}
+	dm_report_free(rh);
+	return 1;
+}
libdm/.exported_symbols +1 -0
index aa70f29..e7a4906 100644
@@ -133,6 +133,7 @@ dm_report_field_uint32
 dm_report_field_uint64
 dm_report_field_set_value
 dm_report_get_field_ids
+dm_report_get_field_value
 dm_report_set_output_field_name_prefix
 dm_regex_create
 dm_regex_match
libdm/libdevmapper.h +12 -0
index cc8302a..d6cb7cf 100644
@@ -927,6 +927,15 @@ struct dm_report_field_type {
 struct dm_report_field_ids_type {
 	struct dm_list list;
 	const char *id;
+	uint32_t type; /* FIXME: unnecessary ?*/
+};
+
+struct dm_report_field_value_type {
+	unsigned is_string;
+	union {
+		char *s_val;
+		uint64_t n_val;
+	} u;
 };
 
 /*
@@ -935,6 +944,9 @@ struct dm_report_field_ids_type {
 int dm_report_get_field_ids(struct dm_report *rh, uint32_t type,
 			    struct dm_list *list);
 
+int dm_report_get_field_value(struct dm_report *rh,
+			      struct dm_report_field_value_type *value);
+
 /*
  * dm_report_init output_flags
  */
libdm/libdm-report.c +29 -0
index a9db211..625e212 100644
@@ -1099,7 +1099,36 @@ int dm_report_get_field_ids(struct dm_report *rh, uint32_t type,
 			log_error("dm_report_get_field_ids: dm_pool_strdup failed");
 			return 0;
 		}
+		field_id->type = type;
 		dm_list_add(list, &field_id->list);
 	}
 	return 1;
 }
+
+
+int dm_report_get_field_value(struct dm_report *rh,
+			      struct dm_report_field_value_type *value)
+{
+	struct dm_report_field *field;
+	struct row *row;
+
+	dm_list_iterate_items(row, &rh->rows) {
+		if ((field = dm_list_item(dm_list_first(&row->fields),
+					  struct dm_report_field))) {
+			if ((field->props->flags & DM_REPORT_FIELD_TYPE_MASK) ==
+			    DM_REPORT_FIELD_TYPE_STRING) {
+				value->is_string = 1;
+				value->u.s_val = dm_pool_strdup(rh->mem,
+								field->sort_value);
+			} else {
+				value->is_string = 0;
+				value->u.n_val = *(const uint64_t *)field->sort_value;
+			}
+			dm_list_del(&field->list);
+		}
+		
+	}
+
+	return 1;
+
+}
test/api/test.c +43 -5

84 lines not on this page · raw message

Signature · 1 line
1.5.5.1