Sectors are managed by the drive's controller, so you'll need to check what the drive reports to the OS through the various "inquiry" commands (such as SCSI READ CAPACITY).
The sector size isn't accessible through normal read/write operations, because... you need to know the sector size before you can issue the read/write operations in the first place.
On Linux, you can use lsblk -t
to see the topology parameters known by the kernel; the same information can be retrieved either directly from files in /sys/class/block/sda/queue
, or through libudev (all sysfs files are 'attributes' in libudev).
You can also check fdisk -l
for the "Sector size" line; it uses ioctl(BLKIOMIN), ioctl(BLKPBSZGET), and ioctl(BLKSSZGET) to retrieve this information from the kernel.
The most direct approach (and the one requiring the most privileges) is to send your own commands to the disk, e.g. for SCSI and ATA disks sg_readcap
will report "Logical block length" and "Logical blocks per physical block exponent".
Note that some USB-SATA adapters send wrong information in these fields.
Is the sector size somewhere stored on the drive Yes, but no. 4k drives may contain 512b in that place. You must ask physical characteristics of HDD (but in this case 4k drive may be treated as 512b drive by BIOS/driver too). – Akina – 2019-09-03T11:25:54.427