Add generic attribute function for vg attributes, v1.
cover Add generic attribute function for vg attributes, v1.
This patchset is a rebase/update of previous patches posted for a generic attribute function for the reporting infrastructure and liblvm. I'm not entirely happy with this approach.
Still TODO:
1. Documentation of the field names and descriptions. This seems necessary as they are no longer documented in the function name, and requiring the user to go to the man page and the help output of the vgs/pvs/lvs commands seems unreasonable.
2. A similar function for PV and LV.
PATCH 1/4 Add dm_report_get_field_value() function and supporting structure.
We need to extend the existing reporting infrastructure so that individual field values may be queried. This function will be used by lvm2app generic attribute functions.
--- libdm/libdevmapper.h | 12 ++++++++++++ libdm/libdm-report.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 0 deletions(-)
libdm/libdevmapper.h +12 -0
@@ -1055,6 +1055,18 @@ struct dm_report_field_type {const char *desc;/* description of the field */ }; +struct dm_report_field_value_type { +unsigned is_string; +union { +const char *s_val; +uint64_t n_val; + } u; +}; + +int dm_report_get_field_value (struct dm_report *rh, +const char *field_id, +struct dm_report_field_value_type *value); +/* * dm_report_init output_flags */
libdm/libdm-report.c +30 -0
@@ -1092,3 +1092,33 @@ int dm_report_output(struct dm_report *rh)else return _output_as_columns (rh); } + +int dm_report_get_field_value (struct dm_report *rh, +const char *field_id, +struct dm_report_field_value_type *value) +{ +struct dm_report_field *field; +struct row *row; +unsigned found=0 ; + + dm_list_iterate_items(row, &rh->rows) { +if ((field =dm_list_item (dm_list_first (&row->fields), + struct dm_report_field))) { +if (!_field_match (rh, field_id,strlen (field_id),0 )) +continue ; + found =1 ; +if ((field->props->flags &DM_REPORT_FIELD_TYPE_MASK ) == +DM_REPORT_FIELD_TYPE_STRING ) { + value->is_string =1 ; + value->u.s_val = field->sort_value; + }else { + value->is_string =0 ; + value->u.n_val = *(const uint64_t *)field->sort_value; + } +dm_list_del (&field->list); + } + } + +return found; + +}
Signature · 1 line
1.6.0.6
PATCH 2/4 Add report_value() function to return a value of a report field.
Call into dm_report_get_field_value() to query the value of a single field in a report.
--- lib/report/report.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/report/report.h | 7 +++++ 2 files changed, 72 insertions(+), 0 deletions(-)
lib/report/report.c +65 -0
@@ -1299,3 +1299,68 @@ int report_object(void *handle, struct volume_group *vg,return dm_report_object (handle, &obj); } + +int report_value (struct volume_group *vg, +struct logical_volume *lv, +struct physical_volume *pv, +struct lv_segment *seg, +struct pv_segment *pvseg, +const char *field_id, +struct dm_report_field_value_type *value) +{ +void *rh; +report_type_t report_type =VGS ; +struct dm_pool *mem; +struct cmd_context *cmd; + +if (vg) { + mem = vg->vgmem; + cmd = vg->cmd; + }else if (lv) { + mem = lv->vg->vgmem; + cmd = lv->vg->cmd; + }else if (pv) { + mem = pv->vg->vgmem; + cmd = pv->vg->cmd; + }else if (seg) { + mem = seg->lv->vg->vgmem; + cmd = seg->lv->vg->cmd; + }else if (pvseg) { + mem = pvseg->pv->vg->vgmem; + cmd = pvseg->pv->vg->cmd; + }else { + stack; +return 0 ; + } + +if (!(rh =report_init (cmd, field_id,"" , &report_type, +" " ,1 ,1 ,1 ,0 ,0 ,0 ))) { + stack; +return 0 ; + } + +if (!report_object (rh, vg, lv, pv, seg, pvseg)) { +dm_report_free (rh); + stack; +return 0 ; + } + +if (!dm_report_get_field_value (rh, field_id, value)) { +dm_report_free (rh); + stack; +return 0 ; + } + +if (!value->is_string) { +dm_report_free (rh); +return 1 ; + } + +if (!(value->u.s_val =dm_pool_strdup (mem, value->u.s_val))) { +log_errno (ENOMEM ,"Memory allocation fail for attribute value" ); +dm_report_free (rh); +return 0 ; + } +dm_report_free (rh); +return 1 ; +}
lib/report/report.h +7 -0
@@ -41,6 +41,13 @@ void report_free(void *handle);int report_object (void *handle,struct volume_group *vg,struct logical_volume *lv,struct physical_volume *pv,struct lv_segment *seg,struct pv_segment *pvseg); +int report_value (struct volume_group *vg, +struct logical_volume *lv, +struct physical_volume *pv, +struct lv_segment *seg, +struct pv_segment *pvseg, +const char *field_id, +struct dm_report_field_value_type *value);int report_output (void *handle);#endif
Signature · 1 line
1.6.0.6
PATCH 3/4 Add lvm_vg_get_attr_value() generic vg attibute function.
Call into lvm internal report_value() function to return a single field value.
--- liblvm/lvm2app.h | 26 ++++++++++++++++++++++++++ liblvm/lvm_vg.c | 8 ++++++++ 2 files changed, 34 insertions(+), 0 deletions(-)
liblvm/lvm2app.h +26 -0
@@ -848,6 +848,32 @@ uint64_t lvm_vg_get_max_lv(const vg_t vg);*/ struct dm_list *lvm_vg_get_tags (const vg_t vg); +/** +* Get the value of the VG attribute 'attr_name'. +* +* \memberof vg_t +* +* The memory allocated for a string attribute value is tied to the vg_t +* handle and will be released when lvm_vg_close() is called. +* +* Example: +* dm_report_field_value_type value; +* +* if (lvm_vg_get_attr_value(vg, "vg_mda_count", &value) < 0) { +* // handle error +* } +* if (value.is_string) +* printf(", value = %s\n", value.u.s_val); +* else +* printf(", value = %"PRIu64"\n", value.u.n_val); +* +* +* \return +* 0 (success) or -1 (failure). +*/ +int lvm_vg_get_attr_value (vg_t vg,const char *attr_name, +struct dm_report_field_value_type *value); +/************************** logical volume handling *************************/ /**
liblvm/lvm_vg.c +8 -0
@@ -20,6 +20,7 @@#include "locking.h" #include "lvmcache.h" #include "lvm_misc.h" +#include "report.h" int lvm_vg_add_tag (vg_t vg,const char *tag) {
@@ -341,6 +342,13 @@ const char *lvm_vg_get_name(const vg_t vg)return dm_pool_strndup (vg->vgmem, (const char *)vg->name,NAME_LEN +1 ); } +int lvm_vg_get_attr_value (vg_t vg,const char *attr_name, +struct dm_report_field_value_type *value) +{ +return !report_value (vg,NULL ,NULL ,NULL ,NULL , + attr_name, value); +} +struct dm_list *lvm_list_vg_names (lvm_t libh) {return get_vgnames ((struct cmd_context *)libh,0 );
Signature · 1 line
1.6.0.6
PATCH 4/4 Add tests for lvm_vg_get_attr_value().
--- test/api/test.c | 34 ++++++++++++++++++++++++++++++++++ 1 files changed, 34 insertions(+), 0 deletions(-)
test/api/test.c +34 -0
@@ -18,6 +18,7 @@#include <readline/readline.h> #include "lvm2app.h" +#include "libdevmapper.h" #define MAX_ARGS 64
@@ -91,6 +92,8 @@ static void _show_help(void) "Issue a lvm_config_override() with accept device filter\n");printf ("'vg_get_tags vgname': " "List the tags of a VG\n" ); +printf ("'vg_get_attr_value vgname attr_name': " +"Display the value of VG attribute\n" );printf ("'lv_get_tags vgname lvname': " "List the tags of a LV\n" ); printf("'vg_{add|remove}_tag vgname tag': "
@@ -546,6 +549,35 @@ static void _vg_tag(char **argv, int argc, int add) add ? "adding":"removing", argv[2], argv[1]); } +static void _vg_get_attr_value (char **argv,int argc) +{ +vg_t vg; +struct dm_report_field_value_type value; +int rc; + +if (argc <3 ) { +printf ("Please enter vgname, field_id\n" ); +return ; + } +if (!(vg =_lookup_vg_by_name (argv, argc))) +return ; + rc =lvm_vg_get_attr_value (vg, argv[2 ], &value); +if (rc) +printf ("Error " ); +else +printf ("Success " ); +printf ("Obtaining value of field %s in VG %s" , + argv[2 ], argv[1 ]); +if (rc) { +printf ("\n" ); +return ; + } +if (value.is_string) +printf (", value = %s\n" , value.u.s_val); +else +printf (", value = %" PRIu64"\n" , value.u.n_val); +} +static void _lv_get_tags (char **argv,int argc) {lv_t lv;
@@ -796,6 +828,8 @@ static int lvmapi_test_shell(lvm_t libh)_vg_tag (argv, argc,0 ); }else if (!strcmp (argv[0 ],"vg_get_tags" )) {_vg_get_tags (argv, argc); + }else if (!strcmp (argv[0 ],"vg_get_attr_value" )) { +_vg_get_attr_value (argv, argc); }else if (!strcmp (argv[0 ],"lv_add_tag" )) {_lv_tag (argv, argc,1 ); }else if (!strcmp (argv[0 ],"lv_remove_tag" )) {
Signature · 1 line
1.6.0.6