Display all block devices on system
[sysadmin-cookbook] / bin / block-devices
diff --git a/bin/block-devices b/bin/block-devices
new file mode 100755 (executable)
index 0000000..99cfdd5
--- /dev/null
@@ -0,0 +1,46 @@
+#!/usr/bin/perl
+
+=head1 block-devices
+
+Display all block devices on system
+
+  hdparm -t `block-devices`
+
+=cut
+
+use warnings;
+use strict;
+#use autodie;
+
+open(my $proc, '<', '/proc/partitions');
+
+my @header;
+my $last_device;
+
+my $name2col;
+sub col {
+       my ( $name, $line ) = @_;
+       if ( ! $name2col ) {
+               $name2col->{ $header[$_] } = $_ foreach 0 .. $#header;
+       }
+       $line =~ s/^\s+// || die "expected line to start with space: $_\n";
+       my @c = split(/\s+/, $line, $#header + 1 );
+       return $c[ $name2col->{ $name } ];
+}
+
+while(<$proc>) {
+       chomp;
+       if ( ! @header ) {
+               @header = split(/\s+/, $_);
+               next;
+       }
+       next unless $_;
+
+       my $device = col 'name', $_;
+       if ( $last_device && substr( $device, 0, length($last_device) ) eq $last_device ) {
+               warn "# partition $device\n";
+       } else {
+               print '/dev/', $last_device = $device, $/;
+       }
+}
+