【问题标题】:Failed to ioctl on specific disks无法在特定磁盘上进行 ioctl
【发布时间】:2013-11-30 14:46:39
【问题描述】:

我使用 /proc/diskstats 来获取读取和写入的扇区数。 我想将此数字转换为字节,以便查找扇区大小。 我使用How to find floppy\ CD sector size in Linux? 获取磁盘 sda、sda1 和 sda2 的扇区大小,但失败并出现以下错误: 文件 /dev/sda 上的 ioctl 失败,错误参数无效 文件 /dev/sda1 上的 ioctl 失败,错误为设备的 ioctl 不合适 和 sda2 一样。 将感谢您的帮助。 谢谢

          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 question有关系吗?

标签: c++ linux ioctl sector


【解决方案1】:

这个ioctl 并不总是适用于某些类型的块设备,但更重要的是,这个值不是/proc/diskstats 中报告的实际扇区大小。

diskstats 代码返回读取的扇区数,其中扇区的大小根据BLKSSZGET 返回的值:

    int sector_size = 0;
    int err = ioctl(fd, BLKSSZGET, &sector_size);
    if (err > 0) {
        currBytesPerSector = sector_size;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 2022-01-20
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多