Linux 内核中 USB 设备和 HDD 设备的盘符

发布于 2024-10-21 02:20:15 字数 180 浏览 5 评论 0原文

如何为 USB/HDD 驱动器分配驱动器盘符?我的意思是在代码级别。我查看了代码,注意到gendisk struct具有disk_name。这给出了sda/sdb/sdc....等。但是,如果磁盘被检测为sda1、sdc1...,那么这些名称可以从结构/代码中获取吗?

How the drive letter is assigned to USB/HDD drives? I meant in the code level. I looked at the code and noticed that the gendisk struct having the disk_name. that gives sda/sdb/sdc....etc. But if the disk is detected as sda1, sdc1...then where these names can be get form the structures/code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隱形的亼 2024-10-28 02:20:15

sda/sdb等是代表整个驱动器的块设备。当驱动器被分区时,您将看到 sda1、sdc1 等。这些块设备仅用于访问该分区。

我对代码并不熟悉,但希望这能帮助您指明正确的方向。

sda/sdb, etc are the block device representing the entire drive. When the drive is partitioned, you will see the sda1, sdc1, etc. These block devices are used to access just that partition.

I'm not familiar with the code specifically, but hopefully this will help point you in the right direction.

青芜 2024-10-28 02:20:15

一个有用的代码调查起点是函数 disk_name(),在 block/partition-generic.c 中定义:

  /*
   * disk_name() is used by partition check code and the genhd driver.
   * It formats the devicename of the indicated disk into
   * the supplied buffer (of size at least 32), and returns
   * a pointer to that same buffer (for convenience).
   */

  char *disk_name(struct gendisk *hd, int partno, char *buf)
  {
          if (!partno)
                  snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
          else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
                  snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
          else
                  snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);

          return buf;
  }

One useful investigation starting point code-wise would be function disk_name(), defined in block/partition-generic.c:

  /*
   * disk_name() is used by partition check code and the genhd driver.
   * It formats the devicename of the indicated disk into
   * the supplied buffer (of size at least 32), and returns
   * a pointer to that same buffer (for convenience).
   */

  char *disk_name(struct gendisk *hd, int partno, char *buf)
  {
          if (!partno)
                  snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
          else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
                  snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
          else
                  snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);

          return buf;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文