4

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?

2 Answers 2

8

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.

9
  • Regarding using of "hdparm --set-sector-size..." - is this permanent setting? I mean is it enough to do this only once and even after the reboot the logical sector size will be as I set before reboot? Jan 25, 2020 at 7:39
  • 1
    I haven't personally tested this, but I would very much expect changing the logical sector size to be a persistent setting, as it makes a pretty fundamental change to how the data is accessed. But that is one of the reasons why I'd recommend using a vendor-specific tool for that job, if one is available: if a particular disk model needs something special to make the setting persist, a vendor-specific tool would "know" for sure how to do it right.
    – telcoM
    Jan 25, 2020 at 9:21
  • Finally someone that answers how to do this in a NVMe disk! THANKS A LOT!
    – TanisDLJ
    Jul 22, 2020 at 10:50
  • I wonder if it's such a good thing to switch to 4Kn sectors. Wikipedia states that individual applications may also have to be able to handle a drives sector size (see picture at en.wikipedia.org/wiki/Advanced_Format#512e). The linux kernel supports 4Kn since ca. 2010, but how can we be sure that there's no application left that's relying on 512e ? I just opened another post for this (unix.stackexchange.com/questions/761398/…). Trying to weight the pros and the cons of switching to 4Kn.
    – ChennyStar
    Nov 17, 2023 at 10:00
  • @ChennyStar Most normal applications read files instead of raw disk blocks, and in such cases, the disk cache within the operating system can and will easily act as an "adapter". Applications that access raw disk blocks would be e.g. disk cloning and partitioning tools, or database engines with their own disk formats. The picture you referred to is oversimplified anyway: most modern OSs will entirely bypass the BIOS except during the boot process.
    – telcoM
    Nov 17, 2023 at 11:20
1

As the block counter is 32 wide, you can address with this counter max of 2TB disk space divided by 512 B sectors. Hence the min. addressable portion of the modern disk is called cluster and usualy is of the 4096 = 8 x 512 Bytes. Even the internal buffers and programs deals still with 512B sector it is just the logical interpretation only. In the man fdisk you can read: Recent kernels know the sector size. Use this option only on old kernels or to override the kernel's ideas. You, mainly on linux, do not need to do any changes of the default configurations.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .