source: MondoRescue/devel/mindi/bin/analyze-lvm@ 2077

Last change on this file since 2077 was 2077, checked in by Bruno Cornec, 15 years ago

much improved version of analyze-lvm based on man page and -c option usage for LVM commands - still not working - wip

  • Property svn:executable set to *
File size: 6.1 KB
Line 
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
13use strict 'vars';
14use Getopt::Long qw(:config auto_abbrev no_ignore_case);
15use Data::Dumper;
16use English;
17use File::Basename;
18use File::Copy;
19use File::stat;
20use File::Temp qw(tempdir);
21use POSIX qw(strftime);
22use lib qw (lib);
23use ProjectBuilder::Base;
24use ProjectBuilder::Distribution;
25
26=pod
27
28=head1 NAME
29
30Analyze-lvm - A Mindi Tool to analyze the LVM configuration and store it
31
32=head1 DESCRIPTION
33
34B<anlyze-lvm> prints all the information related to the LVM configuration that may be used by a restoration process
35
36=head1 SYNOPSIS
37
38analyze-lvm [-v]|[-q]|[-h]|[--man]
39
40=head1 OPTIONS
41
42=over 4
43
44=item B<-v|--verbose>
45
46Print a brief help message and exits.
47
48
49=item B<-q|--quiet>
50
51Do not print any output.
52
53=item B<-h|--help>
54
55Print a brief help message and exits.
56
57=item B<--man>
58
59Prints the manual page and exits.
60
61=item B<-i|--iso iso_image>
62
63Name of the ISO image you want to created.
64
65=back
66
67=head1 WEB SITES
68
69The 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
73The miling list of the project is available at L<mailto:mondo@lists.sf.net>
74
75=head1 AUTHORS
76
77The Mondorescue.org team L<http://www.mondorescue.org/> lead by Bruno Cornec L<mailto:bruno@mondorescue.org>.
78
79=head1 COPYRIGHT
80
81Analyze-LVM is distributed under the GPL v2.0 license
82described in the file C<COPYING> included with the distribution.
83
84=cut
85
86
87# ---------------------------------------------------------------------------
88
89# Initialize the syntax string
90
91pb_syntax_init("analyze-lvm Version PBVER-rPBREV\n");
92
93# Handle options
94#
95GetOptions("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
104if (defined $opts{'h'}) {
105 pb_syntax(0,1);
106}
107if (defined $opts{'man'}) {
108 pb_syntax(0,2);
109}
110if (defined $opts{'v'}) {
111 $pbdebug = $opts{'v'};
112}
113if (defined $opts{'q'}) {
114 $pbdebug=-1;
115}
116
117#
118# Global variables
119#
120my $MINDI_VERSION = "PBVER-rPBREV";
121my $MINDI_PREFIX = "PBPREFIX";
122my $MINDI_CONF = "PBCONF";
123my $MINDI_LIB = "PBLIB";
124my $MINDI_SBIN = "$MINDI_PREFIX/sbin";
125#
126# Temp dir
127#
128pb_temp_init();
129
130my $lvmds = "/usr/sbin/lvmdiskscan";
131my $lvmproc = "/proc/lvm/global";
132my $lvmcmd = "/usr/sbi/lvm";
133
134# -------------------------------- main -----------------------------------
135mr_exit(-1,"$lvmds doesn't exist. No LVM handling.") if ((! -x $lvmds) ;
136mr_exit(-1,"$lvmproc doesn't exist. No LVM handling.") if ((! -x $lvmproc) ;
137
138# Check LVM volumes presence
139open(LVM,$lvmproc) || mr_exit(-1,"Unable to open $lvmproc");
140while (<LVM>) {
141 mr_exit(1,"No LVM volumes found in $lvmproc") if (/0 VGs 0 PVs 0 LVs/);
142}
143close(LVM);
144
145# Check LVM version
146my $lvmver=0;
147open(LVM,"$lvmds --help 2>&1 |") || mr_exit(-1,"Unable to execute $lvmds");
148while (<LVM>) {
149 if (/Logical Volume Manager/ || /LVM version:/) {
150 $lvmver = $_;
151 $lvmver =~ s/:([0-9])\..*/$1/;
152 }
153}
154close(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
161if ($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
175if ($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}
181pb_log(0,"Found LVM version $lvmver");
182
183# For the moment on stdout
184OUTPUT = \*STDOUT;
185
186# Generate the startup scrit on the fly needed to restore LVM conf
187print OUTPUT "# Desactivate Volume Groups\n";
188print OUTPUT "$lvmcmd vgchange -an\n";
189print OUTPUT "\n";
190
191print OUTPUT "# Creating Physical Volumes\n";
192# Analyze the existing physical volumes
193open(LVM,"$lvmcmd pvdisplay -c |") || mr_exit(-1,"Unable to execute $lvmcmd pvdisplay -c");
194while (<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}
198close(LVM);
199print OUTPUT "\n";
200print OUTPUT "# Scanning again Volume Groups\n";
201print OUTPUT "$lvmcmd vgscan\n";
202print OUTPUT "\n";
203
204# Analyze the existing volume groups
205print OUTPUT "# Creating Volume Groups and Activating them\n";
206open(LVM,"$lvmcmd vgdisplay -c |") || mr_exit(-1,"Unable to execute $lvmcmd vgdisplay -c");
207while (<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}
219close(LVM);
220print OUTPUT "\n";
221print OUTPUT "# Activate Volume Group $vg\n";
222print OUTPUT "$lvmcmd vgchange -ay\n";
223print OUTPUT "\n";
224
225print OUTPUT "# Creating Logical Volumes\n";
226open(LVM,"$lvmcmd lvdisplay -c |") || mr_exit(-1,"Unable to execute $lvmcmd lvdisplay -c");
227while (<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}
234close(LVM);
235print OUTPUT "\n";
236print OUTPUT "# Scanning again Volume Groups\n";
237print OUTPUT "$lvmcmd vgscan\n";
238print OUTPUT "\n";
239
240WriteShutdownScript
241mr_exit(0,"End of analyze-lvm");
Note: See TracBrowser for help on using the repository browser.