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