SI/ptable.pm
changeset 3 1b975c58c5bf
parent 2 1f1d347eff00
child 6 df6ca24871d4
equal deleted inserted replaced
2:1f1d347eff00 3:1b975c58c5bf
     8 $ENV{LC_ALL} = "C";
     8 $ENV{LC_ALL} = "C";
     9 
     9 
    10 use SI::tools;
    10 use SI::tools;
    11 
    11 
    12 sub info($\%) {
    12 sub info($\%) {
    13 	my ($file, $part) = @_;
    13     my ($file, $part) = @_;
    14 
    14 
    15 	# find major of devmapper
    15     # find major of devmapper
    16 	my $devmapper = 0;
    16     my $devmapper = 0;
    17 	{
    17     {
    18 	local $/ = undef;
    18         local $/ = undef;
    19 	open(my($i), "/proc/devices") or die "ERR: Can't open /proc/devices: $!\n";
    19         open(my ($i), "/proc/devices")
    20 	($devmapper) =  <$i> =~ /^\s*(\d+)\s+device.mapper\b/m;
    20           or die "ERR: Can't open /proc/devices: $!\n";
    21 	}
    21         ($devmapper) = <$i> =~ /^\s*(\d+)\s+device.mapper\b/m;
       
    22     }
    22 
    23 
    23 	my ($current, %dev);
    24     my ($current, %dev);
    24 	foreach (`sfdisk -d 2>/dev/null`) {
    25     foreach (`sfdisk -d 2>/dev/null`) {
    25 		chomp;
    26         chomp;
    26 		### $_
    27         ### $_
    27 		if (/^# partition table .*?(\/\S+)/) {
    28         if (/^# partition table .*?(\/\S+)/) {
    28 			$current = (stat $1)[6] >> 8 == $devmapper ? undef : $1
    29             $current = (stat $1)[6] >> 8 == $devmapper ? undef : $1
    29 				and push(@{$dev{$current}}, $_);
    30               and push(@{ $dev{$current} }, $_);
    30 			next;
    31             next;
    31 		}
    32         }
    32 		next if not $current;
    33         next if not $current;
    33 		push @{$dev{$current}}, $_;
    34         push @{ $dev{$current} }, $_;
    34 	}
    35     }
    35 	### partiton tables of useful blockdevices:
    36     ### partiton tables of useful blockdevices:
    36 	### %dev
    37     ### %dev
    37 
    38 
    38 	# process the partition tables
    39     # process the partition tables
    39 	# and output the tables for relevant devices
    40     # and output the tables for relevant devices
    40 	foreach my $dev (keys %dev) {
    41     foreach my $dev (keys %dev) {
    41 		($_ = $dev) =~ s/^.*\///;
    42         ($_ = $dev) =~ s/^.*\///;
    42 		my $file = sprintf $file, $_;
    43         my $file = sprintf $file, $_;
    43 		die "ERR: $file exists already\n" if -f $file;
    44         die "ERR: $file exists already\n" if -f $file;
    44 		open(my $o, ">$file") or die "ERR: Can't open $file: $!\n";
    45         open(my $o, ">$file") or die "ERR: Can't open $file: $!\n";
    45 		print $o join "\n", @{$dev{$dev}}, "";
    46         print $o join "\n", @{ $dev{$dev} }, "";
    46 	}
    47     }
    47 
    48 
    48 	# reserve entries in %part, one for each partition
    49     # reserve entries in %part, one for each partition
    49 	@{$part->{physical}}{map { (split)[0] } grep { /^\// } map { @$_ } values %dev} = ();
    50     @{ $part->{physical} }{ map { (split)[0] }
       
    51           grep { /^\// } map { @$_ } values %dev } = ();
    50 }
    52 }
    51 1;
    53 1;
       
    54 # vim:sts=4 sw=4 aw ai si: