source: devel/mindi/lib/MondoRescue/Mindi/LVM.pm @ 2149

Last change on this file since 2149 was 2149, checked in by Bruno Cornec, 11 years ago

Begining f devl branch coding nd preliminary organisation

File size: 6.1 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::Mindi::LVM;
11
12use strict 'vars';
13use Data::Dumper;
14use English;
15use lib qw (lib);
16use ProjectBuilder::Base;
17use ProjectBuilder::Conf;
18
19# Inherit from the "Exporter" module which handles exporting functions.
20
21use Exporter;
22
23# Export, by default, all the functions into the namespace of
24# any code which uses this module.
25
26our @ISA = qw(Exporter);
27our @EXPORT = qw(mr_lvm_check mr_lvm_analyze mr_lvm_prepare);
28
29=pod
30
31=head1 NAME
32
33MondoRescue::Mindi, part of the mondorescue.org
34
35=head1 DESCRIPTION
36
37This 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
45This function checks the usage of LVM and gets the version used
46It returns 2 parameters, the LVM version, and the lvm command to use if needed
47If LVM version is null then no LVM Handling should be done.
48
49=cut
50
51sub mr_lvm_check {
52
53# Get params from the conf file
54my ($lvmds_t,$lvmproc_t,$lvmcmd_t,$lvmpath_t) = pb_conf_get("mr_lvmdiskscan","mr_lvmprocfile","mr_lvmcmd","mr_lvmpath");
55my $lvmds = $lvmds_t->{'mindi'};
56my $lvmproc = $lvmproc_t->{'mindi'};
57my $lvmcmd = $lvmcmd_t->{'mindi'};
58my $lvmpath = $lvmpath_t->{'mindi'};
59
60if (! -x $lvmproc) {
61    pb_log(1,"$lvmproc doesn't exist.");
62    return(0,undef);
63    }
64
65# Check LVM volumes presence
66open(LVM,$lvmproc) || mr_exit(-1,"Unable to open $lvmproc");
67while (<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}
73close(LVM);
74
75# Check LVM version
76my $lvmver=0;
77if (-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}
86close(LVM);
87
88if ($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
102if ($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
113pb_log(1,"Found LVM version $lvmver");
114return ($lvmver,$lvmcmd);
115
116}
117
118=over 4
119
120=item B<mr_lvm_analyze>
121
122This function outputs in a file descriptor the LVM analysis done
123It returns 1 parameters, the LVM version or 0 if no LVM
124
125=cut
126
127sub mr_lvm_analyze {
128
129my $OUTPUT = shift;
130
131my ($lvmver,$lvmcmd) = mr_lvm_check();
132return(0) if ($lvmver == 0);
133
134print $OUTPUT "LVM:$lvmver";
135
136# Analyze the existing physical volumes
137open(LVM,$lvmcmd."pvdisplay -c |") || mr_exit(-1,"Unable to execute ".$lvmcmd."pvdisplay -c");
138while (<LVM>) {
139        print $OUTPUT "PV:$_";
140}
141close(LVM);
142
143# Analyze the existing volume groups
144open(LVM,$lvmcmd."vgdisplay -c |") || mr_exit(-1,"Unable to execute ".$lvmcmd."vgdisplay -c");
145while (<LVM>) {
146        print $OUTPUT "VG:$_";
147}
148close(LVM);
149
150# Analyze the existing logical volumes
151open(LVM,$lvmcmd."lvdisplay -c |") || mr_exit(-1,"Unable to execute ".$lvmcmd."lvdisplay -c");
152while (<LVM>) {
153        print $OUTPUT "LV:$_";
154}
155close(LVM);
156return($lvmver);
157}
158
159
160=over 4
161
162=item B<mr_lvm_prepare>
163
164This function outputs in a file descriptor the LVM setup needed to restore LVM conf
165It returns 1 parameters, the LVM version or 0 if no LVM
166
167=cut
168
169sub mr_lvm_prepare {
170
171my $INPUT = shift;
172my $OUTPUT = shift;
173my $mrmult = shift;
174
175my ($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
181print $OUTPUT "# Desactivate Volume Groups\n";
182print $OUTPUT $lvmcmd."vgchange -an\n";
183print $OUTPUT "\n";
184
185my $firsttime = 0;
186while (<$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}
219print $OUTPUT "\n";
220print $OUTPUT "# Scanning again Volume Groups\n";
221print $OUTPUT $lvmcmd."vgscan\n";
222print $OUTPUT "\n";
223
224}
225
226=back
227
228=head1 WEB SITES
229
230The 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
234The mailing list of the project is available at L<mailto:mondo@lists.sf.net>
235 
236=head1 AUTHORS
237
238The Mondorescue.org team L<http://www.mondorescue.org/> lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.
239
240=head1 COPYRIGHT
241
242This module is distributed under the GPL v2.0 license
243described in the file C<COPYING> included with the distribution.
244
245
246=cut
247
2481;
249
Note: See TracBrowser for help on using the repository browser.