added -A for ACLs in SELinux
[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   sfdisk -l `block-devices with-partitions`
10
11 =cut
12
13 use warnings;
14 use strict;
15 #use autodie;
16
17 my $with_partitions = { true => 1 } if grep { m/with.?partitions/ } @ARGV;
18
19 open(my $proc, '<', '/proc/partitions');
20
21 my @header;
22 my $last_device = '';
23
24 my $name2col;
25 sub col {
26         my ( $name, $line ) = @_;
27         if ( ! $name2col ) {
28                 $name2col->{ $header[$_] } = $_ foreach 0 .. $#header;
29         }
30         $line =~ s/^\s+// || die "expected line to start with space: $_\n";
31         my @c = split(/\s+/, $line, $#header + 1 );
32         return $c[ $name2col->{ $name } ];
33 }
34
35 while(<$proc>) {
36         chomp;
37         if ( ! @header ) {
38                 @header = split(/\s+/, $_);
39                 next;
40         }
41         next unless $_;
42
43         my $device = col 'name', $_;
44         my $device_like_last = substr( $device, 0, length($last_device) );
45         if ( $last_device && $device_like_last eq $last_device ) {
46                 if ( $with_partitions ) {
47                         print "/dev/$device_like_last\n" if ! $with_partitions->{ $device_like_last }++;
48                 } else {
49                         warn "# partition $device\n";
50                 }
51         } else {
52                 print "/dev/$device\n" unless $with_partitions;
53                 $last_device = $device;
54         }
55 }
56