Easy way to detect hard drives and partitions in Linux

Mike Meyer mwm at mired.org
Sun Jun 5 01:06:19 EDT 2005


Jeff Epler <jepler at unpythonic.net> writes:

>> I need a way to detect hard drives and their partitions... labels would
>> be nice too... I did some googling but did not find anything all too
>> useful. This will strictly be on Linux / Unix so any help would be
>> greatly appreciated.
> You're not going to find a single portable "unix" way of doing this.
> The format of /etc/fstab and /etc/mtab are pretty portable, but they
> only list mountable/mounted partitions, not all partitions.

Worse yet, part of what he asks for - labels - may not exist. Either
that, or may be something different from what he thinks they are. Unix
predates MSDOS, and many Unix systems don't support the MSDOS
partition table. Some make use of them optional on disks.

/etc/fstab and /etc/mtab have worse problems than not listing all
partitions. They list mountable "things", not all of which are
partitions on hard drives. For example, my fstab has entries for a
memory card reader, a floppy drive, a cdr and a cdrw, as well as two
different implementations of procfs.

Checking for MSDOS style partitions will miss a lot of things. Unix
predates MSDOS, and many Unices don't use MSDOS style partitions, or
make their use optional.

It's possible you have hard drives hanging off the system that aren't
visible to the system *at all*. For instance, the device nodes may
never have been created. As others have indicated, some systems expose
information about what's on the various system busses via special file
systems. Those are system-dependent, and are the only way to find
drives that don't have device nodes.  Assuming the device nodes have
been created, they may not be used otherwise, so the only way to find
out about them is groveling through /dev (or /devices, depending on
the Unix in question). However, figuring out which are hard disks and
which are not will be system-dependent.

You could start on this by groveling over the output of mount, or
/etc/fstab (to bad getfsent isn't in the standard library). That
provides clues about what disk drive file names look like, which could
be usefull in looking through /dev. But even then, you're going to
pretty quickly run into the differences between systems.

I'd recommend deciding which Unix you want this to work for, and then
asking the question again for that system. For Linux, you may want to
specify a distribution. And then don't be surprised if the answer
chances with releases of the underlying system.

        <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list