Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 06 Dec 2012 16:23:54 -0800
From:      "Ronald F. Guilmette" <rfg@tristatelogic.com>
To:        freebsd-questions@freebsd.org
Subject:   Re: List all hard drives on system (with capacities)... How?
Message-ID:  <11844.1354839834@tristatelogic.com>
In-Reply-To: <50C1313C.4000201@gmx.com>

next in thread | previous in thread | raw e-mail | index | archive | help

In message <50C1313C.4000201@gmx.com>, 
Nikos Vassiliadis <nvass@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";
  }
}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?11844.1354839834>