libblkid, or why you don’t need to worry about 4K disk format

You may have read of the new 4K Advanced Format drives now becoming popular on the market. In fact, if you’ve got a drive larger than 1 TB, you probably have one, even if you don’t know it. I’ve found lots of people assembling home NAS servers using 2 TB drives, linux software RAID or LVM, and all of them are concerned about aligning various parts of the storage infrastructure to the new 4K format. But, thanks to libblkid, you don’t have to.

Full disclosure: I’m one of those people who assembled such a NAS. And I was one of those people who spent an inordinate amount of time worrying about the 4K alignment problem. I eventually learned that I’d been wasting my time. When you find people on various mailing lists, forums, and blogs talking about this, please point them here.

The fundamental problem is the need to align data structures to the physical storage device’s boundaries. If your device stores data in block of 4K, then you want to begin writing your data at a multiple of 4K. You do not want to start writing 4K of data beginning 2K from the start of the device, because that will mean reading and writing two blocks of data. It would be more efficient to align to the boundary and write only one block.

All low-level storage systems, from filesystems (ext4) to logical volume managers (lvm2) to software raid (mdadm) use various block sizes. The terms vary from blocks to chunks to stripes, but the concept is the same. We want these blocks to align to what is best for the underlying layers, from any RAID configuration down to the physical devices.

To solve this problem, libblkid provides topology information for block devices. This includes physical and logical block sizes. As of spring 2010, support for libblkid is available in all the standard tools:

  1. fdisk, since util-linux >= 2.15. You should start with ‘-c -u’ to disable DOS compatibility and use sectors instead of cylinders.
  2. parted, since parted >= 2.1. Parted defaults to 1 MiB alignment for unknown disks, which should align to just about any sector size. This includes gparted and other graphical tools such as the partitioner used in the Ubuntu desktop installer.
  3. mdadm, since util-linux >= 2.15. It is no longer necessary to manually specify stride and stripe-width. All MD superblocks are positioned either at the end of the array (0.9 and 1.0) or in multiples of 4K from the beginning (1.1, 1.2).
  4. lvm2, since util-linux >= 2.15.
  5. mkfs.{ext,xfs,gfs2,ocfs2} all support libblkid directly.

If you’re uncertain, verify that your tool depends on util-linux or libblkid. You can use the package manager, although you may need to examine a dependant library:

user@server:~$ dpkg -s libparted0debian1 | grep libblkid
Depends: libblkid1 (>= 2.17), libc6 (>= 2.8), libdevmapper1.02.1 (>= 2:1.02.36), libuuid1 (>= 2.16)

Or use ldd, which is more direct:

user@server:~$ ldd /sbin/parted | grep libblkid
        libblkid.so.1 => /lib/libblkid.so.1 (0x00007f5833228000)

Conclusion: if you are running Ubuntu 10.04, Fedora 13, or RHEL 6 or later, you do not have to do anything to use your 4K disk efficiently. Create the RAID array, logical volume, and filesystem exactly as you normally would.

Tags: ,

  1. Steve’s avatar

    I had some issues with this recently when installing Ubuntu 11.10 64-bit Desktop on a new (2012) Seagate 2TB 4k format drive. After installation the Ubuntu graphical disk utility reported alignment issues with the newly installed GPT disk and warned that performance would be negatively impacted. After re-partitioning with GParted, then re-installing, the warnings went away.
    I’m going to reinstall with Ubuntu 11.10 Server 64-bit to see if the same issues arise.

    Reply

    1. Tyler Wagner’s avatar

      How did you do the partitioning originally? Using the partition editor during the install, or did you re-use an existing partition layout?

      The partitioner in the installer is linked against libblkid. It should always generate aligned partitions.

      Reply

      1. Steve’s avatar

        I took the default installer options and used guided partitioning and let the installer decide how to partition.

        Here’s what the partition layout looks like according to parted (sda was partitioned by the Ubuntu installer, sdb was manually partitioned with GParted):

        root@amd350:/home/steve# parted -l
        Model: ATA ST2000DM001-9YN1 (scsi)
        Disk /dev/sda: 2000GB
        Sector size (logical/physical): 512B/4096B
        Partition Table: gpt

        Number Start End Size File system Name Flags
        1 17.4kB 20.0MB 20.0MB fat16 boot
        2 20.0MB 1992GB 1992GB ext4
        3 1992GB 2000GB 8169MB linux-swap(v1)

        Model: ATA ST2000DM001-9YN1 (scsi)
        Disk /dev/sdb: 2000GB
        Sector size (logical/physical): 512B/4096B
        Partition Table: gpt

        Number Start End Size File system Name Flags
        1 1049kB 22.0MB 21.0MB fat16 boot
        2 22.0MB 1074GB 1074GB ext4
        3 1074GB 1082GB 8590MB linux-swap(v1)
        4 1082GB 1092GB 10.0GB test lvm

        For partitioning sdb I used the default GParted option of “Align to: MiB:

        Reply

        1. Tyler Wagner’s avatar

          Weird. sda’s partitions are aligned correctly except for the start of the fat16/boot partition. That won’t affect performance in any noticeable way. Is that partition mounted as /boot, or used in Ubuntu at all?

          I haven’t used the installer partitioner in guided mode, uh, ever. :)

          Reply

          1. Steve’s avatar

            I believe that’s the EFI boot partition. It doesn’t seem that the installer takes 4k sector drives into consideration when creating it.

            Reply

            1. Tyler Wagner’s avatar

              I’ll have to look into EFI boot. That’s news to me.

              With a start of 17.4kB from start of disk, it doesn’t even take 512k-sector-size disks into consideration. On the plus side, a non-optimal performance for reading the first 20MB of your disk won’t really matter.

              Reply

              1. Steve’s avatar

                Thanks for taking the time to respond to my comments.

                The release notes for Ubuntu 10.04 LTS does state that by default partitions are aligned to 1MB boundaries by default:
                https://wiki.ubuntu.com/LucidLynx/ReleaseNotes#Partition%20alignment%20changes%20may%20break%20some%20systems

                I couldn’t find anything in the 11.10 release notes about this, however.

                My troubleshooting is being hampered by a limited & meagerly documented BIOS on the system in question which is complicating figuring out the EFI issues.

                I did some testing with VirtualBox and UEFI but I believe their implementation is limited to providing just enough functionality to enable OSX guests to boot on Mac hosts. I can install Ubuntu with emulated EFI in the guest but can’t get past the EFI boot screen after the first reboot.

                Reply

Reply to Steve Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.