Determine sector-size of a drive

0

I know usually the sector size is 512 B but some drives use 4 KiB (advanced format).

How can I check which one is used by a drive? Is the sector size somewhere stored on the drive - e.g. in the MBR or GUID-partitiontable?

I'm interested in solutions for Windows 10 and Linux. It would be also great to know how to read it with a Python script or a hex editor.

Markus Bauer

Posted 2019-09-03T10:52:33.717

Reputation: 1

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

Answers

1

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.

user1686

Posted 2019-09-03T10:52:33.717

Reputation: 283 655