2
* Copyright (c) 2008, XenSource Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
7
* * Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* * Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
12
* * Neither the name of XenSource Inc. nor the names of its contributors
13
* may be used to endorse or promote products derived from this software
14
* without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
static char line[1024];
39
lvm_read_line(FILE *scan)
41
memset(line, 0, sizeof(line));
42
return (fscanf(scan, "%1023[^\n]", line) != 1);
46
lvm_next_line(FILE *scan)
48
return (fscanf(scan, "%1023[\n]", line) != 1);
52
lvm_copy_name(char *dst, const char *src, size_t size)
54
if (strnlen(src, size) == size)
62
lvm_parse_pv(struct vg *vg, const char *name, int pvs, uint64_t start)
70
vg->pvs = calloc(pvs, sizeof(struct pv));
75
for (i = 0; i < pvs; i++) {
81
if (!strcmp(pv->name, name))
91
err = lvm_copy_name(pv->name, name, sizeof(pv->name) - 1);
100
lvm_open_vg(const char *vgname, struct vg *vg)
103
int i, err, pvs, lvs;
104
char *cmd, pvname[256];
105
uint64_t size, pv_start;
107
memset(vg, 0, sizeof(*vg));
109
err = asprintf(&cmd, "/usr/sbin/vgs %s --noheadings --nosuffix --units=b "
110
"--options=vg_name,vg_extent_size,lv_count,pv_count,"
111
"pv_name,pe_start --unbuffered 2> /dev/null", vgname);
116
scan = popen(cmd, "r");
118
err = (errno ? -errno : ENOMEM);
123
if (lvm_read_line(scan))
127
if (sscanf(line, _NAME" %"SCNu64" %d %d "_NAME" %"SCNu64,
128
vg->name, &size, &lvs, &pvs, pvname, &pv_start) != 6)
131
if (strcmp(vg->name, vgname))
134
err = lvm_parse_pv(vg, pvname, pvs, pv_start);
138
if (lvm_next_line(scan))
143
if (strcmp(vg->name, vgname))
146
for (i = 0; i < pvs; i++)
147
if (!vg->pvs[i].name[0])
151
vg->lvs = calloc(lvs, sizeof(struct lv));
158
vg->extent_size = size;
170
lvm_parse_lv_devices(struct vg *vg, struct lv_segment *seg, char *devices)
173
uint64_t start, pe_start;
175
for (i = 0; i < strlen(devices); i++)
176
if (strchr(",()", devices[i]))
179
if (sscanf(devices, _NAME" %"SCNu64, seg->device, &start) != 2)
183
for (i = 0; i < vg->pv_cnt; i++)
184
if (!strcmp(vg->pvs[i].name, seg->device)) {
185
pe_start = vg->pvs[i].start;
192
seg->pe_start = (start * vg->extent_size) + pe_start;
197
lvm_scan_lvs(struct vg *vg)
203
err = asprintf(&cmd, "/usr/sbin/lvs %s --noheadings --nosuffix --units=b "
204
"--options=lv_name,lv_size,segtype,seg_count,seg_start,"
205
"seg_size,devices --unbuffered 2> /dev/null", vg->name);
210
scan = popen(cmd, "r");
212
err = (errno ? -errno : -ENOMEM);
219
struct lv_segment seg;
220
uint64_t size, seg_start;
221
char type[32], name[256], dev[256], devices[1024];
226
if (lvm_read_line(scan)) {
234
if (sscanf(line, _NAME" %"SCNu64" %31s %u %"SCNu64" %"SCNu64" %1023s",
235
name, &size, type, &segs, &seg_start,
236
&seg.pe_size, devices) != 7)
242
if (!strcmp(type, "linear"))
243
seg.type = LVM_SEG_TYPE_LINEAR;
245
seg.type = LVM_SEG_TYPE_UNKNOWN;
247
if (lvm_parse_lv_devices(vg, &seg, devices))
253
lv->first_segment = seg;
255
err = lvm_copy_name(lv->name, name, sizeof(lv->name) - 1);
261
if (lvm_next_line(scan))
275
lvm_free_vg(struct vg *vg)
279
memset(vg, 0, sizeof(*vg));
283
lvm_scan_vg(const char *vg_name, struct vg *vg)
287
memset(vg, 0, sizeof(*vg));
289
err = lvm_open_vg(vg_name, vg);
293
err = lvm_scan_lvs(vg);
306
printf("usage: lvm-util <vgname>\n");
311
main(int argc, char **argv)
317
struct lv_segment *seg;
322
err = lvm_scan_vg(argv[1], &vg);
324
printf("scan failed: %d\n", err);
325
return (err >= 0 ? err : -err);
329
printf("vg %s: extent_size: %"PRIu64", pvs: %d, lvs: %d\n",
330
vg.name, vg.extent_size, vg.pv_cnt, vg.lv_cnt);
332
for (i = 0; i < vg.pv_cnt; i++) {
334
printf("pv %s: start %"PRIu64"\n", pv->name, pv->start);
337
for (i = 0; i < vg.lv_cnt; i++) {
339
seg = &lv->first_segment;
340
printf("lv %s: size: %"PRIu64", segments: %u, type: %u, "
341
"dev: %s, pe_start: %"PRIu64", pe_size: %"PRIu64"\n",
342
lv->name, lv->size, lv->segments, seg->type,
343
seg->device, seg->pe_start, seg->pe_size);