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