【问题标题】:Possible sources for CHS disk geometryCHS 磁盘几何结构的可能来源
【发布时间】:2018-04-19 19:20:24
【问题描述】:

我正在尝试从块设备中确定“正确”的 CHS:

#include <sys/ioctl.h>
#include <linux/hdreg.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>

int main(int argv, char **argv) {
    int r;
    int f;
    struct hd_geometry g;

    f = open(*(argv+1), 0);
    if (f < 0) {
        printf("open fail: %d\n", errno);
        return 1;
    }
    r = ioctl(f, HDIO_GETGEO, (void*)&g);
    if (r == -1) {
        printf("ioctl fail: %d\n", errno);
        return 2;
    }
    close(f);
    printf("c: %d\nh: %d\ns: %d\nstart: %d\n", g.cylinders, g.heads, g.sectors, g.start);
    return 0;
}

在 USB 记忆棒上使用它:

c: 1020
h: 247
s: 62
start: 0

但是,fdisk --units=cylinders -l /dev/sdc 给出:

Geometry: 255 heads, 63 sectors/track, 1020 cylinders

它可能从其他地方获取信息。那么它可能使用哪些其他来源?

有没有办法确定“正确”的?据我了解,CHS在现代完全是由硬件制造商任意设置的。

环境:

  • linux 4.15.13
  • glibc 2.26-11
  • fdisk; util-linux 2.31.1

fdisk 库:

linux-vdso.so.1 (0x00007ffeb7be8000)
libfdisk.so.1 => /usr/lib/libfdisk.so.1 (0x00007fe4b040f000)
libsmartcols.so.1 => /usr/lib/libsmartcols.so.1 (0x00007fe4b01dd000)
libtinfo.so.6 => /usr/lib/libtinfo.so.6 (0x00007fe4aff70000)
libreadline.so.7 => /usr/lib/libreadline.so.7 (0x00007fe4afd22000)
libc.so.6 => /usr/lib/libc.so.6 (0x00007fe4af96b000)
libblkid.so.1 => /usr/lib/libblkid.so.1 (0x00007fe4af71d000)
libuuid.so.1 => /usr/lib/libuuid.so.1 (0x00007fe4af516000)
/lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00007fe4b0896000)

【问题讨论】:

  • 阅读fdisk 上的man 页面并搜索cylinder 会产生一些有趣的结果。显然,这有点未知,并且已被弃用,尽管我不知道为什么这两种计算方式不同。

标签: c ioctl block-device


【解决方案1】:

大多数处理 CHS 的工具只是组成了它们自己的值。这是生成您在 fdisk from a util-linux repo on github 中看到的值的函数。

static void recount_geometry(struct fdisk_context *cxt)
{
    if (!cxt->geom.heads)
        cxt->geom.heads = 255;
    if (!cxt->geom.sectors)
        cxt->geom.sectors = 63;

    cxt->geom.cylinders = cxt->total_sectors /
        (cxt->geom.heads * cxt->geom.sectors);
}

如您所见,它只是将headssectors 与固定值挂钩,然后计算cylinders 值以便计算出总驱动器容量。

HDIO_GETGEO ioctl 返回来自您正在使用的特定设备驱动程序的数字。如果您好奇,可以深入了解 drivers/scsi/sd.c 并找出您的特定值的来源。

也考虑阅读wikipedia page on CHS addressing。这些字段实际上没有任何意义 - 尤其是在固态 USB 记忆棒上!

【讨论】:

    猜你喜欢
    • 2015-12-15
    • 2011-01-09
    • 2012-09-19
    • 2019-06-19
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 2011-04-12
    • 2013-06-19
    相关资源
    最近更新 更多