From owner-freebsd-questions@FreeBSD.ORG Fri Dec 7 00:23:56 2012 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 7196ED74 for ; Fri, 7 Dec 2012 00:23:56 +0000 (UTC) (envelope-from rfg@tristatelogic.com) Received: from outgoing.tristatelogic.com (segfault.tristatelogic.com [69.62.255.118]) by mx1.freebsd.org (Postfix) with ESMTP id 488F88FC13 for ; Fri, 7 Dec 2012 00:23:55 +0000 (UTC) Received: from segfault-nmh-helo.tristatelogic.com (localhost [127.0.0.1]) by segfault.tristatelogic.com (Postfix) with ESMTP id E03AE5081A for ; Thu, 6 Dec 2012 16:23:54 -0800 (PST) To: freebsd-questions@freebsd.org Subject: Re: List all hard drives on system (with capacities)... How? In-Reply-To: <50C1313C.4000201@gmx.com> Date: Thu, 06 Dec 2012 16:23:54 -0800 Message-ID: <11844.1354839834@tristatelogic.com> From: "Ronald F. Guilmette" X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 07 Dec 2012 00:23:56 -0000 In message <50C1313C.4000201@gmx.com>, Nikos Vassiliadis 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"; } }