[2118] | 1 | #!/usr/bin/perl -w
|
---|
| 2 | #
|
---|
[2149] | 3 | # Mindi subroutines related to LVM brought by the MondoRescue project
|
---|
[2118] | 4 | #
|
---|
| 5 | # $Id$
|
---|
| 6 | #
|
---|
| 7 | # Copyright B. Cornec 2008
|
---|
| 8 | # Provided under the GPL v2
|
---|
| 9 |
|
---|
[2540] | 10 | package MondoRescue::LVM;
|
---|
[2118] | 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;
|
---|
[2541] | 18 | use MondoRescue::Base;
|
---|
[2118] | 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);
|
---|
[2149] | 28 | our @EXPORT = qw(mr_lvm_check mr_lvm_analyze mr_lvm_prepare);
|
---|
[2118] | 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
|
---|
[2149] | 48 | If LVM version is null then no LVM Handling should be done.
|
---|
[2118] | 49 |
|
---|
| 50 | =cut
|
---|
| 51 |
|
---|
| 52 | sub mr_lvm_check {
|
---|
| 53 |
|
---|
[2149] | 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");
|
---|
[2540] | 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}};
|
---|
[2118] | 60 |
|
---|
[2541] | 61 | # That file is not mandatory anymore
|
---|
[2149] | 62 | if (! -x $lvmproc) {
|
---|
[2541] | 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 | }
|
---|
[2149] | 73 | }
|
---|
[2541] | 74 | close(LVM);
|
---|
[2118] | 75 | }
|
---|
| 76 |
|
---|
| 77 | # Check LVM version
|
---|
[2541] | 78 | my $lvmver = 0;
|
---|
| 79 | if (-x $lvmds) {
|
---|
| 80 | pb_log(2,"Checking with $lvmds\n");
|
---|
[2118] | 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 = $_;
|
---|
[2541] | 85 | chomp($lvmver);
|
---|
[2118] | 86 | $lvmver =~ s/:([0-9])\..*/$1/;
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
[2541] | 89 | close(LVM);
|
---|
| 90 | pb_log(2,"Found a LVM version of $lvmver with $lvmds --help\n");
|
---|
[2118] | 91 | }
|
---|
| 92 |
|
---|
| 93 | if ($lvmver == 0) {
|
---|
[2541] | 94 | pb_log(2,"LVM version value is still not known\n");
|
---|
[2118] | 95 | if (-x $lvmcmd) {
|
---|
[2541] | 96 | pb_log(2,"Checking with $lvmcmd\n");
|
---|
[2118] | 97 | open(LVM,"$lvmcmd version |") || mr_exit(-1,"Unable to execute $lvmcmd");
|
---|
| 98 | while (<LVM>) {
|
---|
| 99 | if (/LVM version/) {
|
---|
| 100 | $lvmver = $_;
|
---|
[2541] | 101 | chomp($lvmver);
|
---|
| 102 | $lvmver =~ s/:([0-9])\..*/$1/;
|
---|
| 103 | $lvmver =~ s/[\s]*LVM version[:]*[\s]+([0-9])\..*/$1/;
|
---|
[2118] | 104 | }
|
---|
| 105 | }
|
---|
| 106 | close(LVM);
|
---|
[2541] | 107 | pb_log(2,"Found a LVM version of $lvmver with $lvmcmd version\n");
|
---|
[2118] | 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | if ($lvmver == 0) {
|
---|
| 112 | # Still not found
|
---|
[2541] | 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");
|
---|
[2118] | 114 | } elsif ($lvmver == 1) {
|
---|
[2149] | 115 | $lvmcmd = "$lvmpath";
|
---|
| 116 | } elsif ($lvmver == 2) {
|
---|
| 117 | $lvmcmd .= " ";
|
---|
| 118 | } else {
|
---|
[2541] | 119 | pb_log(0,"Unknown LVM version $lvmver\n");
|
---|
[2118] | 120 | }
|
---|
[2149] | 121 | # Here $lvmcmd contains a full path name
|
---|
[2541] | 122 | pb_log(1,"Found LVM version $lvmver\n");
|
---|
[2118] | 123 | return ($lvmver,$lvmcmd);
|
---|
| 124 |
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[2149] | 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;
|
---|
[2542] | 139 | my $lvm;
|
---|
[2149] | 140 |
|
---|
| 141 | my ($lvmver,$lvmcmd) = mr_lvm_check();
|
---|
| 142 | return(0) if ($lvmver == 0);
|
---|
| 143 |
|
---|
[2542] | 144 | print $OUTPUT "LVM:$lvmver\n";
|
---|
[2149] | 145 |
|
---|
| 146 | # Analyze the existing physical volumes
|
---|
[2542] | 147 | #open(LVM,$lvmcmd."pvdisplay -c |") || mr_exit(-1,"Unable to execute ".$lvmcmd."pvdisplay -c");
|
---|
| 148 | open(LVM,$lvmcmd."pvs --noheadings --nosuffix --units m --separator : |") || mr_exit(-1,"Unable to execute ".$lvmcmd."pvs");
|
---|
[2149] | 149 | while (<LVM>) {
|
---|
[2542] | 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;
|
---|
[2149] | 155 | print $OUTPUT "PV:$_";
|
---|
| 156 | }
|
---|
| 157 | close(LVM);
|
---|
| 158 |
|
---|
| 159 | # Analyze the existing volume groups
|
---|
[2542] | 160 | #open(LVM,$lvmcmd."vgdisplay -c |") || mr_exit(-1,"Unable to execute ".$lvmcmd."vgdisplay -c");
|
---|
| 161 | open(LVM,$lvmcmd."vgs --noheadings --nosuffix --units m --separator : |") || mr_exit(-1,"Unable to execute ".$lvmcmd."vgs");
|
---|
[2149] | 162 | while (<LVM>) {
|
---|
[2542] | 163 | s/^[\s]*//;
|
---|
[2149] | 164 | print $OUTPUT "VG:$_";
|
---|
| 165 | }
|
---|
| 166 | close(LVM);
|
---|
| 167 |
|
---|
| 168 | # Analyze the existing logical volumes
|
---|
[2542] | 169 | #open(LVM,$lvmcmd."lvdisplay -c |") || mr_exit(-1,"Unable to execute ".$lvmcmd."lvdisplay -c");
|
---|
| 170 | open(LVM,$lvmcmd."lvs --noheadings --nosuffix --units m --separator : |") || mr_exit(-1,"Unable to execute ".$lvmcmd."lvs");
|
---|
[2149] | 171 | while (<LVM>) {
|
---|
[2542] | 172 | s/^[\s]*//;
|
---|
[2149] | 173 | print $OUTPUT "LV:$_";
|
---|
| 174 | }
|
---|
| 175 | close(LVM);
|
---|
[2542] | 176 | return($lvm);
|
---|
[2149] | 177 | }
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 | =over 4
|
---|
| 181 |
|
---|
| 182 | =item B<mr_lvm_prepare>
|
---|
| 183 |
|
---|
| 184 | This function outputs in a file descriptor the LVM setup needed to restore LVM conf
|
---|
| 185 | It returns 1 parameters, the LVM version or 0 if no LVM
|
---|
| 186 |
|
---|
| 187 | =cut
|
---|
| 188 |
|
---|
| 189 | sub mr_lvm_prepare {
|
---|
| 190 |
|
---|
| 191 | my $INPUT = shift;
|
---|
| 192 | my $OUTPUT = shift;
|
---|
| 193 | my $mrmult = shift;
|
---|
| 194 |
|
---|
| 195 | my ($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 |
|
---|
| 201 | print $OUTPUT "# Desactivate Volume Groups\n";
|
---|
| 202 | print $OUTPUT $lvmcmd."vgchange -an\n";
|
---|
| 203 | print $OUTPUT "\n";
|
---|
| 204 |
|
---|
| 205 | my $firsttime = 0;
|
---|
| 206 | while (<$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:/) {
|
---|
[2541] | 225 | if ($firsttime == 0) {
|
---|
[2149] | 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 | }
|
---|
| 239 | print $OUTPUT "\n";
|
---|
| 240 | print $OUTPUT "# Scanning again Volume Groups\n";
|
---|
| 241 | print $OUTPUT $lvmcmd."vgscan\n";
|
---|
| 242 | print $OUTPUT "\n";
|
---|
| 243 |
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[2118] | 246 | =back
|
---|
| 247 |
|
---|
| 248 | =head1 WEB SITES
|
---|
| 249 |
|
---|
| 250 | 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/>.
|
---|
| 251 |
|
---|
| 252 | =head1 USER MAILING LIST
|
---|
| 253 |
|
---|
| 254 | The mailing list of the project is available at L<mailto:mondo@lists.sf.net>
|
---|
| 255 |
|
---|
| 256 | =head1 AUTHORS
|
---|
| 257 |
|
---|
| 258 | The Mondorescue.org team L<http://www.mondorescue.org/> lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.
|
---|
| 259 |
|
---|
| 260 | =head1 COPYRIGHT
|
---|
| 261 |
|
---|
| 262 | This module is distributed under the GPL v2.0 license
|
---|
| 263 | described in the file C<COPYING> included with the distribution.
|
---|
| 264 |
|
---|
| 265 |
|
---|
| 266 | =cut
|
---|
| 267 |
|
---|
| 268 | 1;
|
---|
| 269 |
|
---|