source: MondoRescue/devel/mr/lib/MondoRescue/LVM.pm@ 2541

Last change on this file since 2541 was 2541, checked in by Bruno Cornec, 14 years ago

r3577@localhost: bruno | 2010-01-08 02:23:52 +0100
The 2 first commands check and analyze begin to work

File size: 6.6 KB
Line 
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
10package MondoRescue::LVM;
11
12use strict 'vars';
13use Data::Dumper;
14use English;
15use lib qw (lib);
16use ProjectBuilder::Base;
17use ProjectBuilder::Conf;
18use MondoRescue::Base;
19
20# Inherit from the "Exporter" module which handles exporting functions.
21
22use Exporter;
23
24# Export, by default, all the functions into the namespace of
25# any code which uses this module.
26
27our @ISA = qw(Exporter);
28our @EXPORT = qw(mr_lvm_check mr_lvm_analyze mr_lvm_prepare);
29
30=pod
31
32=head1 NAME
33
34MondoRescue::Mindi, part of the mondorescue.org
35
36=head1 DESCRIPTION
37
38This 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
46This function checks the usage of LVM and gets the version used
47It returns 2 parameters, the LVM version, and the lvm command to use if needed
48If LVM version is null then no LVM Handling should be done.
49
50=cut
51
52sub mr_lvm_check {
53
54# Get params from the conf file
55my ($lvmds_t,$lvmproc_t,$lvmcmd_t,$lvmpath_t) = pb_conf_get("mr_lvmdiskscan","mr_lvmprocfile","mr_lvmcmd","mr_lvmpath");
56my $lvmds = $lvmds_t->{$ENV{PBPROJ}};
57my $lvmproc = $lvmproc_t->{$ENV{PBPROJ}};
58my $lvmcmd = $lvmcmd_t->{$ENV{PBPROJ}};
59my $lvmpath = $lvmpath_t->{$ENV{PBPROJ}};
60
61# That file is not mandatory anymore
62if (! -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
78my $lvmver = 0;
79if (-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
93if ($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
111if ($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
122pb_log(1,"Found LVM version $lvmver\n");
123return ($lvmver,$lvmcmd);
124
125}
126
127=over 4
128
129=item B<mr_lvm_analyze>
130
131This function outputs in a file descriptor the LVM analysis done
132It returns 1 parameters, the LVM version or 0 if no LVM
133
134=cut
135
136sub mr_lvm_analyze {
137
138my $OUTPUT = shift;
139
140my ($lvmver,$lvmcmd) = mr_lvm_check();
141return(0) if ($lvmver == 0);
142
143print $OUTPUT "LVM:$lvmver";
144
145# Analyze the existing physical volumes
146open(LVM,$lvmcmd."pvdisplay -c |") || mr_exit(-1,"Unable to execute ".$lvmcmd."pvdisplay -c");
147while (<LVM>) {
148 print $OUTPUT "PV:$_";
149}
150close(LVM);
151
152# Analyze the existing volume groups
153open(LVM,$lvmcmd."vgdisplay -c |") || mr_exit(-1,"Unable to execute ".$lvmcmd."vgdisplay -c");
154while (<LVM>) {
155 print $OUTPUT "VG:$_";
156}
157close(LVM);
158
159# Analyze the existing logical volumes
160open(LVM,$lvmcmd."lvdisplay -c |") || mr_exit(-1,"Unable to execute ".$lvmcmd."lvdisplay -c");
161while (<LVM>) {
162 print $OUTPUT "LV:$_";
163}
164close(LVM);
165return($lvmver);
166}
167
168
169=over 4
170
171=item B<mr_lvm_prepare>
172
173This function outputs in a file descriptor the LVM setup needed to restore LVM conf
174It returns 1 parameters, the LVM version or 0 if no LVM
175
176=cut
177
178sub mr_lvm_prepare {
179
180my $INPUT = shift;
181my $OUTPUT = shift;
182my $mrmult = shift;
183
184my ($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
190print $OUTPUT "# Desactivate Volume Groups\n";
191print $OUTPUT $lvmcmd."vgchange -an\n";
192print $OUTPUT "\n";
193
194my $firsttime = 0;
195while (<$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}
228print $OUTPUT "\n";
229print $OUTPUT "# Scanning again Volume Groups\n";
230print $OUTPUT $lvmcmd."vgscan\n";
231print $OUTPUT "\n";
232
233}
234
235=back
236
237=head1 WEB SITES
238
239The 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
243The mailing list of the project is available at L<mailto:mondo@lists.sf.net>
244
245=head1 AUTHORS
246
247The Mondorescue.org team L<http://www.mondorescue.org/> lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.
248
249=head1 COPYRIGHT
250
251This module is distributed under the GPL v2.0 license
252described in the file C<COPYING> included with the distribution.
253
254
255=cut
256
2571;
258
Note: See TracBrowser for help on using the repository browser.