List all hard drives on system (with capacities)... How?
Ronald F. Guilmette
rfg at tristatelogic.com
Fri Dec 7 00:23:56 UTC 2012
In message <50C1313C.4000201 at gmx.com>,
Nikos Vassiliadis <nvass at gmx.com> wrote:
>I think fdisk should need a valid partition table, or not?
Apparently, yes.
>diskinfo works nice with all disk-like devices be it a physical disk,
>a slice, a partition, a swap-backed device etc. Its output is easily
>parsable using a single line per device and if you use -v you will get
>the same info in human-readable form.
Indeed.
I just cooked up the attached trivial Perl script. It works nicely and
does exactly what I wanted.
(Please excuse my verbose Perl. That's just the way I code. I prefer
clarity over brevity.)
cut here
============================================================================
#!/usr/local/bin/perl -w
use strict;
sub
calculate_dsize
{
my ($dname) = @_;
my $count;
my $suffix;
my $bytes = `diskinfo $dname | awk '{print \$3}'`;
chop $bytes;
if ($bytes >= (1024 * 1024 * 1024 * 1024)) {
$count = $bytes / (1024 * 1024 * 1024 * 1024);
$suffix = "TiB";
} elsif ($bytes >= (1024 * 1024 * 1024)) {
$count = $bytes / (1024 * 1024 * 1024);
$suffix = "GiB";
} elsif ($bytes >= (1024 * 1024)) {
$count = $bytes / (1024 * 1024);
$suffix = "MiB";
} else {
die "$dname is too small!\n";
}
$count = sprintf ('%.1f', $count);
return "$count $suffix";
}
foreach my $line (`atacontrol list`) {
if ($line =~ m/: (ad[0-9]+) (.*)/) {
my $dsize = calculate_dsize ($1);
print "$1 $2 ($dsize)\n";
}
}
foreach my $line (`camcontrol devlist`) {
if ($line =~ m/^(.*>).*\((da[0-9]+)/) {
my $dsize = calculate_dsize ($2);
print "$2 $1 ($dsize)\n";
}
}
More information about the freebsd-questions
mailing list