#!/usr/bin/perl =head1 block-devices Display all block devices on system hdparm -t `block-devices` sfdisk -l `block-devices with-partitions` =cut use warnings; use strict; #use autodie; my $with_partitions = { true => 1 } if grep { m/with.?partitions/ } @ARGV; 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', $_; my $device_like_last = substr( $device, 0, length($last_device) ); if ( $last_device && $device_like_last eq $last_device ) { if ( $with_partitions ) { print "/dev/$device_like_last\n" if ! $with_partitions->{ $device_like_last }++; } else { warn "# partition $device\n"; } } else { print "/dev/$device\n" unless $with_partitions; $last_device = $device; } }