[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: lokking for portable way to determine the size of harddisks



On Wed, Mar 07, 2001 at 08:38:54PM +0100, Thomas Lange wrote:
> I want to determine the size of the local harddisks in a shell. It
> must be portable so it runs on all hardware. First is used sfdisk,
> but it's only available on i386. Now I'm using /proc/partitions on a
> 2.2 kernel:

Using the proc filesystem only works on Linux[1]. The only really portable
approach is to use statfs(), and that only works for a mounted filesystems,
which should be sufficient if you are not doing really system specific stuff.

Of course, in shell, statfs is not directly available. You can use the df
utility and parse its output. The -P option makes sure its output conforms
to POSIX and can be parsed. For maximum compatibility, you should list a
file on the filesystem you want to query:

$ df -k -P /tmp
Filesystem         1024-blocks      Used Available Capacity Mounted on
/dev/hda2              4134932   3474616    450268      89% /

You can use awk to select the first or second column in the second line:

ulysses:/tmp# df /tmp | awk 'NR == 2 { print $2 }'
4134932

or if you want all mounted filesystems (but see below):

ulysses:/tmp# df | awk ' NR != 1 { print $1 " " $2 }'
/dev/hda2 4134932
/dev/hdc2 4053203
/dev/hdc5 806560

The -k guarantees that the block size is 1024. Without the -k it should
print out 512 byte blocks, but the current df we have is incompliant wrt
this (note to self: file a bug report).

The Hurd can not list all filesystems with a simple "df", so it would be
nice from you to avoid that. (Think in terms of statfs).
 
If you really need the size of filesystems where you don't know a file from
(or which is not mounted), then you are out of luck anyway and there is no
portable code. If it is a tool useful on the Hurd, I can tell you a way how
to determine the filesize from device files (basically, an open and a stat
will work) on the Hurd.

Thanks,
Marcus

[1] and bad enough there it seems, as you describe it (and I had my own
    problems with it, too)

-- 
`Rhubarb is no Egyptian god.' Debian http://www.debian.org brinkmd@debian.org
Marcus Brinkmann              GNU    http://www.gnu.org    marcus@gnu.org
Marcus.Brinkmann@ruhr-uni-bochum.de
http://www.marcus-brinkmann.de



Reply to: