How to extend logical & extended partition with fdisk

Steps taken according to Gilles' answer:

$ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/sda: 9.8 GiB, 10485760000 bytes, 20480000 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0cd7105f

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sda1  *       2048   999423   997376  487M 83 Linux
/dev/sda2       1001470 16775167 15773698  7.5G  5 Extended
/dev/sda5       1001472 16775167 15773696  7.5G 83 Linux

Command (m for help): d
Partition number (1,2,5, default 5): 2

Partition 2 has been deleted.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): e
Partition number (2-4, default 2):
First sector (999424-20479999, default 999424): 1001470
Last sector, +sectors or +size{K,M,G,T,P} (1001470-20479999, default 20479999):

Created a new partition 2 of type 'Extended' and of size 9.3 GiB.

Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (1003518-20479999, default 1003520):
Last sector, +sectors or +size{K,M,G,T,P} (1003520-20479999, default 20479999):

Created a new partition 5 of type 'Linux' and of size 9.3 GiB.

Command (m for help): x


Expert command (m for help): b
Partition number (1,2,5, default 5):
New beginning of data (1001471-20479999, default 1003520): 1001472

Expert command (m for help): p

Disk /dev/sda: 9.8 GiB, 10485760000 bytes, 20480000 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0cd7105f

Device     Boot   Start      End  Sectors Id Type    Start-C/H/S  End-C/H/S Attrs
/dev/sda1  *       2048   999423   997376 83 Linux       0/33/32   62/55/53    80
/dev/sda2       1001470 20479999 19478530  5 Extende    62/23/86 250/23/209
/dev/sda5       1001472 20479999 19478528 83 Linux     62/57/118 250/23/209

Then r(return to main menu) and w (write table to disk).


In the normal interface, Linux's fdisk applies alignment constraints to partitions. Which constraints depends on the version of fdisk. Older versions defaulted to cylinder alignment, for compatibility with older operating systems that were incompatible with LBA. When LBA was a little over two decades old, fdisk stopped catering for such ancient systems by default, and instead switched to 1MB alignment, which gives better performance on modern storage media.

In current versions of fdisk, to create partitions with any sector (512B) alignment, you need to first create the partition with the desired end point, then go to the expert menu (x) and use the command b to adjust the beginning of the partition (this changes the partition size, not where it ends). It does seem rather clumsy.