Skip to contentSkip to conversation

: liblvm object attribute baseline

PATCH New Dave Wysochanski 1 participant 10 messages Apr 29, 2009

lore
Share & tools
lore
#0 lore raw

cover : liblvm object attribute baseline

The following 8 patches implement a baseline for liblvm objects and their attributes. The primary focus/goal of these patches are:
1. Define / clarify the main liblvm objects and their handles
2. Get the object properties for a specific handle

Together with the handles, the 'get' functions will provide equivalent functionality to the reporting commands (pvs, vgs, lvs) while keeping with the object model of liblvm.

#1 lore raw

PATCH 1/8 Define handles to liblvm objects for pv, vg, lv, lvseg, pvseg.

Define the 5 main liblvm objects to be the pv, vg, lv, lvseg, and pvseg. We need handles defined to all these objects in order for liblvm to be equivalent to the reporting commands pvs, vgs, and lvs.

- move vg_t, lv_t, and pv_t from metadata-exported.h into lvm.h
- move lv_segment and pv_segment forward declarations into lvm.h
- add lvseg_t and pvseg_t to lvm.h

NOTE: We currently have an inconsistency in handle definitions. lvm_t is defined as a pointer, while these other handles are just structures. We should pick one scheme and be consistent - perhaps define all handles as pointers (this is what I've seen elsewhere).

---
 lib/metadata/metadata-exported.h |   11 +----------
 liblvm/lvm.h                     |   15 +++++++++++++++
 2 files changed, 16 insertions(+), 10 deletions(-)
lib/metadata/metadata-exported.h +1 -10
index 3b13e37..6f5d73b 100644
@@ -22,16 +22,7 @@
 #define _LVM_METADATA_EXPORTED_H
 
 #include "uuid.h"
-
-struct physical_volume;
-typedef struct physical_volume pv_t;
-struct volume_group;
-typedef struct volume_group vg_t;
-
-struct logical_volume;
-
-struct lv_segment;
-struct pv_segment;
+#include "lvm.h"
 
 #define MAX_STRIPES 128U
 #define SECTOR_SHIFT 9L
liblvm/lvm.h +15 -0
index 3b60510..4e71e15 100644
@@ -18,6 +18,20 @@
 
 #include <stdint.h>
 
+/* Internal object structures */
+struct volume_group;
+struct physical_volume;
+struct logical_volume;
+struct lv_segment;
+struct pv_segment;
+
+/* liblvm handles to objects pv, vg, lv, pvseg, lvseg */
+typedef struct volume_group vg_t;
+typedef struct physical_volume pv_t;
+typedef struct logical_volume lv_t;
+typedef struct pv_segment pvseg_t;
+typedef struct lv_segment lvseg_t;
+
 struct lvm; /* internal data */
 
 /**
@@ -54,4 +68,5 @@ void lvm_destroy(lvm_t libh);
  */
 int lvm_reload_config(lvm_t libh);
 
+
 #endif /* _LIB_LVM_H */
Signature · 1 line
1.6.0.6
#2 lore raw

PATCH 2/8 Add lvm_object_prop.pl to generate liblvm object property function prototype

The term 'attribute' is used in some of the reporting fields (e.g. vg_attr), so we avoid it here and instead use the term 'properties' or 'prop'.

NOTE: The intent of the perl code, lvm_object_prop.pl, is to help maintain liblvm object property consistency with the reporting commands pvs, vgs, lvs As new fields get added to columns.h, we should re-run this script on the ne FIELD line(s), take the generated code, edit as appropriate, and place into appropriate liblvm code file.

This patch adds the perl file, lvm_object_prop.pl. A later patch will add the files this code generates, namely the liblvm object property function header and C file, lvm_object_prop.[ch].

---
 liblvm/lvm_object_prop.pl |  221 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 221 insertions(+), 0 deletions(-)
 create mode 100644 liblvm/lvm_object_prop.pl
liblvm/lvm_object_prop.pl new file +221 -0
new file mode 100644
index 0000000..08c492d
@@ -0,0 +1,221 @@
+#!/usr/bin/perl
+#
+# Take the colunms.h file as input and produce a header and
+# default code for liblvm object property accessor and mutator functions.
+# To run: perl lvm_object_prop.pl < ../lib/report/columns.h
+#
+# For maintenance purposes, when adding a new line to columns.h,
+# we can pipe the diff to this program to generate the new prototypes
+# and C code, then cut/paste into the proper liblvm files.
+#
+
+sub write_code_header;
+sub write_code_body;
+sub write_code_footer;
+sub write_include_header;
+sub write_include_body;
+sub write_include_footer;
+
+#
+# Open the files and write the headers
+#
+$outfile = "lvm_object_prop";
+open(INCLUDE_FILE, ">./$outfile.h") || die "Cannot open $outfile.h!\n";
+write_include_header(INCLUDE_FILE);
+open(CODE_FILE, ">./$outfile.c") || die "Cannot open $outfile.c!\n";
+write_code_header(CODE_FILE);
+
+#
+# Write the include and code body
+#
+while(<>) {
+    # Skip if line does not begin w/"FIELD" - not perfect but does the job
+    if ($_ !~ /^FIELD/) {
+	next;
+    }
+    # Pull out the actual parameters to the FIELD macro
+    m%FIELD\((.*)\)%;
+    $_ = $1;
+
+    #FIELD(LVS, lv, STR, "LV UUID", lvid.id[1], 38, uuid, "lv_uuid", "Unique identifier")
+    # NOTE: I tried using Text:CSV but could not make it work.
+    # Parse using brute force regex
+    m%(\S+),\s*(\S+),\s*(\S+),\s*(\".*\"),\s*(\S+),\s*(\S+),\s*(\S+),\s*(\".*\"),\s*(\".*\")%;
+    ($type, $strct, $sorttype, $head, $field, $width, $func, $id, $desc) =
+	    ($1, $2, $3, $4, $5, $6, $7, $8, $9);
+
+    #
+    # Fixup the id / field name / attribute name
+    #
+
+    # Unquote 'id' (field name) and description
+    $id =~ tr/"//d;
+    $desc =~ tr/"//d;
+
+    # Skip redundant fields
+    if ($id =~ /^(stripesize|regionsize|chunksize)$/) {
+        next;
+    }
+
+    # Map report type to liblvm object name
+    # - PVS, VGS, LVS reports, the liblvm object is pv/vg/lv
+    # - PVSEG, the object is pvseg
+    # - SEG, the object is lvseg
+    # - LABEL, the object is pv
+    if ($type =~ /^PVS$/) {
+	$object = "pv";
+    } elsif ($type =~ /^VGS$/) {
+	$object = "vg";
+    } elsif ($type =~ /^LVS$/) {
+	$object = "lv";
+    } elsif ($type =~ /^LABEL$/) {
+	$object = "pv";
+    } elsif ($type =~ /^SEGS$/) {
+	$object = "lvseg";
+    } elsif ($type =~ /^PVSEGS$/) {
+	$object = "pvseg";
+    } else {
+	print "Skipping type: $type, line: $_\n"; #DEBUG
+	next;
+    }
+    # Some field names/ids contain the struct name, while others don't.
+    # For consistency, if the field name contains the struct name, remove
+    # it (we always put the object name in the function name.
+    #
+    if ($id =~ /^$strct/) {
+	$id = substr($id,length($strct)+1,length($id));
+    }
+    if ($sorttype =~ m/STR/) {
+	$attr_type = "const char *";
+    } else {
+	$attr_type = "const uint64_t ";
+    }
+    # Write 'get' prototype to header and code to C file.
+    write_include_body(INCLUDE_FILE, $object, $id, $desc, $attr_type);
+    write_code_body(CODE_FILE, $object, $sorttype, $id, $desc, $attr_type);
+
+}
+
+write_include_footer(INCLUDE_FILE);
+write_code_footer(CODE_FILE);
+close(INCLUDE_FILE);
+close(CODE_FILE);
+
+sub write_include_header
+{
+    my($file) = @_;
+    print $file <<ENDHEADER;
+/*
+ * Copyright (C) 2009 Red Hat, Inc. All rights reserved.
+ *
+ * This file is part of LVM2.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License v.2.1.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef _LVM_OBJECT_PROP_H
+#define _LVM_OBJECT_PROP_H
+
+ENDHEADER
+}
+
+sub write_include_body
+{
+    my($file, $object, $id, $desc, $attr_type) = @_;
+    #
+    # Use the description and field name in the comment
+    #
+    print $file "/**\n";
+    print $file " * "."lvm_"."$object"."_{get|set}_$id\n";
+    print $file " * $id - $desc\n";
+    print $file " */\n";
+    # Write out 'get' method prototype
+    $fun_name = "lvm_"."$object"."_get_$id";
+    $fun_args = "($object"."_t *$object)";
+    print $file "$attr_type"."$fun_name"."$fun_args".";\n";
+    # Write out 'set' method prototype
+    $fun_name = "lvm_"."$object"."_set_$id";
+    $fun_args = "($object"."_t *$object, $attr_type"."value)";
+    print $file "int "."$fun_name"."$fun_args".";\n\n";
+}
+
+sub write_include_footer
+{
+    my($file) = @_;
+    print $file <<ENDFOOTER;
+#endif
+ENDFOOTER
+}
+
+sub write_code_header
+{
+    my($file) = @_;
+    print $file <<ENDCODE1;
+/*
+ * Copyright (C) 2009 Red Hat, Inc. All rights reserved.
+ *
+ * This file is part of LVM2.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License v.2.1.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+#include "lvm.h"
+
+ENDCODE1
+}
+
+sub write_code_body
+{
+    my($file, $object, $sorttype, $id, $desc, $attr_type) = @_;
+    print $file "/**\n";
+    print $file " * "."lvm_"."$object"."_{get|set}_$id\n";
+    print $file " * $id - $desc\n";
+    print $file " */\n";
+    # Write out 'get' method
+    $fun_name = "lvm_"."$object"."_get_$id";
+    $fun_args = "($object"."_t *$object)";
+    print $file "$attr_type"."$fun_name"."$fun_args"."\n";
+    if ($sorttype =~ m/STR/) {
+        print $file <<ENDCODE4;
+{
+    /* FIXME: implement function body */
+    return ($attr_type)0;
+}
+ENDCODE4
+	} else {
+    print $file <<ENDCODE5;
+{
+    /* FIXME: implement function body */
+    return -1;
+}
+
+ENDCODE5
+    }
+    # Write out 'set' method
+    $fun_name = "lvm_"."$object"."_set_$id";
+    $fun_args = "($object"."_t *$object, $attr_type"."value)";
+    print $file "int "."$fun_name"."$fun_args"."\n";
+    print $file <<ENDCODE2;
+{
+    /* FIXME: implement function body */
+    return -1;
+}
+ENDCODE2
+}
+
+
+sub write_code_footer
+{
+# nothing to do
+}
Signature · 1 line
1.6.0.6
#3 lore raw

PATCH 3/8 Add generated files, lvm_object_prop.[ch], to liblvm build.

These files were generated from using the current columns.h file as input into lvm_object_prop.pl. They are a starting point for liblvm object property get/set functions. Actual code implementing the functions will be added later.

Include lvm_object_prop.h in lvm.h

---
 include/.symlinks        |    1 +
 liblvm/Makefile.in       |    3 +-
 liblvm/lvm.h             |    1 +
 liblvm/lvm_object_prop.c | 1009 ++++++++++++++++++++++++++++++++++++++++++++++
 liblvm/lvm_object_prop.h |  494 +++++++++++++++++++++++
 5 files changed, 1507 insertions(+), 1 deletions(-)
 create mode 100644 liblvm/lvm_object_prop.c
 create mode 100644 liblvm/lvm_object_prop.h
include/.symlinks +1 -0
index 28da67f..35461af 100644
@@ -1,6 +1,7 @@
 ../daemons/clvmd/clvm.h
 ../daemons/dmeventd/libdevmapper-event.h
 ../liblvm/lvm.h
+../liblvm/lvm_object_prop.h
 ../lib/activate/activate.h
 ../lib/activate/targets.h
 ../lib/cache/lvmcache.h
liblvm/Makefile.in +2 -1
index 3ab4950..a560533 100644
@@ -17,7 +17,8 @@ top_srcdir = @top_srcdir@
 VPATH = @srcdir@
 
 SOURCES =\
-	lvm_base.c
+	lvm_base.c \
+	lvm_object_prop.c
 
 LIB_NAME = liblvm
 LIB_VERSION = $(LIB_VERSION_LVM)
liblvm/lvm.h +1 -0
index 4e71e15..55dcb68 100644
@@ -68,5 +68,6 @@ void lvm_destroy(lvm_t libh);
  */
 int lvm_reload_config(lvm_t libh);
 
+#include "lvm_object_prop.h"
 
 #endif /* _LIB_LVM_H */
liblvm/lvm_object_prop.c new file +1009 -0

1,009 lines not on this page · raw message

liblvm/lvm_object_prop.h new file +494 -0

494 lines not on this page · raw message

Signature · 1 line
1.6.0.6
#4 lore raw

PATCH 4/8 Implement lvm_{vg|lv}_get_{name|uuid}

- use vg->vgmem pool to dup names and uuids
- add exports for lvm_{vg|lv}_get_{name|uuid}.

---
 liblvm/.exported_symbols |    6 +++++-
 liblvm/lvm_object_prop.c |   20 ++++++++++++--------
 2 files changed, 17 insertions(+), 9 deletions(-)
liblvm/.exported_symbols +5 -1
index 1b65e10..f00bb19 100644
@@ -1,3 +1,7 @@
 lvm_create
 lvm_destroy
-lvm_reload_config
\ No newline at end of file
+lvm_reload_config
+lvm_lv_get_name
+lvm_lv_get_uuid
+lvm_vg_get_name
+lvm_vg_get_uuid
liblvm/lvm_object_prop.c +12 -8
index 2b02457..768f2bd 100644
@@ -12,6 +12,8 @@
  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 #include "lvm.h"
+#include "metadata-exported.h"
+#include "lvm-string.h"
 
 /**
  * lvm_lv_{get|set}_uuid
@@ -19,8 +21,9 @@
  */
 const char *lvm_lv_get_uuid(lv_t *lv)
 {
-    /* FIXME: implement function body */
-    return (const char *)0;
+    return (const char *)dm_pool_strndup(lv->vg->vgmem,
+					(const char *)lv->lvid.id[1].uuid,
+					sizeof(lv->lvid.id[1].uuid));
 }
 int lvm_lv_set_uuid(lv_t *lv, const char *value)
 {
@@ -33,8 +36,8 @@ int lvm_lv_set_uuid(lv_t *lv, const char *value)
  */
 const char *lvm_lv_get_name(lv_t *lv)
 {
-    /* FIXME: implement function body */
-    return (const char *)0;
+    return (const char *)dm_pool_strndup(lv->vg->vgmem, lv->name,
+					 NAME_LEN);
 }
 int lvm_lv_set_name(lv_t *lv, const char *value)
 {
@@ -542,8 +545,9 @@ int lvm_vg_set_fmt(vg_t *vg, const char *value)
  */
 const char *lvm_vg_get_uuid(vg_t *vg)
 {
-    /* FIXME: implement function body */
-    return (const char *)0;
+    return (const char *)dm_pool_strndup(vg->vgmem,
+					(const char *)vg->id.uuid,
+					sizeof(vg->id.uuid));
 }
 int lvm_vg_set_uuid(vg_t *vg, const char *value)
 {
@@ -556,8 +560,8 @@ int lvm_vg_set_uuid(vg_t *vg, const char *value)
  */
 const char *lvm_vg_get_name(vg_t *vg)
 {
-    /* FIXME: implement function body */
-    return (const char *)0;
+    return (const char *)dm_pool_strndup(vg->vgmem, vg->name,
+					 NAME_LEN);
 }
 int lvm_vg_set_name(vg_t *vg, const char *value)
 {
Signature · 1 line
1.6.0.6
#5 lore raw

PATCH 5/8 Add list structure definitions for liblvm objects.

- pv_t, vg_t, lv_t, lvseg_t, pvseg_t
- include libdevmapper.h: needed for struct dm_list

These list structures will be needed in later APIs to return a list of handles to one object, given another object. For example, lvm_lvs_in_vg() will return a list of LV handles (lv_t's) given a VG handle (vg_t). We need a structure to do this so we define the LV structure, as well as the other structures at this point.

---
 liblvm/lvm.h |   27 +++++++++++++++++++++++++++
 1 files changed, 27 insertions(+), 0 deletions(-)
liblvm/lvm.h +27 -0
index 55dcb68..b345b36 100644
@@ -15,6 +15,7 @@
 #define _LIB_LVM_H
 
 #include "lvm-version.h"
+#include "libdevmapper.h"
 
 #include <stdint.h>
 
@@ -32,6 +33,32 @@ typedef struct logical_volume lv_t;
 typedef struct pv_segment pvseg_t;
 typedef struct lv_segment lvseg_t;
 
+typedef struct lvm_lvseg_list {
+	struct dm_list list;
+	lvseg_t *lvseg;
+} lvseg_list_t;
+
+typedef struct lvm_pvseg_list {
+	struct dm_list list;
+	pvseg_t *pvseg;
+} pvseg_list_t;
+
+typedef struct lvm_vg_list {
+	struct dm_list list;
+	vg_t *vg;
+} vg_list_t;
+
+typedef struct lvm_pv_list {
+	struct dm_list list;
+	pv_t *pv;
+} pv_list_t;
+
+typedef struct lvm_lv_list {
+	struct dm_list list;
+	lv_t *lv;
+} lv_list_t;
+
+
 struct lvm; /* internal data */
 
 /**
Signature · 1 line
1.6.0.6
#6 lore raw

PATCH 6/8 Add lvm_lvs_in_vg() - returns a list of lv handles for a given vg.

- Use vgmem pool to allocate a list of lvm_lv_list structs
- Allocate a new list each call (list may have changed since last call)
- Add lvm_lvs_in_vg to liblvm's exported symbols

NOTE: we may want to think about memory management beyond using the vgmem pool. Another possibility is to have a list handle that is allocated and freed, then the memory is attached to that. With a list handle you would not have to close/release the vg to free the list memory.

---
 liblvm/.exported_symbols |    1 +
 liblvm/lvm.h             |    7 +++++++
 liblvm/lvm_base.c        |   26 ++++++++++++++++++++++++++
 3 files changed, 34 insertions(+), 0 deletions(-)
liblvm/.exported_symbols +1 -0
index f00bb19..d05be6f 100644
@@ -5,3 +5,4 @@ lvm_lv_get_name
 lvm_lv_get_uuid
 lvm_vg_get_name
 lvm_vg_get_uuid
+lvm_lvs_in_vg
liblvm/lvm.h +7 -0
index b345b36..9574991 100644
@@ -58,6 +58,13 @@ typedef struct lvm_lv_list {
 	lv_t *lv;
 } lv_list_t;
 
+/**
+ * Return a list of LV handles for a given VG handle.
+ *
+ * \return  A list of lv_list_t structures containing lv handles for this vg.
+ *          If no LVs exist on the given VG, NULL is returned.
+ */
+struct dm_list *lvm_lvs_in_vg(vg_t *vg);
 
 struct lvm; /* internal data */
 
liblvm/lvm_base.c +26 -0
index 213246a..4e235ab 100644
@@ -60,3 +60,29 @@ int lvm_reload_config(lvm_t libh)
 	/* FIXME: re-init locking needed here? */
 	return refresh_toolcontext((struct cmd_context *)libh);
 }
+
+struct dm_list *lvm_lvs_in_vg(vg_t *vg)
+{
+	struct dm_list *list;
+	lv_list_t *lvs;
+	struct lv_list *lvl;
+
+	if (dm_list_empty(&vg->lvs))
+		return NULL;
+
+	if (!(list = dm_pool_zalloc(vg->vgmem, sizeof(*list)))) {
+		log_error("Memory allocation fail for dm_list.\n");
+		return NULL;
+	}
+	dm_list_init(list);
+
+	dm_list_iterate_items(lvl, &vg->lvs) {
+		if (!(lvs = dm_pool_zalloc(vg->vgmem, sizeof(*lvs)))) {
+			log_error("Memory allocation fail for lvm_lv_list.\n");
+			return NULL;
+		}
+		lvs->lv = lvl->lv;
+		dm_list_add(list, &lvs->list);
+	}
+	return list;
+}
Signature · 1 line
1.6.0.6
#7 lore raw

PATCH 7/8 Add lvm_vg_open() and lvm_vg_close for testing.

- lvm_vg_open: very simplistic version of vg_read()
- export lvm_vg_open and lvm_vg_close

NOTE: This patch is mainly to allow testing of the attribute patches. I view this patch as peripheral to the attribute patches and is mainly included for testing so please don't get hung up on it.

---
 liblvm/.exported_symbols |    2 ++
 liblvm/lvm.h             |    4 ++++
 liblvm/lvm_base.c        |   25 +++++++++++++++++++++++++
 3 files changed, 31 insertions(+), 0 deletions(-)
liblvm/.exported_symbols +2 -0
index d05be6f..3b0ffb8 100644
@@ -6,3 +6,5 @@ lvm_lv_get_uuid
 lvm_vg_get_name
 lvm_vg_get_uuid
 lvm_lvs_in_vg
+lvm_vg_open
+lvm_vg_close
liblvm/lvm.h +4 -0
index 9574991..47a5270 100644
@@ -18,6 +18,7 @@
 #include "libdevmapper.h"
 
 #include <stdint.h>
+#include <fcntl.h>
 
 /* Internal object structures */
 struct volume_group;
@@ -104,4 +105,7 @@ int lvm_reload_config(lvm_t libh);
 
 #include "lvm_object_prop.h"
 
+vg_t *lvm_vg_open(lvm_t libh, const char *vg_name, const mode_t mode);
+void lvm_vg_close(vg_t *vg);
+
 #endif /* _LIB_LVM_H */
liblvm/lvm_base.c +25 -0
index 4e235ab..a4a3deb 100644
@@ -16,6 +16,8 @@
 #include "lvm.h"
 #include "toolcontext.h"
 #include "locking.h"
+#include "metadata-exported.h"
+#include "lvmcache.h"
 
 lvm_t lvm_create(const char *system_dir)
 {
@@ -86,3 +88,26 @@ struct dm_list *lvm_lvs_in_vg(vg_t *vg)
 	}
 	return list;
 }
+
+/*
+ * Open / read a VG.
+ * FIXME: Only read access allowed.
+ */
+vg_t *lvm_vg_open(lvm_t libh, const char *vg_name, mode_t mode)
+{
+	if ((mode & O_ACCMODE) != O_RDONLY) {
+		log_error("Invalid access mode 0x%x for lvm_vg_read()\n",
+			  mode);
+		return NULL;
+	}
+	return vg_lock_and_read((struct cmd_context *)libh,
+				vg_name, NULL, LCK_VG_READ, CLUSTERED, 0);
+}
+
+void lvm_vg_close(vg_t *vg)
+{
+	if (vgname_is_locked(vg->name))
+	    unlock_vg(vg->cmd, vg->name);
+	vg_release(vg);
+}
+
Signature · 1 line
1.6.0.6
#8 lore raw

PATCH 8/8 Update test/api/test.c for simple tests involving vg/lv name/uuid attributes.

Tests the following APIs:
- lvm_{vg|lv}_get_{name|uuid}()
- lvm_lvs_in_vg()

Example:
./test
lvm> vg_open vg1
Success opening vg vg1
lvm> vgs_open
vg1 (VeMEO8rGOfY44H8MWieFxIf26CKKpJNj): mode = READ lvm> lvs_in_vg vg1
LVs in VG vg1:
vg1/lv1 (7phxOHwNPWS9wsqjkD22Fmd1vvLImYjm) lvm> vg_close vg1
lvm> exit
Exiting.

---
 test/api/test.c |  204 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 196 insertions(+), 8 deletions(-)
test/api/test.c +196 -8
index b742976..8217301 100644
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.  
+ * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
  * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
  *
  * This file is part of LVM2.
@@ -29,7 +29,7 @@ static int lvm_split(char *str, int *argc, char **argv, int max)
 		while (*b && isspace(*b))
 			b++;
 
-		if ((!*b) || (*b == '#'))
+		if ((!*b) || ((*argc == 0)&&(*b == '#')))
 			break;
 
 		e = b;
@@ -48,11 +48,190 @@ static int lvm_split(char *str, int *argc, char **argv, int max)
 	return *argc;
 }
 
+static void _show_help(void)
+{
+	printf("'lvs_in_vg vgname': "
+	       "List the LVs that exist in VG vgname\n");
+	printf("'vgs_open': "
+	       "List the VGs that are currently open\n");
+	printf("'vgs': "
+	       "List all VGs known to the system\n");
+	printf("'vg_open vgname ['r' | 'w']': "
+	       "Issue a lvm_vg_open() API call on VG 'vgname'\n");
+	printf("'vg_close vgname': "
+	       "Issue a lvm_vg_close() API call on VG 'vgname'\n");
+	printf("'quit': exit the program\n");
+}
+
+static struct dm_hash_table *_vgid_hash = NULL;
+static struct dm_hash_table *_vgname_hash = NULL;
+static struct dm_hash_table *_pvname_hash = NULL;
+static struct dm_hash_table *_lvname_hash = NULL;
+
+static void _hash_destroy_single(struct dm_hash_table **htable)
+{
+	if (htable && *htable) {
+		dm_hash_destroy(*htable);
+		*htable = NULL;
+	}
+}
+
+static void _hash_destroy(void)
+{
+	_hash_destroy_single(&_vgname_hash);
+	_hash_destroy_single(&_vgid_hash);
+	_hash_destroy_single(&_pvname_hash);
+	_hash_destroy_single(&_lvname_hash);
+}
+
+static int _hash_create(void)
+{
+	if (!(_vgname_hash = dm_hash_create(128)))
+		return 0;
+	if (!(_pvname_hash = dm_hash_create(128))) {
+		_hash_destroy_single(&_vgname_hash);
+		return 0;
+	}
+	if (!(_lvname_hash = dm_hash_create(128))) {
+		_hash_destroy_single(&_vgname_hash);
+		_hash_destroy_single(&_pvname_hash);
+		return 0;
+	}
+	if (!(_vgid_hash = dm_hash_create(128))) {
+		_hash_destroy_single(&_vgname_hash);
+		_hash_destroy_single(&_pvname_hash);
+		_hash_destroy_single(&_lvname_hash);
+		return 0;
+	}
+	return 1;
+}
+
+static vg_t *_lookup_vg_by_name(char **argv, int argc)
+{
+	vg_t *vg;
+
+	if (argc < 2) {
+		printf ("Please enter vg_name\n");
+		return NULL;
+	}
+	if (!(vg = dm_hash_lookup(_vgid_hash, argv[1])) &&
+	    !(vg = dm_hash_lookup(_vgname_hash, argv[1]))) {
+		printf ("Can't find %s in open VGs - run vg_open first\n",
+			argv[1]);
+		return NULL;
+	}
+	return vg;
+}
+
+static void _vg_open(char **argv, int argc, lvm_t libh)
+{
+	vg_t *vg;
+	struct dm_list *lvs;
+	struct lvm_lv_list *lvl;
+#if 0
+	struct dm_list *pvs;
+	struct lvm_pv_list *pvl;
+#endif
+	mode_t mode;
+
+	if (argc < 2) {
+		printf ("Please enter vg_name\n");
+		return;
+	}
+	if ((vg = dm_hash_lookup(_vgid_hash, argv[1])) ||
+	    (vg = dm_hash_lookup(_vgname_hash, argv[1]))) {
+		printf ("VG already open\n");
+		return;
+	}
+	if ((argc == 3) && (argv[2][0] == 'w')) {
+		mode = O_RDWR;
+	} else
+		mode = O_RDONLY;
+	vg = lvm_vg_open(libh, argv[1], mode);
+	if (!vg || !lvm_vg_get_name(vg)) {
+		printf("Error opening %s\n", argv[1]);
+		return;
+	}
+
+	printf("Success opening vg %s\n", argv[1]);
+	dm_hash_insert(_vgname_hash, lvm_vg_get_name(vg), vg);
+	dm_hash_insert(_vgid_hash, lvm_vg_get_uuid(vg), vg);
+
+	/*
+	 * Add the LVs and PVs into the hashes for lookups
+	 */
+	lvs = lvm_lvs_in_vg(vg);
+	dm_list_iterate_items(lvl, lvs) {
+		/* Concatenate VG name with LV name */
+		dm_hash_insert(_lvname_hash, lvm_lv_get_name(lvl->lv), lvl->lv);
+	}
+#if 0
+	pvs = lvm_pvs_in_vg(vg);
+	dm_list_iterate_items(pvl, pvs) {
+		dm_hash_insert(_pvname_hash, lvm_pv_name(pvl->pv), pvl->pv);
+	}
+#endif
+}
+
+static void _vg_close(char **argv, int argc)
+{
+	vg_t *vg;
+
+	if (argc < 2) {
+		printf ("Please enter vg_name\n");
+		return;
+	}
+	while((vg = dm_hash_lookup(_vgname_hash, argv[1]))) {
+		dm_hash_remove(_vgid_hash, lvm_vg_get_uuid(vg));
+		dm_hash_remove(_vgname_hash, lvm_vg_get_name(vg));
+		lvm_vg_close(vg);
+	}
+	while((vg = dm_hash_lookup(_vgid_hash, argv[1]))) {
+		dm_hash_remove(_vgid_hash, lvm_vg_get_uuid(vg));
+		dm_hash_remove(_vgname_hash, lvm_vg_get_name(vg));
+		lvm_vg_close(vg);
+	}
+}
+
+static void _show_one_vg(vg_t *vg)
+{
+	/* FIXME: uuid is not null terminated */
+	printf("%s (%s): mode = %s\n", lvm_vg_get_name(vg),
+	       lvm_vg_get_uuid(vg), "READ");
+}
+
+static void _list_open_vgs(void)
+{
+	dm_hash_iter(_vgid_hash, (dm_hash_iterate_fn) _show_one_vg);
+}
+
+static void _lvs_in_vg(char **argv, int argc)
+{
+	struct dm_list *lvs;
+	struct lvm_lv_list *lvl;
+	vg_t *vg;
+
+	if (!(vg = _lookup_vg_by_name(argv, argc)))
+		return;
+	lvs = lvm_lvs_in_vg(vg);
+	if (!lvs || dm_list_empty(lvs)) {
+		printf("No LVs in VG %s\n", lvm_vg_get_name(vg));
+		return;
+	}
+	printf("LVs in VG %s:\n", lvm_vg_get_name(vg));
+	dm_list_iterate_items(lvl, lvs) {
+		printf("%s/%s (%s)\n", lvm_vg_get_name(vg),
+		       lvm_lv_get_name(lvl->lv), lvm_lv_get_uuid(lvl->lv));
+	}
+}
+
 static int lvmapi_test_shell(lvm_t libh)
 {
-	int argc, i;
+	int argc;
 	char *input = NULL, *args[MAX_ARGS], **argv;
 
+	_hash_create();
+	argc=0;
 	while (1) {
 		free(input);
 		input = readline("lvm> ");
@@ -86,17 +265,26 @@ static int lvmapi_test_shell(lvm_t libh)
 			printf("Exiting.\n");
 			break;
 		} else if (!strcmp(argv[0], "?") || !strcmp(argv[0], "help")) {
-			printf("No commands defined\n");
-		} else if (!strcmp(argv[0], "scan")) {
-			for (i=1; i < argc; i++)
-				printf("Scan a path!\n");
+			_show_help();
+		} else if (!strcmp(argv[0], "vg_open")) {
+			_vg_open(argv, argc, libh);
+		} else if (!strcmp(argv[0], "vg_close")) {
+			_vg_close(argv, argc);
+		} else if (!strcmp(argv[0], "vgs_open")) {
+			_list_open_vgs();
+		} else if (!strcmp(argv[0], "lvs_in_vg")) {
+			_lvs_in_vg(argv, argc);
+		} else {
+			printf ("Unrecognized command %s\n", argv[0]);
 		}
 	}
 
+	dm_hash_iter(_vgname_hash, (dm_hash_iterate_fn) lvm_vg_close);
+	_hash_destroy();
 	free(input);
 	return 0;
 }
-		      
+
 int main (int argc, char *argv[])
 {
 	lvm_t libh;
Signature · 1 line
1.6.0.6
#9 lore raw

cover : liblvm object attribute baseline

In reply to Dave Wysochanski
Quoted 10 lines · Dave Wysochanski

The following 8 patches implement a baseline for liblvm objects and their attributes. The primary focus/goal of these patches are:
1. Define / clarify the main liblvm objects and their handles
2. Get the object properties for a specific handle

Together with the handles, the 'get' functions will provide equivalent functionality to the reporting commands (pvs, vgs, lvs) while keeping with the object model of liblvm.

I also pushed these patches on a dwysocha-liblvm-object-attr-1 branch of the exp-git repo on git.et.redhat.com:
http://git.et.redhat.com/?p=lvm-exp.git;a=summary
http://tinyurl.com/c7m684