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
The accepted answer by @lik shows how you can specify the preferred alignment. However, parted always uses that alignment for checking the resulting partition, but not always creates partitions with that alignment.
TL;DR: When 1MiB is the optimal alignment, specifying 0% will work for disks of 200MiB and bigger. For smaller disks or larger alignment, specifying 0G could work where 0% does not. See below about why.
Parted does try to satisfy the alignment constraints when creating a partition, but it also will not deviate from your requested values too much. What counts as “too much” depends on how you specified the postions.
When you specify a start (or end) position to the mkpart command, it internally generates a range of acceptable values. This range is centered on the value you specify, and extends equally on both sides by half the unit size you used (that’s what I read from the code, the comments say one full unit size on both ends).
For example, when you specify “10M”, it will try to use any position between 9.5M and 10.5M. The same goes for percentages, so if you specify 0% it will use any value between 0% and 0.5% (it does not go negative, obviously).
One exception is that when using power-of-two-units (such as KiB for 1024, as opposed to K for 1000) parted assumes you’re trying to specify an exact position and only considers the exact value you specified.
Since optimal alignment typically seems to be 1MiB-aligned, the K and M units will often not have sufficient room to reach optimal alignment. Specifying positions in G should have plenty room, but % is usually also fine.
So, this is why 0% usually works, though even that has limits.
My solution has been to use gdisk which can perform partitions alignment automatically on a 2048 sectors (1024KiB) by default, although it can be changed in the expert menu.