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

Last change on this file since 2542 was 2542, checked in by bruno, 10 years ago

r3578@localhost: bruno | 2010-01-08 15:12:12 +0100
Begin modifications top replace the *display commands by the *s command in LVM

File size: 7.2 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;
139my $lvm;
140
141my ($lvmver,$lvmcmd) = mr_lvm_check();
142return(0) if ($lvmver == 0);
143
144print $OUTPUT "LVM:$lvmver\n";
145
146# Analyze the existing physical volumes
147#open(LVM,$lvmcmd."pvdisplay -c |") || mr_exit(-1,"Unable to execute ".$lvmcmd."pvdisplay -c");
148open(LVM,$lvmcmd."pvs --noheadings --nosuffix --units m --separator : |") || mr_exit(-1,"Unable to execute ".$lvmcmd."pvs");
149while (<LVM>) {
150        s/^[\s]*//;
151        my ($pv,$vg,$foo,$foo2,$size,$foo3) = split(/:/);
152        $lvm->{$vg}->{'pv'}[$lvm->{$vg}->{'pvnum'}] = $pv;
153        $lvm->{$vg}->{'pvnum'}++;
154        $lvm->{$vg}->{$pv}->{'size'} = $size;
155        print $OUTPUT "PV:$_";
156}
157close(LVM);
158
159# Analyze the existing volume groups
160#open(LVM,$lvmcmd."vgdisplay -c |") || mr_exit(-1,"Unable to execute ".$lvmcmd."vgdisplay -c");
161open(LVM,$lvmcmd."vgs --noheadings --nosuffix --units m --separator : |") || mr_exit(-1,"Unable to execute ".$lvmcmd."vgs");
162while (<LVM>) {
163        s/^[\s]*//;
164        print $OUTPUT "VG:$_";
165}
166close(LVM);
167
168# Analyze the existing logical volumes
169#open(LVM,$lvmcmd."lvdisplay -c |") || mr_exit(-1,"Unable to execute ".$lvmcmd."lvdisplay -c");
170open(LVM,$lvmcmd."lvs --noheadings --nosuffix --units m --separator : |") || mr_exit(-1,"Unable to execute ".$lvmcmd."lvs");
171while (<LVM>) {
172        s/^[\s]*//;
173        print $OUTPUT "LV:$_";
174}
175close(LVM);
176return($lvm);
177}
178
179
180=over 4
181
182=item B<mr_lvm_prepare>
183
184This function outputs in a file descriptor the LVM setup needed to restore LVM conf
185It returns 1 parameters, the LVM version or 0 if no LVM
186
187=cut
188
189sub mr_lvm_prepare {
190
191my $INPUT = shift;
192my $OUTPUT = shift;
193my $mrmult = shift;
194
195my ($lvmver,$lvmcmd) = mr_lvm_check();
196
197# Generate the startup scrit needed to restore LVM conf
198# from what is given on input
199# Multiply by the multiplier given in input or 1 of none
200
201print $OUTPUT "# Desactivate Volume Groups\n";
202print $OUTPUT $lvmcmd."vgchange -an\n";
203print $OUTPUT "\n";
204
205my $firsttime = 0;
206while (<$INPUT>) {
207    if (/^PV:/) {
208        my ($tag,$pvname,$vgname,$pvsize,$ipvn,$pvstat,$pvna,$lvnum,$pesize,$petot,$pefree,$pelloc) = split(/:/);
209        print $OUTPUT "# Creating Physical Volumes $pvname\n";
210        print $OUTPUT $lvmcmd."pvcreate -ff -y -s ".$pesize*$mrmult." $pvname\n";
211        print $OUTPUT "\n";
212    } elsif (/^VG:/) {
213        my ($tag,$vgname,$vgaccess,$vgstat,$vgnum,$lvmaxnum,$lvnum,$ocalvinvg,$lvmaxsize,$pvmaxnum,$cnumpv,$anumpv,$vgsize,$pesize,$penum,$pealloc,$pefree,$uuid) = split(/:/);
214        if ($lvmver < 2) {
215            print $OUTPUT "# Removing device first as LVM v1 doesn't do it\n";
216            print $OUTPUT "rm -Rf /dev/$vgname\n";
217        }
218        $lvmaxnum = 255 if ($lvmaxnum > 256);
219        $pvmaxnum = 255 if ($pvmaxnum > 256);
220        print $OUTPUT "# Create Volume Group $vgname\n";
221        # Pb sur pesize unite ?
222        print $OUTPUT $lvmcmd."vgcreate $vgname -p $pvmaxnum -s $pesize -l $lvmaxnum\n";
223        print $OUTPUT "\n";
224    } elsif (/^LV:/) {
225        if ($firsttime == 0) {
226            print $OUTPUT "\n";
227            print $OUTPUT "# Activate All Volume Groups\n";
228            print $OUTPUT $lvmcmd."vgchange -ay\n";
229            print $OUTPUT "\n";
230            $firsttime = 1;
231        }
232        my ($tag,$lvname,$vgname,$lvaccess,$lvstat,$lvnum,$oclv,$lvsize,$leinlv,$lealloc,$allocpol,$readahead,$major,$minor) = split(/:/);
233        print $OUTPUT "# Create Logical Volume $lvname\n";
234        print $OUTPUT $lvmcmd."lvcreate -n $lvname -L ".$lvsize*$mrmult." -r $readahead $vgname\n";
235        #[ "$stripes" ]    && output="$output -i $stripes"
236        #[ "$stripesize" ] && output="$output -I $stripesize"
237    }
238}
239print $OUTPUT "\n";
240print $OUTPUT "# Scanning again Volume Groups\n";
241print $OUTPUT $lvmcmd."vgscan\n";
242print $OUTPUT "\n";
243
244}
245
246=back
247
248=head1 WEB SITES
249
250The 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/>.
251
252=head1 USER MAILING LIST
253
254The mailing list of the project is available at L<mailto:mondo@lists.sf.net>
255 
256=head1 AUTHORS
257
258The Mondorescue.org team L<http://www.mondorescue.org/> lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.
259
260=head1 COPYRIGHT
261
262This module is distributed under the GPL v2.0 license
263described in the file C<COPYING> included with the distribution.
264
265
266=cut
267
2681;
269
Note: See TracBrowser for help on using the repository browser.