Skip to contentSkip to conversation

Implement liblvm vgcreate/vgremove and related APIs

PATCH New Dave Wysochanski 2 participants 9 messages Jul 13, 2009

lore
Share & tools
lore
#0 lore raw

cover Implement liblvm vgcreate/vgremove and related APIs

The following patchset implements enough of the liblvm VG APIs to get the first of the unit test sequences completed as discussed in Berlin. The unit test involves a simple vgcreate/vgextend/vgremove sequence and uses the following liblvm APIs: lvm_create, lvm_destroy, lvm_vg_create, lvm_vg_extend, lvm_vg_set_extent_size, lvm_vg_write, lvm_vg_remove, and lvm_vg_close.

Unit test passes (provided hardcoded values are free) and applies cleanly on:
commit 1ed598ada6fabade22c680ef57e3fbd9a608ea91

Author: Dave Wysochanski <[email protected]>
Date:   Fri Jul 10 21:19:37 2009 +0000

Remove unused code vg_lock_and_read() and related flags.

Author: Dave Wysochanski <[email protected]>

#1 lore raw

PATCH 1/5 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 7a2f813..47b2e8a 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/5 Add default cmd->cmd_line initialization for liblvm lvm_create().

This needs initialized to non-NULL before using the archive() call. Normally this is set to the cmdline string when lvm is called from a tool. We could think about using it in another way, as a potential audit trail of liblvm calls, or just leave it set to the default "liblvm", similar to what clvmd does. For now, just set it to "liblvm".

---
 liblvm/lvm_base.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
liblvm/lvm_base.c +6 -0
index 213246a..4fa8541 100644
@@ -45,6 +45,12 @@ lvm_t lvm_create(const char *system_dir)
 		lvm_destroy((lvm_t) cmd);
 		return NULL;
 	}
+	/*
+	 * FIXME: Use cmd->cmd_line as audit trail for liblvm calls.  Used in
+	 * archive() call.  Possible example:
+	 * cmd_line = "lvm_vg_create: vg1\nlvm_vg_extend vg1 /dev/sda1\n"
+	 */
+	cmd->cmd_line = (char *)"liblvm";
 
 	return (lvm_t) cmd;
 }
Signature · 1 line
1.6.0.6
#3 lore raw

PATCH 3/5 Add lvm_vg_* APIs to create and modify VGs.

Add some liblvm APIs for VGs. Most of these APIs simply call into the internal liblvm library. Ideally we should call the liblvm functions directly from the tools. However, until we convert more of the code to liblvm functions, things like the cmd_context will get in the way. For now just implement the liblvm functions as wrappers around the internal functions, with a little error checking and return code handling.

The following APIs are implemented:
lvm_vg_create, lvm_vg_extend, lvm_vg_set_extent_size, lvm_vg_write, lvm_vg_remove, lvm_vg_close.

Still TODO:
- cleanup error handling by using lvm_errno() and related APIs
- cleanup naming / clarify which functions commit to disk vs not
- implement more 'set' functions
- decide on 'set' / 'change' nomenclature

---
 liblvm/Makefile.in |    3 +-
 liblvm/lvm.h       |   66 ++++++++++++++++++++++++++++++++++++++++
 liblvm/vg.c        |   84 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 152 insertions(+), 1 deletions(-)
 create mode 100644 liblvm/vg.c
liblvm/Makefile.in +2 -1
index fdd2ecc..5895c0c 100644
@@ -17,7 +17,8 @@ top_srcdir = @top_srcdir@
 VPATH = @srcdir@
 
 SOURCES =\
-	lvm_base.c
+	lvm_base.c \
+	vg.c
 
 LIB_NAME = liblvm2app
 LIB_VERSION = $(LIB_VERSION_LVM)
liblvm/lvm.h +66 -0

71 lines not on this page · raw message

liblvm/vg.c new file +84 -0

84 lines not on this page · raw message

Signature · 1 line
1.6.0.6
#4 lore raw

PATCH 4/5 Add VG APIs to liblvm/.exported_symbols.

Add the following VG APIs to liblvm/.exported_symbols:
-lvm_reload_config
-lvm_vg_create
-lvm_vg_extend
-lvm_vg_set_extent_size
-lvm_vg_write
-lvm_vg_close
-lvm_vg_remove

---
 liblvm/.exported_symbols |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
liblvm/.exported_symbols +7 -1
index 1b65e10..5a6d92e 100644
@@ -1,3 +1,9 @@
 lvm_create
 lvm_destroy
-lvm_reload_config
\ No newline at end of file
+lvm_reload_config
+lvm_vg_create
+lvm_vg_extend
+lvm_vg_set_extent_size
+lvm_vg_write
+lvm_vg_close
+lvm_vg_remove
Signature · 1 line
1.6.0.6
#5 lore raw

PATCH 5/5 Add liblvm test case for creating of a vg.

liblvm unit test case uses the following APIs:
- lvm_create, lvm_destroy
- lvm_vg_create, lvm_vg_extend, lvm_vg_set_extent_size, lvm_vg_write, lvm_vg_remove, lvm_vg_close

---
 test/api/vgcreate.c |   95 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 95 insertions(+), 0 deletions(-)
 create mode 100644 test/api/vgcreate.c
test/api/vgcreate.c new file +95 -0
new file mode 100644
index 0000000..c09e7a8
@@ -0,0 +1,95 @@
+/*
+ * 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
+ */
+/*
+ * Unit test case for vgcreate and related APIs.
+ * # gcc -g vgcreate.c -I../../liblvm -I../../include -L../../liblvm \
+ *   -L../../libdm -ldevmapper -llvm2app
+ * # export LD_LIBRARY_PATH=`pwd`/../../libdm:`pwd`/../../liblvm
+ */
+#include <stdio.h>
+#include <unistd.h>
+#include "lvm.h"
+
+lvm_t handle;
+vg_t *vg;
+char *vg_name = "my_vg";
+char *device = "/dev/loop3";
+uint64_t size = 1024;
+
+int main(int argc, char *argv[])
+{
+	int status;
+
+	/* FIXME: input vgname, etc from cmdline */
+	/* FIXME: make the below messages verbose-only and print PASS/FAIL*/
+	printf("Opening LVM\n");
+	handle = lvm_create(NULL);
+	if (!handle) {
+		fprintf(stderr, "Unable to lvm_create\n");
+		goto bad;
+	}
+
+	printf("Creating VG %s\n", vg_name);
+	vg = lvm_vg_create(handle, vg_name);
+	if (!vg) {
+		fprintf(stderr, "Error creating volume group %s\n", vg_name);
+		goto bad;
+	}
+
+	printf("Extending VG %s\n", vg_name);
+	status = lvm_vg_extend(vg, device);
+	if (!status) {
+		fprintf(stderr, "Error extending volume group %s "
+			"with device %s\n", vg_name, device);
+		goto bad;
+	}
+
+	printf("Setting VG %s extent_size to %d\n", vg_name, size);
+	status = lvm_vg_set_extent_size(vg, size);
+	if (!status) {
+		fprintf(stderr, "Can not set physical extent "
+			"size '%ld' for '%s'\n",
+			size, vg_name);
+		goto bad;
+	}
+
+	printf("Committing VG %s to disk\n", vg_name);
+	status = lvm_vg_write(vg);
+	if (!status) {
+		fprintf(stderr, "Creation of volume group '%s' on "
+			"device '%s' failed\n",
+			vg_name, device);
+		goto bad;
+	}
+
+	printf("Removing VG %s from system\n", vg_name);
+	status = lvm_vg_remove(vg);
+	if (!status) {
+		fprintf(stderr, "Revmoval of volume group '%s' failed\n",
+			vg_name);
+		goto bad;
+	}
+
+	lvm_vg_close(vg);
+	lvm_destroy(handle);
+	printf("liblvm vgcreate unit test PASS\n");
+	_exit(0);
+bad:
+	printf("liblvm vgcreate unit test FAIL\n");
+	if (vg)
+		lvm_vg_close(vg);
+	if (handle)
+		lvm_destroy(handle);
+	_exit(-1);
+}
Signature · 1 line
1.6.0.6
#6 lore raw

PATCH 3/5 Add lvm_vg_* APIs to create and modify VGs.

In reply to Dave Wysochanski
Quoted 1 line · Dave Wysochanski
 liblvm/vg.c        |   84 ++++++++++++++++++++++++++++++++++++++++++++++++++++

Add something else to the name 'vg' to make it more distinctive.

Quoted 2 lines

+/**
+ * Create a VG with default parameters.

Extend all the comments to give more information on how the API should be used. E.g. Explain here that you must close it with vg_close(), but must you also do that if you get an error?

Quoted 1 line

+ * Write a VG to disk.

Does it commit it?

Quoted 7 lines

+/**
+ * Close a VG opened with lvm_vg_create
+ *
+ * \param vg
+ * VG handle obtained from lvm_vg_create.
+ *
+ * \return Status code of 1 (success) or 0 (failure).

What are the failure modes? E.g. Can it fail if passed a valid handle, and if so what should you do?

Alasdair

#7 lore raw

PATCH 2/5 Add default cmd->cmd_line initialization for liblvm lvm_create().

In reply to Dave Wysochanski

liblvm/lvm_base.c lvm_t lvm_create(const char *system_dir) · line 53

Quoted 1 line · Dave Wysochanski
liblvm/lvm_base.c +1 -0
+	cmd->cmd_line = (char *)"liblvm";

Don't see why that isn't const - I've changed it.

Alasdair

#8 lore raw

cover Implement liblvm vgcreate/vgremove and related APIs

In reply to Dave Wysochanski
Quoted 2 lines · Dave Wysochanski

The following patchset implements enough of the liblvm VG APIs to get the first of the unit test sequences completed as discussed in Berlin.

Ack.

Alasdair