1 package SI::ptable; |
1 package SI::ptable; |
2 |
2 |
3 use strict; |
3 use strict; |
4 use warnings; |
4 use warnings; |
5 use if $ENV{DEBUG} ~~ "ptable" => "Smart::Comments"; |
|
6 use File::Find; |
5 use File::Find; |
|
6 use IO::File; |
7 |
7 |
|
8 use if $ENV{DEBUG} ~~ /ptable|all/ => qw(Smart::Comments); |
|
9 |
|
10 use SI::tools; |
8 $ENV{LC_ALL} = "C"; |
11 $ENV{LC_ALL} = "C"; |
9 |
12 |
10 use SI::tools; |
13 sub volumes($\%) { |
|
14 my ($file, $devs) = @_; |
11 |
15 |
12 sub save($\%) { |
16 # find the non-removable devices |
13 my ($file, $part) = @_; |
17 my ($current, $of); |
14 |
|
15 # find the major number of the devmapper |
|
16 my $devmapper = 0; |
|
17 { |
|
18 local $/ = undef; |
|
19 open(my ($i), "/proc/devices") |
|
20 or die "ERR: Can't open /proc/devices: $!\n"; |
|
21 ($devmapper) = <$i> =~ /^\s*(\d+)\s+device.mapper\b/m; |
|
22 } |
|
23 |
|
24 # find the non-removable devices, store their |
|
25 # names as $part->{disks}{DEVICE} |
|
26 # and save the current sfdisk dump of these devices |
|
27 # in @{$dev{DEVICE}} |
|
28 my ($current, %dev); |
|
29 foreach (`sfdisk -d 2>/dev/null`) { |
18 foreach (`sfdisk -d 2>/dev/null`) { |
30 chomp; |
19 chomp; |
31 if (/^# partition table .*?(\/\S+\/(\S+))/) { |
20 if (/^# partition table .*?(\/\S+\/(\S+))/) { |
32 # skip the removable devices |
21 # skip the removable devices |
33 my ($device, $name) = ($1, $2); |
22 my ($device, $name) = ($1, $2); |
34 $_ = (grep { /ATTR{removable}/ } `udevadm info --attribute-walk --name $name`)[0]; |
23 verbose("device $device"); |
35 $current = /==.0./ ? $device : undef; |
24 if ((grep { /ATTR{removable}/ } `udevadm info --attribute-walk --name $name`)[0] !~ /==.0./) { |
36 $part->{disks}{$current} = undef if $current; |
25 $current = undef; |
|
26 verbose("skipping (removable)\n"); |
|
27 next; |
|
28 } |
|
29 |
|
30 # save in our data structure |
|
31 verbose("scanning\n"); |
|
32 $current = $device; |
|
33 push @{$devs->{disk}{$current}{pt}}, $_; |
|
34 |
|
35 # and open the outfile |
|
36 my $f = sprintf ">$file", $name; |
|
37 $of = new IO::File $f or die "ERR: Can't open $f: $!\n"; |
|
38 print $of "$_\n"; |
|
39 |
37 next; |
40 next; |
38 } |
41 } |
39 next if not $current; |
42 next if not $current; |
40 push @{ $dev{$current} }, $_; |
43 push @{$devs->{disk}{$current}{pt}}, $_; |
|
44 print $of "$_\n"; |
|
45 |
|
46 if (/^(\/dev\/\S+)\s*:/) { |
|
47 $devs->{volume}{$1} = undef; |
|
48 } |
|
49 |
41 } |
50 } |
42 |
51 |
43 ### %dev |
52 ### $devs |
44 ### exit |
53 return; |
45 |
|
46 # process the partition tables |
|
47 # and output the tables for relevant devices |
|
48 foreach my $dev (keys %dev) { |
|
49 ($_ = $dev) =~ s/^.*\///; |
|
50 my $file = sprintf $file, $_; |
|
51 die "ERR: $file exists already\n" if -f $file; |
|
52 open(my $o, ">$file") or die "ERR: Can't open $file: $!\n"; |
|
53 print $o join "\n", @{ $dev{$dev} }, ""; |
|
54 } |
|
55 |
|
56 # reserve entries in %part, one for each partition |
|
57 @{ $part->{physical} }{ map { (split)[0] } |
|
58 grep { /^\// } map { @$_ } values %dev } = (); |
|
59 } |
54 } |
60 1; |
55 1; |
61 # vim:sts=4 sw=4 aw ai si: |
56 # vim:sts=4 sw=4 aw ai si: |