'How kernel prints partitions of block devices in dmesg?
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.
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | KagurazakaKotori |
