I'm writing an application where I need to find disk stats in /proc/diskstats, but the only information I have to go off of is the filesystem label.
I know I can use the blkid library to convert a label to a partition (/dev/X/YZ where Z is the partition), but the data in /proc/diskstats is only indexed by the root block device (X/Y in my example). There is no proper way to just strip off the "Z" part because it's different for every driver (sda(1), c0d0(p1), etc). I was thinking I could use /proc/partitions and look for the name of the device with a minor number of "0", but it won't work if I have multiple disks connected to the same disk controller (they'll all have the same major number).
So... is there any good way to do this?
Thanks!
Are you aware of sysfs?
To the best of my knowledge, the standard way to perform resource discovery beginning with the 2.6 kernel series is through the sysfs file system. Begin with
/sys/class/block/
and
/sys/block/
The directory entries in /sys/class/block matches the list of block devices in both /proc/partitions and in /proc/diskstats. The directory entries corresponding with partitioned devices go further in containing directory entries for their partitions.
The directory entries in /sys/block matches only the devices (not including the partitions).
In this way, it should be possible, with minimal coding, to establish a consistent and reliable partition/device tree reflecting the current state of your system's block devices.
Oh wow, I can't believe I
Oh wow, I can't believe I didn't think of that. I believe that solves my problem. Thank you!
Ok... after some digging
Ok... after some digging around, it looks like sysfs is a little broken too. /sys/class/block would be exactly what I need, but it doesn't always exist. It might come from CONFIG_SYSFS_DEPRECATED, but I don't want to rely on information that is supposed to be deprecated. I am directed by the following document to use HAL, so I'll see what I can come up with in there.
http://lwn.net/Articles/237664/
Spam blocker... Arrgh...
I wrote a really nice comment about abstraction minimizing coupling between a management application and the system, which was black-holed by the spam-blocker. A few days' delay allowed me to create an official registration which will presumably defeat the blockage...
Thanks for noticing the Greg K-H note, I will use its contents to guide further development. I have not found a better source than sysfs for block device and partition management, so I've resolved to establish an abstraction layer in my various drive identification applications such that it can be replaced or updated conveniently.
I have an application that needs to locate a USB-connected SD/MMC card reader with minimum user input, which is at least tangentially related to your question. If/when I discover something predictable, I'll describe it here. For my application, I'll need to correlate /proc/scsi/scsi with the SCSI bus info in sysfs, which leads to a device name.