5

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?

8

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
[...]
  • Does this mean that hdparm sends ATA commands to the device itself? I would have thought you'd have to go through the kernel to get to the device. – mjb2kmn Nov 1 '18 at 19:26
  • 1
    So if we are talking about the lowest level, where on the disk this sector size is stored and "who" retrieves this information first and pass it to upper levels when i call something like fdisk ? a kernel code? I asked this question because i was looking at the MBR of my hard disk and i was wondering if the sector size is stored there and a kernel level code reads it from there or not? – John P Nov 1 '18 at 19:27
  • @mjb2kmn the two aren’t exclusive ;-). hdparm it sends ATA commands to the device itself, using the SG_IO ioctl (so with the help of the kernel). – Stephen Kitt Nov 1 '18 at 19:56
  • @JohnP the kernel asks the device, which typically stores the information in its firmware (but that’s really up to the device). – Stephen Kitt Nov 1 '18 at 19:58
  • @StephenKitt so it is not stored in the MBR, correct? – John P Nov 1 '18 at 20:01
2

There are a few questions in there, I tried to track down the answer to "Where does fdisk find the sector size of a disk?" The best I've found is that it gets the info from ioctl.

ioctl info on Wikipedia and Linux man page.

fdisk source code link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.