Create partition aligned using parted

  • I'm partitioning a non-SSD hard disk with parted because I want a GPT partition table.

    parted /dev/sda mklabel gpt

    Now, I'm trying to create the partitions correctly aligned so I use the following command to know where the first sector begins:

    parted /dev/sda unit s p free
    Disk /dev/sda: 488397168s
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Number  Start  End         Size        File system  Name      Flags
            34s    488397134s  488397101s  Free Space

    We can see that it starts in sector 34 (that's the default when this partition table is used).

    So, to create the first partition I tried:

    parted /dev/sda mkpart primary 63s 127s

    to align it on sector 64 since it's a multiple of 8 but it shows:

    Warning: The resulting partition is not properly aligned for best performance.

    The logical and physical sector sizes in my hard disk are both 512 bytes:

    cat /sys/block/sda/queue/physical_block_size
    cat /sys/block/sda/queue/logical_block_size 

    How do I create partitions correctly aligned? What am I doing wrong?

    Are you sure sector numbers start at zero?

    I tried too `parted /dev/sda mkpart primary 64s 128s` (in case it starts in 1) but it returns the same warning.

    It looks like there's a command to check if a value is aligned: Maybe try that with various values using a for loop and spit out the values that are ok?

    Burton, I didn't know about that command but it cann't be used until to have a partition.

    Sorry about that, I didn't read the description carefully enough.

  • lik

    lik Correct answer

    8 years ago

    In order to align partition with parted you can use --align option. Valid alignment types are:

    • none - Use the minimum alignment allowed by the disk type.
    • cylinder - Align partitions to cylinders.
    • minimal - Use minimum alignment as given by the disk topology information. This and the opt value will use layout information provided by the disk to align the logical partition table addresses to actual physical blocks on the disks. The min value is the minimum alignment needed to align the partition properly to physical blocks, which avoids performance degradation.
    • optimal Use optimum alignment as given by the disk topology information. This aligns to a multiple of the physical block size in a way that guarantees optimal performance.

    Other useful tip is that you can set the size with percentages to get it aligned. Start at 0% and end at 100%. For example:

    parted -a optimal /dev/sda mkpart primary 0% 4096MB

    Should the command not end `0% 100%`? Where does `4096MB` come from?

    Value 4096MB is used here just an example to show ability of mixing % and specific size numbers

    Just a note, I had to enclose the `'0%'` in quotes for my shell to execute the command properly.

    I found the `parted -a optimal` aligned my 500MB partition to sector 2098, but doing it manually/interactively the optimal was found 2048 start-- a 50 sector difference!

    Nevermind the above, I just noticed the parted `print` uses kB as 1000 bytes...

    Note that parted does not always set the alignment as requested, it depends on how you specify the values. Percentages usually work, see my answer below for more details on how this works.

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM