xref: /dragonfly/contrib/lvm2/dist/test/api/vgtest.c (revision 9348a738)
1 /*	$NetBSD: vgtest.c,v 1.1.1.1 2009/12/02 00:26:03 haad Exp $	*/
2 
3 /*
4  * Copyright (C) 2009 Red Hat, Inc. All rights reserved.
5  *
6  * This file is part of LVM2.
7  *
8  * This copyrighted material is made available to anyone wishing to use,
9  * modify, copy, or redistribute it subject to the terms and conditions
10  * of the GNU Lesser General Public License v.2.1.
11  *
12  * You should have received a copy of the GNU Lesser General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16 /*
17  * Unit test case for vgcreate and related APIs.
18  * # gcc -g vgcreate.c -I../../liblvm -I../../include -L../../liblvm \
19  *   -L../../libdm -ldevmapper -llvm2app
20  * # export LD_LIBRARY_PATH=`pwd`/../../libdm:`pwd`/../../liblvm
21  */
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <inttypes.h>
25 
26 #include "lvm2app.h"
27 
28 lvm_t handle;
29 vg_t vg;
30 const char *vg_name;
31 #define MAX_DEVICES 16
32 const char *device[MAX_DEVICES];
33 uint64_t size = 1024;
34 
35 #define vg_create(vg_name) \
36 	printf("Creating VG %s\n", vg_name); \
37 	vg = lvm_vg_create(handle, vg_name); \
38 	if (!vg) { \
39 		fprintf(stderr, "Error creating volume group %s\n", vg_name); \
40 		goto bad; \
41 	}
42 #define vg_extend(vg, dev) \
43 	printf("Extending VG %s by %s\n", vg_name, dev); \
44 	status = lvm_vg_extend(vg, dev); \
45 	if (status) { \
46 		fprintf(stderr, "Error extending volume group %s " \
47 			"with device %s\n", vg_name, dev); \
48 		goto bad; \
49 	}
50 #define vg_commit(vg) \
51 	printf("Committing VG %s to disk\n", vg_name); \
52 	status = lvm_vg_write(vg); \
53 	if (status) { \
54 		fprintf(stderr, "Commit of volume group '%s' failed\n", \
55 			lvm_vg_get_name(vg)); \
56 		goto bad; \
57 	}
58 #define vg_open(vg_name, mode) \
59 	printf("Opening VG %s %s\n", vg_name, mode); \
60 	vg = lvm_vg_open(handle, vg_name, mode, 0); \
61 	if (!vg) { \
62 		fprintf(stderr, "Error opening volume group %s\n", vg_name); \
63 		goto bad; \
64 	}
65 #define vg_close(vg) \
66 	printf("Closing VG %s\n", vg_name); \
67 	if (lvm_vg_close(vg)) { \
68 		fprintf(stderr, "Error closing volume group %s\n", vg_name); \
69 		goto bad; \
70 	}
71 #define vg_reduce(vg, dev) \
72 	printf("Reducing VG %s by %s\n", vg_name, dev); \
73 	status = lvm_vg_reduce(vg, dev); \
74 	if (status) { \
75 		fprintf(stderr, "Error reducing volume group %s " \
76 			"by device %s\n", vg_name, dev); \
77 		goto bad; \
78 	}
79 #define vg_remove(vg) \
80 	printf("Removing VG %s from system\n", vg_name); \
81 	status = lvm_vg_remove(vg); \
82 	if (status) { \
83 		fprintf(stderr, "Revmoval of volume group '%s' failed\n", \
84 			vg_name); \
85 		goto bad; \
86 	}
87 
88 static int init_vgtest(int argc, char *argv[])
89 {
90 	int i;
91 
92 	if (argc < 4) {
93 		fprintf(stderr, "Usage: %s <vgname> <pv1> <pv2> [... <pvN> ]",
94 			argv[0]);
95 		return -1;
96 	}
97 	vg_name = argv[1];
98 	for(i=2; i<MAX_DEVICES && i < argc; i++) {
99 		device[i-2] = argv[i];
100 	}
101 	return 0;
102 }
103 
104 int main(int argc, char *argv[])
105 {
106 	int status;
107 
108 	if (init_vgtest(argc, argv) < 0)
109 		goto bad;
110 
111 	/* FIXME: make the below messages verbose-only and print PASS/FAIL*/
112 	printf("Opening LVM\n");
113 	handle = lvm_init(NULL);
114 	if (!handle) {
115 		fprintf(stderr, "Unable to lvm_init\n");
116 		goto bad;
117 	}
118 
119 	printf("Library version: %s\n", lvm_library_get_version());
120 	vg_create(vg_name);
121 	vg_extend(vg, device[0]);
122 
123 	printf("Setting VG %s extent_size to %"PRIu64"\n", vg_name, size);
124 	status = lvm_vg_set_extent_size(vg, size);
125 	if (status) {
126 		fprintf(stderr, "Can not set physical extent "
127 			"size '%"PRIu64"' for '%s'\n",
128 			size, vg_name);
129 		goto bad;
130 	}
131 
132 	vg_commit(vg);
133 	vg_close(vg);
134 
135 	vg_open(vg_name, "r");
136 	vg_close(vg);
137 
138 	vg_open(vg_name, "w");
139 	vg_extend(vg, device[1]);
140 	vg_reduce(vg, device[0]);
141 	vg_commit(vg);
142 	vg_close(vg);
143 
144 	vg_open(vg_name, "w");
145 	vg_extend(vg, device[0]);
146 	vg_commit(vg);
147 	vg_close(vg);
148 
149 	vg_open(vg_name, "w");
150 	vg_remove(vg);
151 	vg_commit(vg);
152 	vg_close(vg);
153 
154 	lvm_quit(handle);
155 	printf("liblvm vgcreate unit test PASS\n");
156 	_exit(0);
157 bad:
158 	printf("liblvm vgcreate unit test FAIL\n");
159 	if (handle && lvm_errno(handle))
160 		fprintf(stderr, "LVM Error: %s\n", lvm_errmsg(handle));
161 	if (vg)
162 		lvm_vg_close(vg);
163 	if (handle)
164 		lvm_quit(handle);
165 	_exit(-1);
166 }
167