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 |
|
---|
30 | Analyze-lvm - A Mindi Tool to analyze the LVM configuration and store it
|
---|
31 |
|
---|
32 | =head1 DESCRIPTION
|
---|
33 |
|
---|
34 | B<anlyze-lvm> prints all the information related to the LVM configuration that may be used by a restoration process
|
---|
35 |
|
---|
36 | =head1 SYNOPSIS
|
---|
37 |
|
---|
38 | analyze-lvm [-v]|[-q]|[-h]|[--man]
|
---|
39 |
|
---|
40 | =head1 OPTIONS
|
---|
41 |
|
---|
42 | =over 4
|
---|
43 |
|
---|
44 | =item B<-v|--verbose>
|
---|
45 |
|
---|
46 | Print a brief help message and exits.
|
---|
47 |
|
---|
48 |
|
---|
49 | =item B<-q|--quiet>
|
---|
50 |
|
---|
51 | Do not print any output.
|
---|
52 |
|
---|
53 | =item B<-h|--help>
|
---|
54 |
|
---|
55 | Print a brief help message and exits.
|
---|
56 |
|
---|
57 | =item B<--man>
|
---|
58 |
|
---|
59 | Prints the manual page and exits.
|
---|
60 |
|
---|
61 | =item B<-i|--iso iso_image>
|
---|
62 |
|
---|
63 | Name of the ISO image you want to created.
|
---|
64 |
|
---|
65 | =back
|
---|
66 |
|
---|
67 | =head1 WEB SITES
|
---|
68 |
|
---|
69 | 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/>.
|
---|
70 |
|
---|
71 | =head1 USER MAILING LIST
|
---|
72 |
|
---|
73 | The miling list of the project is available at L<mailto:mondo@lists.sf.net>
|
---|
74 |
|
---|
75 | =head1 AUTHORS
|
---|
76 |
|
---|
77 | The Mondorescue.org team L<http://www.mondorescue.org/> lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.
|
---|
78 |
|
---|
79 | =head1 COPYRIGHT
|
---|
80 |
|
---|
81 | Analyze-LVM is distributed under the GPL v2.0 license
|
---|
82 | described in the file C<COPYING> included with the distribution.
|
---|
83 |
|
---|
84 | =cut
|
---|
85 |
|
---|
86 |
|
---|
87 | # ---------------------------------------------------------------------------
|
---|
88 |
|
---|
89 | # Initialize the syntax string
|
---|
90 |
|
---|
91 | pb_syntax_init("analyze-lvm Version PBVER-rPBREV\n");
|
---|
92 |
|
---|
93 | # Handle options
|
---|
94 | #
|
---|
95 | GetOptions("help|?|h" => \$opts{'h'},
|
---|
96 | "man" => \$opts{'man'},
|
---|
97 | "verbose|v+" => \$opts{'v'},
|
---|
98 | "quiet|q" => \$opts{'q'},
|
---|
99 | "log-files|l=s" => \$opts{'l'},
|
---|
100 | "version|V" => \$opts{'V'},
|
---|
101 | ) || pb_syntax(-1,0);
|
---|
102 |
|
---|
103 | # easy options
|
---|
104 | if (defined $opts{'h'}) {
|
---|
105 | pb_syntax(0,1);
|
---|
106 | }
|
---|
107 | if (defined $opts{'man'}) {
|
---|
108 | pb_syntax(0,2);
|
---|
109 | }
|
---|
110 | if (defined $opts{'v'}) {
|
---|
111 | $pbdebug = $opts{'v'};
|
---|
112 | }
|
---|
113 | if (defined $opts{'q'}) {
|
---|
114 | $pbdebug=-1;
|
---|
115 | }
|
---|
116 |
|
---|
117 | #
|
---|
118 | # Global variables
|
---|
119 | #
|
---|
120 | my $MINDI_VERSION = "PBVER-rPBREV";
|
---|
121 | my $MINDI_PREFIX = "PBPREFIX";
|
---|
122 | my $MINDI_CONF = "PBCONF";
|
---|
123 | my $MINDI_LIB = "PBLIB";
|
---|
124 | my $MINDI_SBIN = "$MINDI_PREFIX/sbin";
|
---|
125 | #
|
---|
126 | # Temp dir
|
---|
127 | #
|
---|
128 | pb_temp_init();
|
---|
129 |
|
---|
130 | my $lvmds = "/usr/sbin/lvmdiskscan";
|
---|
131 | my $lvmproc = "/proc/lvm/global";
|
---|
132 | my $lvmcmd = "/usr/sbi/lvm";
|
---|
133 |
|
---|
134 | # -------------------------------- main -----------------------------------
|
---|
135 | mr_exit(-1,"$lvmds doesn't exist. No LVM handling.") if ((! -x $lvmds) ;
|
---|
136 | mr_exit(-1,"$lvmproc doesn't exist. No LVM handling.") if ((! -x $lvmproc) ;
|
---|
137 |
|
---|
138 | # Check LVM volumes presence
|
---|
139 | open(LVM,$lvmproc) || mr_exit(-1,"Unable to open $lvmproc");
|
---|
140 | while (<LVM>) {
|
---|
141 | mr_exit(1,"No LVM volumes found in $lvmproc") if (/0 VGs 0 PVs 0 LVs/);
|
---|
142 | }
|
---|
143 | close(LVM);
|
---|
144 |
|
---|
145 | # Check LVM version
|
---|
146 | my $lvmver=0;
|
---|
147 | open(LVM,"$lvmds --help 2>&1 |") || mr_exit(-1,"Unable to execute $lvmds");
|
---|
148 | while (<LVM>) {
|
---|
149 | if (/Logical Volume Manager/ || /LVM version:/) {
|
---|
150 | $lvmver = $_;
|
---|
151 | $lvmver =~ s/:([0-9])\..*/$1/;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | close(LVM);
|
---|
155 | #lvmversion=`lvmdiskscan --help 2>&1 |
|
---|
156 | #grep -E "Logical Volume Manager|LVM version:" |
|
---|
157 | #cut -d: -f2 | cut -d. -f1 |
|
---|
158 | #awk '{print $NF}' |
|
---|
159 | #sed -e 's/ //g'`
|
---|
160 |
|
---|
161 | if ($lvmver = 0) {
|
---|
162 | # Still not found
|
---|
163 | if (-x $lvmcmd) {
|
---|
164 | open(LVM,"$lvmcmd version |") || mr_exit(-1,"Unable to execute $lvmcmd");
|
---|
165 | while (<LVM>) {
|
---|
166 | if (/LVM version/) {
|
---|
167 | $lvmver = $_;
|
---|
168 | $lvmver =~ s/LVM version ([0-9])\..*/$1/;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | close(LVM);
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | if ($lvmver = 0) {
|
---|
176 | # Still not found
|
---|
177 | mr_exit(-1,"Unable to determine LVM version.\nPlease report to the dev team with the result of the commands\n$lvmds and $lvmvmd version");
|
---|
178 | } elsif ($lvmver = 1) {
|
---|
179 | $lvmcmd = "";
|
---|
180 | }
|
---|
181 | pb_log(0,"Found LVM version $lvmver");
|
---|
182 |
|
---|
183 | # For the moment on stdout
|
---|
184 | OUTPUT = \*STDOUT;
|
---|
185 |
|
---|
186 | # Generate the startup scrit on the fly needed to restore LVM conf
|
---|
187 | print OUTPUT "# Desactivate Volume Groups\n";
|
---|
188 | print OUTPUT "$lvmcmd vgchange -an\n";
|
---|
189 | print OUTPUT "\n";
|
---|
190 |
|
---|
191 | print OUTPUT "# Creating Physical Volumes\n";
|
---|
192 | # Analyze the existing physical volumes
|
---|
193 | open(LVM,"$lvmcmd pvdisplay -c |") || mr_exit(-1,"Unable to execute $lvmcmd pvdisplay -c");
|
---|
194 | while (<LVM>) {
|
---|
195 | my ($pvname,$vgname,$pvsize,$ipvn,$pvstat,$pvna,$lvnum,$pesize,$petot,$pefree,$pelloc) = split(/:/);
|
---|
196 | print OUTPUT "$lvmcmd pvcreate -ff -y -s $pesize $pvname\n";
|
---|
197 | }
|
---|
198 | close(LVM);
|
---|
199 | print OUTPUT "\n";
|
---|
200 | print OUTPUT "# Scanning again Volume Groups\n";
|
---|
201 | print OUTPUT "$lvmcmd vgscan\n";
|
---|
202 | print OUTPUT "\n";
|
---|
203 |
|
---|
204 | # Analyze the existing volume groups
|
---|
205 | print OUTPUT "# Creating Volume Groups and Activating them\n";
|
---|
206 | open(LVM,"$lvmcmd vgdisplay -c |") || mr_exit(-1,"Unable to execute $lvmcmd vgdisplay -c");
|
---|
207 | while (<LVM>) {
|
---|
208 | my ($vgname,$vgaccess,$vgstat,$vgnum,$lvmaxnum,$lvnum,$ocalvinvg,$lvmaxsize,$pvmaxnum,$cnumpv,$anumpv,$vgsize,$pesize,$penum,$pealloc,$pefree,$uuid) = split(/:/);
|
---|
209 | if ($lvmver < 2) {
|
---|
210 | print OUTPUT "# Removing device first as LVM v1 doesn't do it\n";
|
---|
211 | print OUTPUT "rm -Rf /dev/$vg\n";
|
---|
212 | }
|
---|
213 | $lvmaxnum = 255 if ($lvmaxnum > 256);
|
---|
214 | $pvmaxnum = 255 if ($pvmaxnum > 256);
|
---|
215 | print OUTPUT "# Create Volume Group $vgname\n";
|
---|
216 | # Pb sur pesize unite ?
|
---|
217 | print OUTPUT "$lvmcmd vgcreate $vgname -p $pvmaxnum -s $pesize -l $lvmaxnum $lvmaxnum\n";
|
---|
218 | }
|
---|
219 | close(LVM);
|
---|
220 | print OUTPUT "\n";
|
---|
221 | print OUTPUT "# Activate Volume Group $vg\n";
|
---|
222 | print OUTPUT "$lvmcmd vgchange -ay\n";
|
---|
223 | print OUTPUT "\n";
|
---|
224 |
|
---|
225 | print OUTPUT "# Creating Logical Volumes\n";
|
---|
226 | open(LVM,"$lvmcmd lvdisplay -c |") || mr_exit(-1,"Unable to execute $lvmcmd lvdisplay -c");
|
---|
227 | while (<LVM>) {
|
---|
228 | my ($lvname,$vgname,$lvaccess,$lvstat,$lvnum,$oclv,$lvsize,$leinlv,$lealloc,$allocpol,$reaadhead,$major,$minor) = split(/:/);
|
---|
229 | print OUTPUT "# Create Logical Volume $lvname\n";
|
---|
230 | print OUTPUT "$lvmcmd lvcreate -n $lvname -L $lvsize -r $redahed $vgname\n";
|
---|
231 | [ "$stripes" ] && output="$output -i $stripes"
|
---|
232 | [ "$stripesize" ] && output="$output -I $stripesize"
|
---|
233 | }
|
---|
234 | close(LVM);
|
---|
235 | print OUTPUT "\n";
|
---|
236 | print OUTPUT "# Scanning again Volume Groups\n";
|
---|
237 | print OUTPUT "$lvmcmd vgscan\n";
|
---|
238 | print OUTPUT "\n";
|
---|
239 |
|
---|
240 | WriteShutdownScript
|
---|
241 | mr_exit(0,"End of analyze-lvm");
|
---|