1
votes

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);
          }
1
I've edited the code in the question you linked to, it was invalid and passing a wild pointer around. - nos
I already did the change for me before and it still didn't work. - walla
Is it related to this question? - Petesh

1 Answers

1
votes

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, &sector_size);
    if (err > 0) {
        currBytesPerSector = sector_size;
    }