SI/ptable.pm
changeset 7 03ca6d811a17
parent 6 df6ca24871d4
child 10 23dd30396607
--- a/SI/ptable.pm	Fri Jan 15 23:17:23 2010 +0100
+++ b/SI/ptable.pm	Sat Jan 16 23:53:50 2010 +0100
@@ -2,60 +2,55 @@
 
 use strict;
 use warnings;
-use if $ENV{DEBUG} ~~ "ptable" => "Smart::Comments";
 use File::Find;
+use IO::File;
 
-$ENV{LC_ALL} = "C";
+use if $ENV{DEBUG} ~~ /ptable|all/ => qw(Smart::Comments);
 
 use SI::tools;
-
-sub save($\%) {
-    my ($file, $part) = @_;
+$ENV{LC_ALL} = "C";
 
-    # find the major number of the 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;
-    }
+sub volumes($\%) {
+    my ($file, $devs) = @_;
 
-    # find the non-removable devices, store their
-    # names as $part->{disks}{DEVICE}
-    # and save the current sfdisk dump of these devices
-    # in @{$dev{DEVICE}}
-    my ($current, %dev);
+    # find the non-removable devices
+    my ($current, $of);
     foreach (`sfdisk -d 2>/dev/null`) {
         chomp;
         if (/^# partition table .*?(\/\S+\/(\S+))/) {
 	    # skip the removable devices
 	    my ($device, $name) = ($1, $2);
-	    $_ = (grep { /ATTR{removable}/ } `udevadm info --attribute-walk --name $name`)[0];
-	    $current = /==.0./ ? $device : undef;
-	    $part->{disks}{$current} = undef if $current;
+	    verbose("device $device");
+	    if ((grep { /ATTR{removable}/ } `udevadm info --attribute-walk --name $name`)[0] !~ /==.0./) {
+		    $current = undef;
+		    verbose("skipping (removable)\n");
+		    next;
+	    }
+
+	    # save in our data structure
+	    verbose("scanning\n");
+	    $current = $device;
+	    push @{$devs->{disk}{$current}{pt}}, $_;
+
+	    # 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";
+
 	    next;
         }
         next if not $current;
-        push @{ $dev{$current} }, $_;
+	push @{$devs->{disk}{$current}{pt}}, $_;
+	print $of "$_\n";
+
+	if (/^(\/dev\/\S+)\s*:/) {
+	    $devs->{volume}{$1} = undef;
+	}
+
     }
 
-    ### %dev
-### exit
-
-    # 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 } = ();
+### $devs
+    return;
 }
 1;
 # vim:sts=4 sw=4 aw ai si: