I need to get specifications of hard disk on both Win and *nix machines. I used <hdreg.h>
on Linux like this:
static struct hd_driveid hd;
int device;
if ((device = open("/dev/sda", O_RDONLY | O_NONBLOCK)) < 0)
{
cerr << "ERROR: Cannot open device /dev/sda \n";
exit(1);
}
if (!ioctl(device, HDIO_GET_IDENTITY, &hd))
{
cout << hd.model << endl;
cout << hd.serial_no << endl;
cout << hd.heads << endl;
}
I need hd_driveid
to tell me some more information about disk. I want to know:
- Number of partitions
- Specifications of each partition (format, label, flags, size, start point, number of tracks etc.)
- Number of tracks per cylinder
- Number of total tracks
- Maximum block size
- Minimum Block size
- Default block size
- Total size of device
My questions are:
- Is there a common (platform-independent) way to connect hardware? I would like use same code for win and *nix. (even if there was no way other than embedding assembly code into cpp)
- If there isn't, how do I get above information in *nix?
No, there is no platform-independent way. There is even no *nix way. There is just Linux way.
In Linux, all relevant information is available in various files in the
/proc
filesystem. The/proc/devices
will tell you what devices there are (the files in/dev/
may exist even when the devices are not available, though opening them will fail in that case),/proc/partitions
will tell you what partitions are available on each disk and than you'll have to look in the various subdirectories for the information. Just look around on some linux system where is what you need.For GNU/Linux have a look at this: obtaining hard disk metadata
Nearly everything in your list has nothing to do with "specifications of hard disk":
hd_driveid.sector_bytes
(usually 512, but some modern drives use 4096). I'm not aware of a maximum "block size", which is a property of the filesystem. I'm also not sure why this is useful.The total size in sectors is in
hd_driveid.lba_capacity_2
. Additionally, the size in bytes can probably be obtained with something likeNote that in both cases, it'll probably be a few megabytes bigger than sizes calculated by C×H×S.
It might help if you told us why you wanted this information...