summaryrefslogtreecommitdiff
path: root/pvinfo
blob: b2380ac3ee8e39b48a3a55366675197ec8242153 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/perl

# show the distribution of LVs/LEs in the systems PVs
#
# Copyright 2005, 2008, 2009 Uli Martens <uli@youam.net>
#           2009 Peter Palfrader <peter@palfrader.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

# ChangeLog:
# 2009-05-30 Peter Palfrader <peter@palfrader.org>
#   * teach it about striped logical volumes
# 2009-01-05 Uli Martens <uli@youam.net>
#   * right-justify #PE, fixes ragged output for large slices
# 2008-01-20 Uli Martens <uli@youam.net>
#   * fix off-by-two error for lv slice sizes
# 2005-04-11 Uli Martens <uli@youam.net>
#   * written

# Example Output:
# $ ~/bin/pvinfo
#         first/last PE    #PE       device                    first/last LE
# vg: vg
#  pv: /dev/md5
#    (      0 ..    295 (  296 )) -- /dev/vg/backup       (      0 ..    295 )
#    (    296 ..    807 (  512 )) -- /dev/vg/kyra         (      0 ..    511 )
# [...]


use strict;
use warnings;

# ---- vv config ----

my $pvdisplay_file = "pvdisplay |";
my $lvdisplay_file = "lvdisplay -m |";

my $lvname_width = 25;
# -------------------

my $all;

my $pv;
my $lv;
my $vg;

open PVD, $pvdisplay_file
	or die "can't get pvdisplay info: $!";

while (<PVD>) {
	if ( m/^  PV Name\s+(.*)$/ ) {
		$pv = $1;
		next;
	}
	if ( m/^  VG Name\s+(.*)$/ ) {
		$vg = $1;
		next;
	}
	if ( m/^  Total PE\s+(.*)$/ ) {
		$all->{$vg}->{$pv}->{size} = $1 -1;
		undef $vg;
		undef $pv;
		next;
	}
#	print;
}
close PVD;

open LVD, $lvdisplay_file
	or die "can't get lvdisplay info: $!";

VG: while (<LVD>) {
	if ( m/^  LV Name\s+(.*)$/ ) {
		$lv = $1;
		next;
	}
	if ( m/^  VG Name\s+(.*)$/ ) {
		$vg = $1;
		next;
	}
	if ( m/^  --- Segments ---$/ ) {
		my $from;
		my $to;
		my $pfrom;
		my $pto;
		my $pv;
		LE: while (<LVD>) {
			if ( m/^  --- Logical volume ---/ ) {
				next VG;
			}
			if ( m/^  Logical extent (\d+) to (\d+):$/ ) {
				$from = $1;
				$to = $2;
				next;
			}
			if ( m/^    Type\s+linear$/ ) {
				while (<LVD>) {
					if ( m/^    Physical volume\s+(.*)$/ ) {
						$pv = $1;
						next;
					}
					if ( m/^    Physical extents\s+(\d+) to (\d+)$/ ) {
						$pfrom = $1;
						$pto = $2;
						next;
					}
					
					if ( m/^\s*$/ and defined $pv ) {
						#print "GOT: $pv / $from / $to\n";
						$all->{$vg}->{$pv}->{lvs}->{$pfrom} = {
							from => $pfrom,
							to => $pto,
							lv => $lv,
							pv => $pv,
							pfrom => $from,
							pto => $to,
						};
						undef $pv;
						undef $from;
						undef $to;
						next LE;
					}
					#print ">> $_";
				}
			} elsif ( m/^    Type\s+striped$/ ) {
				my $numstripes;
				my $curstripe;
				while (<LVD>) {
					if ( m/^    Stripes\s+(.*)$/ ) {
						$numstripes = $1;
						next;
					}
					while (<LVD>) {
						if ( m/^      Physical volume\s+(.*)$/ ) {
							$pv = $1;
							next;
						}
						if ( m/^      Physical extents\s+(\d+) to (\d+)$/ ) {
							$pfrom = $1;
							$pto = $2;
							next;
						}
						if ((m/^\s*$/ or m/    Stripe\s+(\d+):/) and defined $pv ) {
							#print "GOT: $pv / $from / $to\n";
							$all->{$vg}->{$pv}->{lvs}->{$pfrom} = {
								from => $pfrom,
								to => $pto,
								lv => $lv." ($curstripe/$numstripes)",
								pv => $pv,
								pfrom => $from,
								pto => $to,
							};
							undef $pv;
							undef $pfrom;
							undef $pto;
						}
						if (m/    Stripe\s+(\d+):/) {
							$curstripe = $1;
						}
						if ( m/^\s*$/) {
							undef $pv;
							undef $from;
							undef $to;
							next LE;
						}
					}
				}
			} elsif ( m/^    Type\s+(.*)$/ ) {
				warn "Do not understand type $1 for $lv"."'s extend $from - $to.\n";
				next LE;
			}
		}
	}
	#print "XX $_";
}

close LVD;

printf "     %17s  %5s       %*s  %17s\n",
	"first/last PE", "#PE", -$lvname_width, "device", "first/last LE";
for my $vg ( sort keys %{$all} ) {
	print "vg: $vg\n";
	for my $pv ( sort keys %{$all->{$vg}} ) {
		print "  pv: $pv\n";
		my $pelast = -1;
		for my $x ( sort { $a <=> $b } keys %{$all->{$vg}->{$pv}->{lvs}} ) {
			if ( $pelast + 1 != $all->{$vg}->{$pv}->{lvs}->{$x}->{from} ) {
				printf "    (%7i ..%7i (%6i))\n",
					$pelast+1,
					$all->{$vg}->{$pv}->{lvs}->{$x}->{from} -1,
					$all->{$vg}->{$pv}->{lvs}->{$x}->{from} - $pelast -1;
			}
			printf "    (%7i ..%7i (%6i)) -- %*s (%7i ..%7i )\n",
				$all->{$vg}->{$pv}->{lvs}->{$x}->{from},
				$all->{$vg}->{$pv}->{lvs}->{$x}->{to},
				$all->{$vg}->{$pv}->{lvs}->{$x}->{to} - $all->{$vg}->{$pv}->{lvs}->{$x}->{from} +1,
				-$lvname_width,
				$all->{$vg}->{$pv}->{lvs}->{$x}->{lv},
				$all->{$vg}->{$pv}->{lvs}->{$x}->{pfrom},
				$all->{$vg}->{$pv}->{lvs}->{$x}->{pto};
			$pelast = $all->{$vg}->{$pv}->{lvs}->{$x}->{to};
		}
		if ( $pelast != $all->{$vg}->{$pv}->{size} ) {
			printf "    (%7i ..%7i (%6i))\n",
				$pelast +1,
				$all->{$vg}->{$pv}->{size},
				$all->{$vg}->{$pv}->{size} - $pelast;
		}
	}
}