SI/ptable.pm
changeset 13 2e3ad71484ea
parent 12 a1aee1136609
child 15 4e3753b998a9
--- a/SI/ptable.pm	Mon Jan 18 20:35:27 2010 +0100
+++ b/SI/ptable.pm	Sun Jan 17 13:54:10 2010 +0100
@@ -5,68 +5,58 @@
 use strict;
 use warnings;
 use File::Find;
+use File::Basename;
 use IO::File;
 
-
 use SI::tools;
 $ENV{LC_ALL} = "C";
 
 sub volumes($\%) {
     my ($file, $devs) = @_;
 
-    # find the non-removable devices
-
-    my ($current, $of);
-    foreach (`sfdisk -d 2>/dev/null`) {
-        chomp;
+    # find the partition tables of all
+    # non-removable block devices (this may include
+    # LVs (used as disk itself) too)
+    foreach (glob("/sys/block/*")) {
+	    my $name = basename($_);
+	    verbose("device $name");
 
-	# new entry, new disk
-        if (my ($device, $name) = /^# partition table .*?(\/\S+\/(\S+))/) {
-	    verbose("scanning device $device");
-
+	    if (!-e "$_/device") {
+		    verbose("skipping (non-dev)\n");
+		    next;
+	    }
 	    if ((grep { /ATTR{removable}/ } `udevadm info --attribute-walk --name $name`)[0] !~ /==.0./) {
-		    $current = undef;
 		    verbose("skipping (removable)\n");
 		    next;
 	    }
+	    verbose("\n");
 
-	    # if it looks like a device mapper, we'll have to remember
-	    # this
-	    if ($device =~ /\/dev\/dm-/) {
-		if (my $alias = find_by_devid("/dev/mapper", (stat $device)[6])) {
-		    verbose("(alias $device)");
-		    $devs->{disk}{$alias}{dm} = $device;
-		    $device = $alias;
-		}
-	    }
+	    die "ERR: /dev/$name does not exist. (should not happen)"
+		if !-e "/dev/$name";
+
+	    # now the physical disk -- let's ask for the partition table
 
-	    # save in our data structure
-	    verbose("\n");
-	    $current = $device;
-	    push @{$devs->{disk}{$current}{pt}}, $_;
+	    my @sfdisk = `sfdisk -d /dev/$name 2>/dev/null`;
+
+	    my $of = sprintf $file, $name;
+	    my $oh = new IO::File ">$of" or die "Can't open >$of: $!\n";
+	    print $oh @sfdisk;
 
-	    # and open the outfile
-	    my $f = sprintf ">$file", $name;
-	    $of = new IO::File $f or die "ERR: Can't open $f: $!\n";
-	    print $of "$_\n";
+	    $devs->{disk}{"/dev/$name"} = { 
+		pt => \@sfdisk
+	    };
 
-	    next;
-        }
-
-        next if not $current;
+	    push @{$devs->{known}}, "/dev/$name";
 
-	push @{$devs->{disk}{$current}{pt}}, $_;
-	print $of "$_\n";
-
-	# if it looks like a partition (volume), then
-	# save it
-	if (my ($dev, $id) = /^(\/dev\/\S+)\s*:.*\sId=\s*(\d+)/) {
-	    if (my $dm = $devs->{disk}{$current}{dm}) {
-		$dev =~ s/$dm/$current/;
+	    # and let's prepare the volume entries
+	    foreach (@sfdisk) {
+		/^(\S+)\s*:.*Id=\s*(\d+)/ or next;
+		$devs->{volume}{$1} = {
+			origin => "ptable",
+			ptable_type => $2,
+		    };
+		push @{$devs->{known}}, $1;
 	    }
-	    $devs->{volume}{$dev}{fstype} = $id;
-	}
-
     }
 
     return;