Linux – How do commands like fdisk -l find the sector size

clustercommanddiskhard-disklinux

Does the MBR of the disk contain this information and therefore when i call a command like fdisk, a kernel level code eventually runs and reads it from a specific part in MBR? If so, which part of it? What offset?

If it's not in the MBR, then how can these types of commands find it? They can't be reading it from the beginning of a partition considering they need to calculate the starting address of that partition and they need the sector size to do so, don't they?

How are commands like fdisk implemented to find this information? Where do they read it from?

Best Answer

A device’s sector size isn’t stored in the MBR.

User space commands such as fdisk use the BLKBSZGET and BLKSSZGET ioctls to retrieve the sector sizes from disks. Those ioctls are handled by drivers in the kernel, which retrieve the relevant information from the drives themselves.

(There isn’t much documentation about the relevant ioctls; you need to check the kernel source code.)

You can see the relevant information using other tools which query drives directly, for example hdparm. On a small SSD, hdparm -I tells me

[...]
Logical  Sector size:                   512 bytes
Physical Sector size:                   512 bytes
Logical Sector-0 offset:                  0 bytes
[...]
cache/buffer size  = unknown
Form Factor: 2.5 inch
Nominal Media Rotation Rate: Solid State Device
[...]

On a large spinning disk with 4K sectors, I get instead

[...]
Logical  Sector size:                   512 bytes
Physical Sector size:                  4096 bytes
Logical Sector-0 offset:                  0 bytes
[...]
cache/buffer size  = unknown
Form Factor: 3.5 inch
Nominal Media Rotation Rate: 5400
[...]
Related Question