How to Switch HDD Sector Size to 4096 Bytes on Linux

disklinux

I have some HDD disk which has 4096 physical sector size and 512 bytes logical size. It is SATA disk. Now I'd like to use 4kiB in Linux as a logical also sector size – not 512 bytes one. How can I achieve this? Is it possible to switch this disk to operate only in 4kiB mode?

How can I be sure that all the partitions I created are aligned to 4kiB? Do I have to manually calculate start and end sector numbers of given partition to have 4kiB alignment?

I'm using Linux and sometimes Windows. Mainly I'm creating partitions using Linux fdisk – not Windows one. Maybe using of "fdisk -b 4096" is enough solution? Hm… Probably not, because how Linux will be know which sector size given disk uses?

Best Answer

Unless you use options to force a legacy MS-DOS compatible mode, or use expert mode to specify exact LBA block numbers for the beginning and end of partitions, most modern partitioning tools (Linux and otherwise) will align partitions to multiples of 1MB by default. This is also what modern Windows does, and it guarantees compatibility with both 4kB sector size and various SSD and SAN storage devices which might require alignment to larger powers of two for optimal performance.

You can use lsblk -t to check the alignment offsets of each partition. If the value in the ALIGNMENT column is zero, then as far as the kernel knows, the partition should be optimally aligned.

To switch the HDD sector size, you would first need to verify that your HDD supports the reconfiguration of the Logical Sector Size. Changing the Logical Sector Size will most likely make all existing data on the disk unusable, requiring you to completely repartition the disk and recreate any filesystems from scratch. The hdparm --set-sector-size 4096 /dev/sdX would be the "standard" way to change the sector size, but if there's a vendor-specific tool for it, I would generally prefer to use it instead - just in case a particular disk requires vendor-specific special steps.

On NVMe SSDs, nvme id-ns -H /dev/nvmeXnY will tell (among other things) the sector size(s) supported by the SDD, the LBA Format number associated with each sector size, and the currently-used sector size. If you wish to change the sector size, and the desired size is actually supported, you can use nvme format --lbaf=<number> /dev/nvmeXnY to reformat a particular NVMe namespace to a different sector size.

Related Question