]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libdisk/lvm.c
590b03086105ceffd21fca6255804ec4d9e116fa
[thirdparty/xfsprogs-dev.git] / libdisk / lvm.c
1 /*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33 #include <stdio.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <linux/major.h>
41 #include <volume.h>
42
43 int
44 mnt_is_lvm_subvol(dev_t dev)
45 {
46 if (dev >> 8 == LVM_BLK_MAJOR)
47 return 1;
48 return 0;
49 }
50
51 int
52 lvm_get_subvol_stripe(
53 char *dfile,
54 sv_type_t type,
55 int *sunit,
56 int *swidth,
57 struct stat64 *sb)
58 {
59 int lvpipe[2], stripes = 0, stripesize = 0;
60 char *largv[3], buf[1024];
61 FILE *stream;
62
63 if (!mnt_is_lvm_subvol(sb->st_rdev))
64 return 0;
65
66 /* Quest for lvdisplay */
67 if (!access("/usr/local/sbin/lvdisplay", R_OK|X_OK))
68 largv[0] = "/usr/local/sbin/lvdisplay";
69 else if (!access("/usr/sbin/lvdisplay", R_OK|X_OK))
70 largv[0] = "/usr/sbin/lvdisplay";
71 else if (!access("/sbin/lvdisplay", R_OK|X_OK))
72 largv[0] = "/sbin/lvdisplay";
73 else {
74 fprintf(stderr,
75 "Warning - LVM device, but no lvdisplay(8) found\n");
76 return 0;
77 }
78
79 largv[1] = dfile;
80 largv[2] = NULL;
81
82 /* Open pipe */
83 if (pipe(lvpipe) < 0) {
84 fprintf(stderr, "Could not open pipe\n");
85 exit(1);
86 }
87
88 /* Spawn lvdisplay */
89 switch (fork()) {
90 case 0:
91 /* Plumbing */
92 close(lvpipe[0]);
93
94 if (lvpipe[1] != STDOUT_FILENO)
95 dup2(lvpipe[1], STDOUT_FILENO);
96
97 execv(largv[0], largv);
98
99 fprintf(stderr, "\nFailed to execute %s\n", largv[0]);
100 exit(1);
101
102 case -1:
103 fprintf(stderr, "Failed forking lvdisplay process\n");
104 exit(1);
105
106 default:
107 break;
108 }
109
110 close(lvpipe[1]);
111 stream = fdopen(lvpipe[0], "r");
112
113 /* Scan stream for keywords */
114 while (fgets(buf, 1023, stream) != NULL) {
115
116 if (!strncmp(buf, "Stripes", 7))
117 sscanf(buf, "Stripes %d", &stripes);
118
119 if (!strncmp(buf, "Stripe size", 11))
120 sscanf(buf, "Stripe size (KByte) %d", &stripesize);
121 }
122
123 /* Update sizes */
124 *sunit = stripesize << 1;
125 *swidth = (stripes * stripesize) << 1;
126
127 fclose(stream);
128
129 return 1;
130 }