done part/blkid/lvm
authorHeiko Schlittermann <hs@schlittermann.de>
Fri, 15 Jan 2010 01:16:12 +0100
changeset 1 b9ddf49db5b8
parent 0 c0faaa882cf6
child 2 1f1d347eff00
done part/blkid/lvm
SI/blkid.pm
SI/lvm.pm
SI/ptable.pm
si
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SI/blkid.pm	Fri Jan 15 01:16:12 2010 +0100
@@ -0,0 +1,47 @@
+package SI::blkid;
+
+use strict;
+use warnings;
+use File::Find;
+
+sub ff($$) {
+	my ($dir, $id) = @_;
+	my $found;
+	find(sub { 
+		return if $found;
+		$found = (stat)[6] == $id ? $File::Find::name : undef;
+		return;
+		}, $dir);
+	return $found;
+}
+
+sub info($\%) {
+	my ($file, $part) = @_;
+
+	foreach (`blkid -c /dev/null`) {
+		my ($dev) = (split /:/)[0];
+
+		my ($uuid) = /\sUUID=.(.*?).\s/;
+		my ($type) = /\sTYPE=.(.*?).\s/;
+		my ($label) = /\sLABEL=.(.*?).\s/;
+
+		if ($dev ~~ $part->{physical}) {
+			$part->{physical}{$dev}{uuid} = $uuid;
+			$part->{physical}{$dev}{type} = $type;
+			$part->{physical}{$dev}{label} = $label;
+			next;
+		}
+	
+		# dev mapper names should be replace by nicer ones
+		if ($dev ~~ /^\/dev\/dm-/) {
+			$dev = ff("/dev/mapper", (stat $dev)[6])
+				or next;
+			$part->{logical}{$dev}{uuid} = $uuid;
+			$part->{logical}{$dev}{type} = $type;
+			$part->{logical}{$dev}{label} = $label;
+		}
+		
+	}
+}
+
+1;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SI/lvm.pm	Fri Jan 15 01:16:12 2010 +0100
@@ -0,0 +1,29 @@
+package SI::lvm;
+
+use strict;
+use warnings;
+use SI::tools;
+
+sub vg_info($\%) {
+	my ($file, $part) = @_;
+	foreach (map { (split /:/)[0] } map { /^\s*(.*)/; } `vgs --noheadings --separator :`) {
+		my $file = sprintf $file, $_;
+		run("vgcfgbackup -f $file >/dev/null");
+	}
+}
+
+sub lv_info($\%) {
+	my ($file, $part) = @_;
+
+	foreach my $p (keys %$part) {
+	`pvs -o +uuid --nameprefix --noheadings $p` =~ /\bLVM2_PV_UUID='(.*?)'/
+		or next;
+
+	$part->{$p}{type} = "pv";
+	$part->{$p}{uuid} = $1;
+
+	}
+
+}
+
+1;
--- a/SI/ptable.pm	Thu Jan 14 20:50:54 2010 +0100
+++ b/SI/ptable.pm	Fri Jan 15 01:16:12 2010 +0100
@@ -2,12 +2,48 @@
 
 use strict;
 use warnings;
+use if $ENV{DEBUG} ~~ "ptable" => "Smart::Comments";
+use File::Find;
+
+$ENV{LC_ALL} = "C";
 
 use SI::tools;
 
-sub save($) {
-	my $file = shift;
-	run "sfdisk -d >$file 2>/dev/null";
+sub info($\%) {
+	my ($file, $part) = @_;
+
+	# find major of devmapper
+	my $devmapper = 0;
+	{
+	local $/ = undef;
+	open(my($i), "/proc/devices") or die "ERR: Can't open /proc/devices: $!\n";
+	($devmapper) =  <$i> =~ /^\s*(\d+)\s+device.mapper\b/m;
+	}
+
+	my ($current, %dev);
+	foreach (`sfdisk -d 2>/dev/null`) {
+		chomp;
+		if (/^# partition table .*?(\/\S+)/) {
+			$current = (stat $1)[6] >> 8 == $devmapper ? undef : $1
+				and push(@{$dev{$current}}, $_);
+			next;
+		}
+		push @{$dev{$current}}, $_;
+	}
+	### partiton tables of useful blockdevices:
+	### %dev
+
+	# process the partition tables
+	# and output the tables for relevant devices
+	foreach my $dev (keys %dev) {
+		($_ = $dev) =~ s/^.*\///;
+		my $file = sprintf $file, $_;
+		die "ERR: $file exists already\n" if -f $file;
+		open(my $o, ">$file") or die "ERR: Can't open $file: $!\n";
+		print $o join "\n", @{$dev{$dev}}, "";
+	}
+
+	# reserve entries in %part, one for each partition
+	@{$part->{physical}}{map { (split)[0] } grep { /^\// } map { @$_ } values %dev} = ();
 }
-
 1;
--- a/si	Thu Jan 14 20:50:54 2010 +0100
+++ b/si	Fri Jan 15 01:16:12 2010 +0100
@@ -3,11 +3,21 @@
 
 use strict;
 use warnings;
+use if $ENV{DEBUG} => "Smart::Comments";
 
 use SI::ptable;
+use SI::blkid;
+use SI::lvm;
 
 my $OUT = "out";
 
 -d $OUT or mkdir($OUT, 0700) or die "Can't mkdir $OUT: $!\n";
 
-SI::ptable::save("$OUT/partitions");
+my %physical;
+
+SI::ptable::info("$OUT/partitions.%s", %physical);
+SI::blkid::info("$OUT/blkid", %physical);
+SI::lvm::vg_info("$OUT/vg.%s", %physical);
+
+### %physical
+