Display all block devices on system
[sysadmin-cookbook] / bin / block-devices
1 #!/usr/bin/perl
2
3 =head1 block-devices
4
5 Display all block devices on system
6
7   hdparm -t `block-devices`
8
9 =cut
10
11 use warnings;
12 use strict;
13 #use autodie;
14
15 open(my $proc, '<', '/proc/partitions');
16
17 my @header;
18 my $last_device;
19
20 my $name2col;
21 sub col {
22         my ( $name, $line ) = @_;
23         if ( ! $name2col ) {
24                 $name2col->{ $header[$_] } = $_ foreach 0 .. $#header;
25         }
26         $line =~ s/^\s+// || die "expected line to start with space: $_\n";
27         my @c = split(/\s+/, $line, $#header + 1 );
28         return $c[ $name2col->{ $name } ];
29 }
30
31 while(<$proc>) {
32         chomp;
33         if ( ! @header ) {
34                 @header = split(/\s+/, $_);
35                 next;
36         }
37         next unless $_;
38
39         my $device = col 'name', $_;
40         if ( $last_device && substr( $device, 0, length($last_device) ) eq $last_device ) {
41                 warn "# partition $device\n";
42         } else {
43                 print '/dev/', $last_device = $device, $/;
44         }
45 }
46