Easy way to detect hard drives and partitions in Linux

R

RunLevelZero

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.

TIA
 
P

Peter Hansen

RunLevelZero said:
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.

os.popen('/sbin/sfdisk -l /dev/hda') etc... ?

Or reading from /proc/ide and friends?
 
J

Jeff Epler

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.

In addition to the linux possibilities mentioned in another reply, there
is also /proc/partitions. Finally, if you only want to recognize
FDISK.EXE-type partitions (used by many types of Linux, though that
seems to be changing in Fedora Core 4), it shouldn't be hard to write a
Python program to read the partition table directly from the disk. The
details will be readily available online.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFCn0rcJd01MZaTXX0RAhVLAJ90DC3XyxTcgnweHqvHbqfQlBKZIgCfSpdl
O8X4WWSLBjcr6tJxIJUAiz4=
=evN/
-----END PGP SIGNATURE-----
 
R

RunLevelZero

Jeff said:
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.

In addition to the linux possibilities mentioned in another reply, there
is also /proc/partitions. Finally, if you only want to recognize
FDISK.EXE-type partitions (used by many types of Linux, though that
seems to be changing in Fedora Core 4), it shouldn't be hard to write a
Python program to read the partition table directly from the disk. The
details will be readily available online.

Jeff

Well thanks for the responses and so quickly. I'm very new to
programming and am not afraid to say it. I'll see what I can come up
with. at least this is a good start.

Thanks.
 
M

Mike Meyer

Jeff Epler said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top