1 | #!/usr/bin/perl -w |
---|
2 | # |
---|
3 | # Mindi subroutines related to LVM brought by the MondoRescue project |
---|
4 | # |
---|
5 | # $Id$ |
---|
6 | # |
---|
7 | # Copyright B. Cornec 2008 |
---|
8 | # Provided under the GPL v2 |
---|
9 | |
---|
10 | package MondoRescue::Mindi::LVM; |
---|
11 | |
---|
12 | use strict 'vars'; |
---|
13 | use Data::Dumper; |
---|
14 | use English; |
---|
15 | use lib qw (lib); |
---|
16 | use ProjectBuilder::Base; |
---|
17 | use ProjectBuilder::Conf; |
---|
18 | |
---|
19 | # Inherit from the "Exporter" module which handles exporting functions. |
---|
20 | |
---|
21 | use Exporter; |
---|
22 | |
---|
23 | # Export, by default, all the functions into the namespace of |
---|
24 | # any code which uses this module. |
---|
25 | |
---|
26 | our @ISA = qw(Exporter); |
---|
27 | our @EXPORT = qw(mr_lvm_check mr_lvm_analyze mr_lvm_prepare); |
---|
28 | |
---|
29 | =pod |
---|
30 | |
---|
31 | =head1 NAME |
---|
32 | |
---|
33 | MondoRescue::Mindi, part of the mondorescue.org |
---|
34 | |
---|
35 | =head1 DESCRIPTION |
---|
36 | |
---|
37 | This modules provides low level functions for the Mindi part of the Mondorescue project |
---|
38 | |
---|
39 | =head1 USAGE |
---|
40 | |
---|
41 | =over 4 |
---|
42 | |
---|
43 | =item B<mr_lvm_check> |
---|
44 | |
---|
45 | This function checks the usage of LVM and gets the version used |
---|
46 | It returns 2 parameters, the LVM version, and the lvm command to use if needed |
---|
47 | If LVM version is null then no LVM Handling should be done. |
---|
48 | |
---|
49 | =cut |
---|
50 | |
---|
51 | sub mr_lvm_check { |
---|
52 | |
---|
53 | # Get params from the conf file |
---|
54 | my ($lvmds_t,$lvmproc_t,$lvmcmd_t,$lvmpath_t) = pb_conf_get("mr_lvmdiskscan","mr_lvmprocfile","mr_lvmcmd","mr_lvmpath"); |
---|
55 | my $lvmds = $lvmds_t->{'mindi'}; |
---|
56 | my $lvmproc = $lvmproc_t->{'mindi'}; |
---|
57 | my $lvmcmd = $lvmcmd_t->{'mindi'}; |
---|
58 | my $lvmpath = $lvmpath_t->{'mindi'}; |
---|
59 | |
---|
60 | if (! -x $lvmproc) { |
---|
61 | pb_log(1,"$lvmproc doesn't exist."); |
---|
62 | return(0,undef); |
---|
63 | } |
---|
64 | |
---|
65 | # Check LVM volumes presence |
---|
66 | open(LVM,$lvmproc) || mr_exit(-1,"Unable to open $lvmproc"); |
---|
67 | while (<LVM>) { |
---|
68 | if (/0 VGs 0 PVs 0 LVs/) { |
---|
69 | pb_log(1,"No LVM volumes found in $lvmproc"); |
---|
70 | return(0,undef); |
---|
71 | } |
---|
72 | } |
---|
73 | close(LVM); |
---|
74 | |
---|
75 | # Check LVM version |
---|
76 | my $lvmver=0; |
---|
77 | if (-x $lvmds ) { |
---|
78 | open(LVM,"$lvmds --help 2>&1 |") || mr_exit(-1,"Unable to execute $lvmds"); |
---|
79 | while (<LVM>) { |
---|
80 | if (/Logical Volume Manager/ || /LVM version:/) { |
---|
81 | $lvmver = $_; |
---|
82 | $lvmver =~ s/:([0-9])\..*/$1/; |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | close(LVM); |
---|
87 | |
---|
88 | if ($lvmver == 0) { |
---|
89 | # Still not found |
---|
90 | if (-x $lvmcmd) { |
---|
91 | open(LVM,"$lvmcmd version |") || mr_exit(-1,"Unable to execute $lvmcmd"); |
---|
92 | while (<LVM>) { |
---|
93 | if (/LVM version/) { |
---|
94 | $lvmver = $_; |
---|
95 | $lvmver =~ s/LVM version ([0-9])\..*/$1/; |
---|
96 | } |
---|
97 | } |
---|
98 | close(LVM); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | if ($lvmver == 0) { |
---|
103 | # Still not found |
---|
104 | mr_exit(-1,"Unable to determine LVM version.\nPlease report to the dev team with the result of the commands\n$lvmds and $lvmcmd"); |
---|
105 | } elsif ($lvmver == 1) { |
---|
106 | $lvmcmd = "$lvmpath"; |
---|
107 | } elsif ($lvmver == 2) { |
---|
108 | $lvmcmd .= " "; |
---|
109 | } else { |
---|
110 | pb_log(0,"Unknown LVM version $lvmver"); |
---|
111 | } |
---|
112 | # Here $lvmcmd contains a full path name |
---|
113 | pb_log(1,"Found LVM version $lvmver"); |
---|
114 | return ($lvmver,$lvmcmd); |
---|
115 | |
---|
116 | } |
---|
117 | |
---|
118 | =over 4 |
---|
119 | |
---|
120 | =item B<mr_lvm_analyze> |
---|
121 | |
---|
122 | This function outputs in a file descriptor the LVM analysis done |
---|
123 | It returns 1 parameters, the LVM version or 0 if no LVM |
---|
124 | |
---|
125 | =cut |
---|
126 | |
---|
127 | sub mr_lvm_analyze { |
---|
128 | |
---|
129 | my $OUTPUT = shift; |
---|
130 | |
---|
131 | my ($lvmver,$lvmcmd) = mr_lvm_check(); |
---|
132 | return(0) if ($lvmver == 0); |
---|
133 | |
---|
134 | print $OUTPUT "LVM:$lvmver"; |
---|
135 | |
---|
136 | # Analyze the existing physical volumes |
---|
137 | open(LVM,$lvmcmd."pvdisplay -c |") || mr_exit(-1,"Unable to execute ".$lvmcmd."pvdisplay -c"); |
---|
138 | while (<LVM>) { |
---|
139 | print $OUTPUT "PV:$_"; |
---|
140 | } |
---|
141 | close(LVM); |
---|
142 | |
---|
143 | # Analyze the existing volume groups |
---|
144 | open(LVM,$lvmcmd."vgdisplay -c |") || mr_exit(-1,"Unable to execute ".$lvmcmd."vgdisplay -c"); |
---|
145 | while (<LVM>) { |
---|
146 | print $OUTPUT "VG:$_"; |
---|
147 | } |
---|
148 | close(LVM); |
---|
149 | |
---|
150 | # Analyze the existing logical volumes |
---|
151 | open(LVM,$lvmcmd."lvdisplay -c |") || mr_exit(-1,"Unable to execute ".$lvmcmd."lvdisplay -c"); |
---|
152 | while (<LVM>) { |
---|
153 | print $OUTPUT "LV:$_"; |
---|
154 | } |
---|
155 | close(LVM); |
---|
156 | return($lvmver); |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | =over 4 |
---|
161 | |
---|
162 | =item B<mr_lvm_prepare> |
---|
163 | |
---|
164 | This function outputs in a file descriptor the LVM setup needed to restore LVM conf |
---|
165 | It returns 1 parameters, the LVM version or 0 if no LVM |
---|
166 | |
---|
167 | =cut |
---|
168 | |
---|
169 | sub mr_lvm_prepare { |
---|
170 | |
---|
171 | my $INPUT = shift; |
---|
172 | my $OUTPUT = shift; |
---|
173 | my $mrmult = shift; |
---|
174 | |
---|
175 | my ($lvmver,$lvmcmd) = mr_lvm_check(); |
---|
176 | |
---|
177 | # Generate the startup scrit needed to restore LVM conf |
---|
178 | # from what is given on input |
---|
179 | # Multiply by the multiplier given in input or 1 of none |
---|
180 | |
---|
181 | print $OUTPUT "# Desactivate Volume Groups\n"; |
---|
182 | print $OUTPUT $lvmcmd."vgchange -an\n"; |
---|
183 | print $OUTPUT "\n"; |
---|
184 | |
---|
185 | my $firsttime = 0; |
---|
186 | while (<$INPUT>) { |
---|
187 | if (/^PV:/) { |
---|
188 | my ($tag,$pvname,$vgname,$pvsize,$ipvn,$pvstat,$pvna,$lvnum,$pesize,$petot,$pefree,$pelloc) = split(/:/); |
---|
189 | print $OUTPUT "# Creating Physical Volumes $pvname\n"; |
---|
190 | print $OUTPUT $lvmcmd."pvcreate -ff -y -s ".$pesize*$mrmult." $pvname\n"; |
---|
191 | print $OUTPUT "\n"; |
---|
192 | } elsif (/^VG:/) { |
---|
193 | my ($tag,$vgname,$vgaccess,$vgstat,$vgnum,$lvmaxnum,$lvnum,$ocalvinvg,$lvmaxsize,$pvmaxnum,$cnumpv,$anumpv,$vgsize,$pesize,$penum,$pealloc,$pefree,$uuid) = split(/:/); |
---|
194 | if ($lvmver < 2) { |
---|
195 | print $OUTPUT "# Removing device first as LVM v1 doesn't do it\n"; |
---|
196 | print $OUTPUT "rm -Rf /dev/$vgname\n"; |
---|
197 | } |
---|
198 | $lvmaxnum = 255 if ($lvmaxnum > 256); |
---|
199 | $pvmaxnum = 255 if ($pvmaxnum > 256); |
---|
200 | print $OUTPUT "# Create Volume Group $vgname\n"; |
---|
201 | # Pb sur pesize unite ? |
---|
202 | print $OUTPUT $lvmcmd."vgcreate $vgname -p $pvmaxnum -s $pesize -l $lvmaxnum\n"; |
---|
203 | print $OUTPUT "\n"; |
---|
204 | } elsif (/^LV:/) { |
---|
205 | if ($firsttime eq 0) { |
---|
206 | print $OUTPUT "\n"; |
---|
207 | print $OUTPUT "# Activate All Volume Groups\n"; |
---|
208 | print $OUTPUT $lvmcmd."vgchange -ay\n"; |
---|
209 | print $OUTPUT "\n"; |
---|
210 | $firsttime = 1; |
---|
211 | } |
---|
212 | my ($tag,$lvname,$vgname,$lvaccess,$lvstat,$lvnum,$oclv,$lvsize,$leinlv,$lealloc,$allocpol,$readahead,$major,$minor) = split(/:/); |
---|
213 | print $OUTPUT "# Create Logical Volume $lvname\n"; |
---|
214 | print $OUTPUT $lvmcmd."lvcreate -n $lvname -L ".$lvsize*$mrmult." -r $readahead $vgname\n"; |
---|
215 | #[ "$stripes" ] && output="$output -i $stripes" |
---|
216 | #[ "$stripesize" ] && output="$output -I $stripesize" |
---|
217 | } |
---|
218 | } |
---|
219 | print $OUTPUT "\n"; |
---|
220 | print $OUTPUT "# Scanning again Volume Groups\n"; |
---|
221 | print $OUTPUT $lvmcmd."vgscan\n"; |
---|
222 | print $OUTPUT "\n"; |
---|
223 | |
---|
224 | } |
---|
225 | |
---|
226 | =back |
---|
227 | |
---|
228 | =head1 WEB SITES |
---|
229 | |
---|
230 | The main Web site of the project is available at L<http://www.mondorescue.org/>. Bug reports should be filled using the trac instance of the project at L<http://trac.mondorescue.org/>. |
---|
231 | |
---|
232 | =head1 USER MAILING LIST |
---|
233 | |
---|
234 | The mailing list of the project is available at L<mailto:mondo@lists.sf.net> |
---|
235 | |
---|
236 | =head1 AUTHORS |
---|
237 | |
---|
238 | The Mondorescue.org team L<http://www.mondorescue.org/> lead by Bruno Cornec L<mailto:bruno@mondorescue.org>. |
---|
239 | |
---|
240 | =head1 COPYRIGHT |
---|
241 | |
---|
242 | This module is distributed under the GPL v2.0 license |
---|
243 | described in the file C<COPYING> included with the distribution. |
---|
244 | |
---|
245 | |
---|
246 | =cut |
---|
247 | |
---|
248 | 1; |
---|
249 | |
---|