内核如何在DMESG中打印块设备的分区?

发布于 2025-01-28 06:54:24 字数 323 浏览 3 评论 0原文

在添加内核启动或块设备上,它打印了块设备的分区,例如:

[    4.168995]  nvme0n1: p1 p2
[    4.202666]  nvme2n1: p1
[    4.228494]  nvme1n1: p1
[    5.104852]  sda: sda1 sda2
[    5.681698]  sdc: sdc1 sdc2
[    5.717981]  sde: sde1 sde2
[    5.718320]  sdb: sdb1
[    5.727097]  sdd: sdd1 sdd2

我想知道这些邮件在哪个内核代码中打印。

On kernel boot or block device added, it prints the partitions of block devices like:

[    4.168995]  nvme0n1: p1 p2
[    4.202666]  nvme2n1: p1
[    4.228494]  nvme1n1: p1
[    5.104852]  sda: sda1 sda2
[    5.681698]  sdc: sdc1 sdc2
[    5.717981]  sde: sde1 sde2
[    5.718320]  sdb: sdb1
[    5.727097]  sdd: sdd1 sdd2

I'd like to know in which piece of kernel code these messages are printed.

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

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

发布评论

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

评论(1

肤浅与狂妄 2025-02-04 06:54:24

我想我找到了答案(用于内核5.17)。

此消息在block/partitions/core.c:check_partition()中打印,但由多个函数生成。首先,它为pp_buf分配一个页面以保存消息,并将块名为“”。以nvme0为例。

对于以nvme0nbd0之类的数字结尾的块设备,它将名称替换为p后,将设备打印到缓冲区。

snprintf(state->name, BDEVNAME_SIZE, "%s", hd->disk_name);
snprintf(state->pp_buf, PAGE_SIZE, " %s:", state->name);
// " nvme0:"
if (isdigit(state->name[strlen(state->name)-1]))
        sprintf(state->name, "p");

然后,它调用check_part []中的函数以检查块设备是否具有支持的分区表。在这里,我将EFI(GPT)作为示例。该函数循环分区表条目和调用put_partition()将分区附加到pp_bufput_partition()(在block/partitions/check.h中)使用设备名称和分区索引来形成分区名称。

snprintf(tmp, sizeof(tmp), " %s%d", p->name, n);
// n == 1, formatted string: " p1", pp_buf: " nvme0: p1"

循环后,它将\ n附加到pp_buf并返回到check_partition(),然后打印pp_buf,pp_buf ,提供我们在DMESG中看到的消息。

 nvme0: p1 p2

I think I've found the answer (for kernel 5.17).

This message is printed in block/partitions/core.c:check_partition(), but it is generated by multiple functions. First it allocates a page for pp_buf to hold the message, and puts the block name to it. Taking nvme0 as an example.

For block devices ending with digits like nvme0 or nbd0, it replaces the name to p after printing the device to buffer.

snprintf(state->name, BDEVNAME_SIZE, "%s", hd->disk_name);
snprintf(state->pp_buf, PAGE_SIZE, " %s:", state->name);
// " nvme0:"
if (isdigit(state->name[strlen(state->name)-1]))
        sprintf(state->name, "p");

Then it calls the funtions in check_part[] to check if the block device has a supported partition table. Here I use efi (GPT) as the example. The function loops the partition table entries and calls put_partition() to append partitions to pp_buf. put_partition() (in block/partitions/check.h) uses the device name and partition index to form the partition name.

snprintf(tmp, sizeof(tmp), " %s%d", p->name, n);
// n == 1, formatted string: " p1", pp_buf: " nvme0: p1"

After the loop, it appends \n to the pp_buf and returns to check_partition(), then prints pp_buf, giving the message we saw in dmesg.

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