[2076] | 1 | #!/usr/bin/perl -w
|
---|
| 2 | #
|
---|
| 3 | # Analyze the LVM configuration
|
---|
| 4 | # and stor the configuration for restore time
|
---|
| 5 | #
|
---|
| 6 | # $Id$
|
---|
| 7 | #
|
---|
| 8 | # Copyright B. Cornec 2008
|
---|
| 9 | # Provided under the GPL v2
|
---|
| 10 |
|
---|
| 11 | # Syntax: see below
|
---|
| 12 |
|
---|
| 13 | use strict 'vars';
|
---|
| 14 | use Getopt::Long qw(:config auto_abbrev no_ignore_case);
|
---|
| 15 | use Data::Dumper;
|
---|
| 16 | use English;
|
---|
| 17 | use File::Basename;
|
---|
| 18 | use File::Copy;
|
---|
| 19 | use File::stat;
|
---|
| 20 | use File::Temp qw(tempdir);
|
---|
| 21 | use POSIX qw(strftime);
|
---|
| 22 | use lib qw (lib);
|
---|
| 23 | use ProjectBuilder::Base;
|
---|
| 24 | use ProjectBuilder::Distribution;
|
---|
| 25 |
|
---|
| 26 | =pod
|
---|
| 27 |
|
---|
| 28 | =head1 NAME
|
---|
| 29 |
|
---|
[2119] | 30 | mrprepare-lvm - A Mindi Tool to restore the LVM configuration and apply it
|
---|
[2076] | 31 |
|
---|
| 32 | =head1 DESCRIPTION
|
---|
| 33 |
|
---|
[2119] | 34 | B<mrprepare-lvm> gets all the information related to the LVM configuration from stdin or a file and prepare a restoration script
|
---|
[2076] | 35 |
|
---|
| 36 | =head1 SYNOPSIS
|
---|
| 37 |
|
---|
[2119] | 38 | mrprepare-lvm [-v]|[-q]|[-h]|[--man][-i inputfile][-l logfile][-m multiplier]
|
---|
[2076] | 39 |
|
---|
| 40 | =head1 OPTIONS
|
---|
| 41 |
|
---|
| 42 | =over 4
|
---|
| 43 |
|
---|
| 44 | =item B<-v|--verbose>
|
---|
| 45 |
|
---|
[2119] | 46 | Be more verbose
|
---|
[2076] | 47 |
|
---|
| 48 | =item B<-q|--quiet>
|
---|
| 49 |
|
---|
| 50 | Do not print any output.
|
---|
| 51 |
|
---|
| 52 | =item B<-h|--help>
|
---|
| 53 |
|
---|
| 54 | Print a brief help message and exits.
|
---|
| 55 |
|
---|
| 56 | =item B<--man>
|
---|
| 57 |
|
---|
| 58 | Prints the manual page and exits.
|
---|
| 59 |
|
---|
[2119] | 60 | =item B<-i|--input>
|
---|
[2076] | 61 |
|
---|
[2119] | 62 | Name of the file to get input from. Use stdin by default
|
---|
[2076] | 63 |
|
---|
[2119] | 64 | The input format is:
|
---|
| 65 | LVM:lvm-version
|
---|
| 66 | PV:pv-information as done with pvdisplay -c
|
---|
| 67 | VG:vg-information as done with vgdisplay -c
|
---|
| 68 | LV:lv-information as done with lvdisplay -c
|
---|
| 69 |
|
---|
| 70 | =item B<-o|--output>
|
---|
| 71 |
|
---|
| 72 | Name of the file to write output to. Use stdout by default.
|
---|
| 73 | The output file is a srpit ready to run in order to setup correctly LVM
|
---|
| 74 |
|
---|
| 75 |
|
---|
[2076] | 76 | =back
|
---|
| 77 |
|
---|
| 78 | =head1 WEB SITES
|
---|
| 79 |
|
---|
| 80 | 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/>.
|
---|
| 81 |
|
---|
| 82 | =head1 USER MAILING LIST
|
---|
| 83 |
|
---|
[2119] | 84 | The mailing list of the project is available at L<mailto:mondo@lists.sf.net>
|
---|
[2076] | 85 |
|
---|
| 86 | =head1 AUTHORS
|
---|
| 87 |
|
---|
| 88 | The Mondorescue.org team L<http://www.mondorescue.org/> lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.
|
---|
| 89 |
|
---|
| 90 | =head1 COPYRIGHT
|
---|
| 91 |
|
---|
| 92 | Analyze-LVM is distributed under the GPL v2.0 license
|
---|
| 93 | described in the file C<COPYING> included with the distribution.
|
---|
| 94 |
|
---|
| 95 | =cut
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | # ---------------------------------------------------------------------------
|
---|
[2119] | 99 | # Globals
|
---|
| 100 | my %opts; # CLI Options
|
---|
[2076] | 101 |
|
---|
| 102 | # Initialize the syntax string
|
---|
| 103 |
|
---|
[2119] | 104 | pb_syntax_init("mrprepare-lvm Version PBVER-rPBREV\n");
|
---|
[2076] | 105 |
|
---|
| 106 | # Handle options
|
---|
| 107 | #
|
---|
| 108 | GetOptions("help|?|h" => \$opts{'h'},
|
---|
| 109 | "man" => \$opts{'man'},
|
---|
| 110 | "verbose|v+" => \$opts{'v'},
|
---|
| 111 | "quiet|q" => \$opts{'q'},
|
---|
[2119] | 112 | "input|i=s" => \$opts{'o'},
|
---|
| 113 | "output|o=s" => \$opts{'o'},
|
---|
| 114 | "multiplier|m=s" => \$opts{'o'},
|
---|
[2076] | 115 | "log-files|l=s" => \$opts{'l'},
|
---|
| 116 | "version|V" => \$opts{'V'},
|
---|
| 117 | ) || pb_syntax(-1,0);
|
---|
| 118 |
|
---|
| 119 | # easy options
|
---|
| 120 | if (defined $opts{'h'}) {
|
---|
| 121 | pb_syntax(0,1);
|
---|
| 122 | }
|
---|
| 123 | if (defined $opts{'man'}) {
|
---|
| 124 | pb_syntax(0,2);
|
---|
| 125 | }
|
---|
| 126 | if (defined $opts{'v'}) {
|
---|
| 127 | $pbdebug = $opts{'v'};
|
---|
| 128 | }
|
---|
| 129 | if (defined $opts{'q'}) {
|
---|
[2119] | 130 | $pbdebug = -1;
|
---|
[2076] | 131 | }
|
---|
[2119] | 132 | my $mrmult = 1;
|
---|
| 133 | if (defined $opts{'m'}) {
|
---|
| 134 | $mrmult = $opts{'m'};
|
---|
| 135 | }
|
---|
[2076] | 136 |
|
---|
| 137 | #
|
---|
| 138 | # Global variables
|
---|
| 139 | #
|
---|
| 140 | my $MINDI_VERSION = "PBVER-rPBREV";
|
---|
| 141 | my $MINDI_PREFIX = "PBPREFIX";
|
---|
| 142 | my $MINDI_CONF = "PBCONF";
|
---|
| 143 | my $MINDI_LIB = "PBLIB";
|
---|
| 144 | my $MINDI_SBIN = "$MINDI_PREFIX/sbin";
|
---|
| 145 | #
|
---|
| 146 | # Temp dir
|
---|
| 147 | #
|
---|
| 148 | pb_temp_init();
|
---|
| 149 |
|
---|
| 150 | # -------------------------------- main -----------------------------------
|
---|
[2119] | 151 | my ($lvmver,$lvmcmd) = mr_lvm_check();
|
---|
[2076] | 152 |
|
---|
[2119] | 153 | # Where to send the output
|
---|
| 154 | my $OUTPUT = \*STDOUT;
|
---|
| 155 | if (defined $opts{'o'}) {
|
---|
| 156 | open(OUTPUT, "> $opts{'o'}") || mr_exit(-1, "Unable to write to $opts{'o'}");
|
---|
| 157 | $OUTPUT = \*OUTPUT;
|
---|
[2076] | 158 | }
|
---|
| 159 |
|
---|
[2119] | 160 | # Where to get the input
|
---|
| 161 | my $INPUT = \*STDIN;
|
---|
| 162 | if (defined $opts{'i'}) {
|
---|
| 163 | open(INPUT, " $opts{'i'}") || mr_exit(-1, "Unable to read from $opts{'i'}");
|
---|
| 164 | $INPUT = \*INPUT;
|
---|
[2076] | 165 | }
|
---|
| 166 |
|
---|
[2119] | 167 |
|
---|
| 168 | # Generate the startup scrit needed to restore LVM conf
|
---|
| 169 | # from what is given on input
|
---|
| 170 | # Multiply by the multiplier given in input or 1 of none
|
---|
| 171 | print $OUTPUT "# Desactivate Volume Groups\n";
|
---|
| 172 | print $OUTPUT "$lvmcmd vgchange -an\n";
|
---|
| 173 | print $OUTPUT "\n";
|
---|
| 174 |
|
---|
| 175 | my $firsttime = 0;
|
---|
| 176 | while (<INPUT>) {
|
---|
| 177 | if (/^PV:/) {
|
---|
| 178 | my ($tag,$pvname,$vgname,$pvsize,$ipvn,$pvstat,$pvna,$lvnum,$pesize,$petot,$pefree,$pelloc) = split(/:/);
|
---|
| 179 | print $OUTPUT "# Creating Physical Volumes $pvname\n";
|
---|
| 180 | print $OUTPUT "$lvmcmd pvcreate -ff -y -s ".$pesize*$mrmult." $pvname\n";
|
---|
| 181 | print $OUTPUT "\n";
|
---|
| 182 | } elsif (/^VG:/) {
|
---|
| 183 | my ($tag,$vgname,$vgaccess,$vgstat,$vgnum,$lvmaxnum,$lvnum,$ocalvinvg,$lvmaxsize,$pvmaxnum,$cnumpv,$anumpv,$vgsize,$pesize,$penum,$pealloc,$pefree,$uuid) = split(/:/);
|
---|
| 184 | if ($lvmver < 2) {
|
---|
| 185 | print $OUTPUT "# Removing device first as LVM v1 doesn't do it\n";
|
---|
| 186 | print $OUTPUT "rm -Rf /dev/$vgname\n";
|
---|
[2076] | 187 | }
|
---|
[2119] | 188 | $lvmaxnum = 255 if ($lvmaxnum > 256);
|
---|
| 189 | $pvmaxnum = 255 if ($pvmaxnum > 256);
|
---|
| 190 | print $OUTPUT "# Create Volume Group $vgname\n";
|
---|
| 191 | # Pb sur pesize unite ?
|
---|
| 192 | print $OUTPUT "$lvmcmd vgcreate $vgname -p $pvmaxnum -s $pesize -l $lvmaxnum\n";
|
---|
| 193 | print $OUTPUT "\n";
|
---|
| 194 | } elsif (/^LV:/) {
|
---|
| 195 | if ($firsttime eq 0) {
|
---|
| 196 | print $OUTPUT "\n";
|
---|
| 197 | print $OUTPUT "# Activate All Volume Groups\n";
|
---|
| 198 | print $OUTPUT "$lvmcmd vgchange -ay\n";
|
---|
| 199 | print $OUTPUT "\n";
|
---|
[2135] | 200 | $firsttime = 1;
|
---|
[2119] | 201 | }
|
---|
| 202 | my ($tag,$lvname,$vgname,$lvaccess,$lvstat,$lvnum,$oclv,$lvsize,$leinlv,$lealloc,$allocpol,$readahead,$major,$minor) = split(/:/);
|
---|
| 203 | print $OUTPUT "# Create Logical Volume $lvname\n";
|
---|
| 204 | print $OUTPUT "$lvmcmd lvcreate -n $lvname -L ".$lvsize*$mrmult." -r $readahead $vgname\n";
|
---|
| 205 | #[ "$stripes" ] && output="$output -i $stripes"
|
---|
| 206 | #[ "$stripesize" ] && output="$output -I $stripesize"
|
---|
[2076] | 207 | }
|
---|
| 208 | }
|
---|
[2119] | 209 | print $OUTPUT "\n";
|
---|
| 210 | print $OUTPUT "# Scanning again Volume Groups\n";
|
---|
| 211 | print $OUTPUT "$lvmcmd vgscan\n";
|
---|
| 212 | print $OUTPUT "\n";
|
---|
[2076] | 213 |
|
---|
| 214 |
|
---|
[2119] | 215 | #WriteShutdownScript
|
---|
| 216 | mr_exit(0,"End of mrprepare-lvm");
|
---|
[2076] | 217 |
|
---|
[2119] | 218 | sub mr_exit {
|
---|
[2076] | 219 |
|
---|
[2119] | 220 | my $code = shift;
|
---|
| 221 | my $msg = shift || "";
|
---|
[2076] | 222 |
|
---|
[2119] | 223 | close(OUTPUT);
|
---|
| 224 | print $msg."\n";
|
---|
| 225 | die "ERROR returned\n" if ($code < 0);
|
---|
| 226 | print "No LVM handling\n" if ($code > 0);
|
---|
| 227 | exit(0);
|
---|
[2076] | 228 | }
|
---|