CP-23825: Structure lvm-util as a business logic library with an execution wrapper

From: Mark Syms <mark.syms@citrix.com>

Signed-off-by: Mark Syms <mark.syms@citrix.com>

diff --git a/lvm/Makefile.am b/lvm/Makefile.am
index d026f5f..f890f94 100644
--- a/lvm/Makefile.am
+++ b/lvm/Makefile.am
@@ -4,8 +4,11 @@ AM_CFLAGS += -Werror
 
 sbin_PROGRAMS = lvm-util
 
-lvm_util_SOURCES  = lvm-util.c
-lvm_util_SOURCES += lvm-util.h
+noinst_LTLIBRARIES = liblvmutil.la
 
-lvm_util_CPPFLAGS  = -D_GNU_SOURCE
-lvm_util_CPPFLAGS += -DLVM_UTIL
+liblvmutil_la_SOURCES  = lvm-util.c
+
+lvm_util_SOURCES = main.c
+lvm_util_LDADD = liblvmutil.la
+
+liblvmutil_la_CPPFLAGS  = -D_GNU_SOURCE
diff --git a/lvm/lvm-util-priv.h b/lvm/lvm-util-priv.h
new file mode 100644
index 0000000..5c97dea
--- /dev/null
+++ b/lvm/lvm-util-priv.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2016, Citrix Systems, Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 
+ *  1. Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *  3. Neither the name of the copyright holder nor the names of its 
+ *     contributors may be used to endorse or promote products derived from 
+ *     this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _LVM_UTIL_PRIV_H
+#define _LVM_UTIL_PRIV_H
+#include "lvm-util.h"
+
+int
+lvm_scan_vg(const char *vg_name, struct vg *vg);
+
+void
+lvm_free_vg(struct vg *vg);
+
+#endif /*_LVM_UTIL_PRIV_H*/
diff --git a/lvm/lvm-util.c b/lvm/lvm-util.c
index 4468c7f..8129e51 100644
--- a/lvm/lvm-util.c
+++ b/lvm/lvm-util.c
@@ -327,51 +327,3 @@ lvm_scan_vg(const char *vg_name, struct vg *vg)
 
 	return 0;
 }
-
-#ifdef LVM_UTIL
-static int
-usage(void)
-{
-	printf("usage: lvm-util <vgname>\n");
-	exit(EINVAL);
-}
-
-int
-main(int argc, char **argv)
-{
-	int i, err;
-	struct vg vg;
-	struct pv *pv;
-	struct lv *lv;
-	struct lv_segment *seg;
-
-	if (argc != 2)
-		usage();
-
-	err = lvm_scan_vg(argv[1], &vg);
-	if (err) {
-		printf("scan failed: %d\n", err);
-		return (err >= 0 ? err : -err);
-	}
-
-	printf("vg %s: extent_size: %"PRIu64", pvs: %d, lvs: %d\n",
-	       vg.name, vg.extent_size, vg.pv_cnt, vg.lv_cnt);
-
-	for (i = 0; i < vg.pv_cnt; i++) {
-		pv = vg.pvs + i;
-		printf("pv %s: start %"PRIu64"\n", pv->name, pv->start);
-	}
-
-	for (i = 0; i < vg.lv_cnt; i++) {
-		lv  = vg.lvs + i;
-		seg = &lv->first_segment;
-		printf("lv %s: size: %"PRIu64", segments: %u, type: %u, "
-		       "dev: %s, pe_start: %"PRIu64", pe_size: %"PRIu64"\n",
-		       lv->name, lv->size, lv->segments, seg->type,
-		       seg->device, seg->pe_start, seg->pe_size);
-	}
-
-	lvm_free_vg(&vg);
-	return 0;
-}
-#endif
diff --git a/lvm/main.c b/lvm/main.c
new file mode 100644
index 0000000..ef1705d
--- /dev/null
+++ b/lvm/main.c
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2016, Citrix Systems, Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 
+ *  1. Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *  3. Neither the name of the copyright holder nor the names of its 
+ *     contributors may be used to endorse or promote products derived from 
+ *     this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+
+#include "lvm-util.h"
+#include "lvm-util-priv.h"
+
+static int
+usage(void)
+{
+	printf("usage: lvm-util <vgname>\n");
+	exit(EINVAL);
+}
+
+int
+main(int argc, char **argv)
+{
+	int i, err;
+	struct vg vg;
+	struct pv *pv;
+	struct lv *lv;
+	struct lv_segment *seg;
+
+	if (argc != 2)
+		usage();
+
+	err = lvm_scan_vg(argv[1], &vg);
+	if (err) {
+		printf("scan failed: %d\n", err);
+		return (err >= 0 ? err : -err);
+	}
+
+	printf("vg %s: extent_size: %"PRIu64", pvs: %d, lvs: %d\n",
+	       vg.name, vg.extent_size, vg.pv_cnt, vg.lv_cnt);
+
+	for (i = 0; i < vg.pv_cnt; i++) {
+		pv = vg.pvs + i;
+		printf("pv %s: start %"PRIu64"\n", pv->name, pv->start);
+	}
+
+	for (i = 0; i < vg.lv_cnt; i++) {
+		lv  = vg.lvs + i;
+		seg = &lv->first_segment;
+		printf("lv %s: size: %"PRIu64", segments: %u, type: %u, "
+		       "dev: %s, pe_start: %"PRIu64", pe_size: %"PRIu64"\n",
+		       lv->name, lv->size, lv->segments, seg->type,
+		       seg->device, seg->pe_start, seg->pe_size);
+	}
+
+	lvm_free_vg(&vg);
+	return 0;
+}
+
diff --git a/vhd/lib/Makefile.am b/vhd/lib/Makefile.am
index 8c15701..f78df10 100644
--- a/vhd/lib/Makefile.am
+++ b/vhd/lib/Makefile.am
@@ -34,11 +34,10 @@ libvhd_la_SOURCES += canonpath.c
 libvhd_la_SOURCES += canonpath.h
 libvhd_la_SOURCES += atomicio.c
 libvhd_la_SOURCES += atomicio.h
-libvhd_la_SOURCES += ../../lvm/lvm-util.c
 
 libvhd_la_LDFLAGS = -version-info 1:1:1
 
-libvhd_la_LIBADD = -luuid $(LIBICONV)
+libvhd_la_LIBADD = -luuid $(LIBICONV) $(top_srcdir)/lvm/liblvmutil.la
 
 libvhdio_la_SOURCES  = libvhdio.c
 libvhdio_la_SOURCES += ../../part/partition.c
