I used /proc/diskstats to get the number of sectors read and wrote. I want to convert this number to bytes so I look for the sector size. I used How to find floppy\ CD sector size in Linux? to get the sector size of disks sda, sda1 and sda2 but it fails with the following errors: failed ioctl on file /dev/sda with error Invalid argument failed ioctl on file /dev/sda1 with error Inappropriate ioctl for device and the same for sda2. Will appreciate your help. Thanks
struct hd_driveid id;
string fileName = "/dev/";
fileName += diskName;
int fd;
fd = open(fileName.c_str(), O_RDONLY|O_NONBLOCK);
if (fd < 0) {
LogError("Cannot open file " << fileName);
}
else
{
if (ioctl(fd, HDIO_GET_IDENTITY, &id) < 0) {
LogError("failed ioctl on with error " << strerror(errno));
} else {
currBytesPerSector = id.sector_bytes;
}
close(fd);
}
This ioctl
doesn't always work for certain types of block devices, but, more importantly, this value is not the actual sector size as reported in /proc/diskstats
.
the diskstats code return the number of sectors read, where sectors are sized from the value returned from BLKSSZGET
:
int sector_size = 0;
int err = ioctl(fd, BLKSSZGET, §or_size);
if (err > 0) {
currBytesPerSector = sector_size;
}