resize extended partition

I usually don’t use Ubuntu auto-partitioning in installer but sometimes I get so lazy that I can make this terrible mistake.

Warning, have a backup copy before proceeding. I will not be responsible of data loss.

I usually do this on live systems because I always have a ready backup or I can lose data (e.g. on test systems) without problems.

The problem

The problem is not the automatic partitioning itself, the problem is the sector alignment that is not what fdisk expects.

Trying the following, I always do it on live systems with LVM onboard, the correct starting sector of sda5 partition will be preserved, but the correct starting sector of the extended sda2 partition wont.

# 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: 1.3 TiB, 1367947083776 bytes, 2671771648 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: 0xf5a0eaaf

Device     Boot   Start        End    Sectors    Size Id Type
/dev/sda1  *       2048    1499135    1497088    731M 83 Linux
/dev/sda2       1501182 2147483647 2145982466 1023.3G  5 Extended
/dev/sda5       1501184 2147483647 2145982464 1023.3G 8e Linux LVM

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

Partition 5 has been deleted.

Command (m for help): d
Partition number (1,2, default 2): 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): 2
First sector (1499136-2671771647, default 1499136): 
Last sector, +sectors or +size{K,M,G,T,P} (1501182-2671771647, default 2671771647):

Created a new partition 2 of type 'Extended' and of size 1.2 TiB.

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

Created a new partition 5 of type 'Linux' and of size 1.2 TiB.

Expert command (m for help): p
Disk /dev/sda: 1.3 TiB, 1367947083776 bytes, 2671771648 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: 0xf5a0eaaf

Device     Boot   Start        End    Sectors Id Type
/dev/sda1  *       2048    1499135    1497088 83 Linux
/dev/sda2       1499136 2671771647 2670270466  5 Extended
/dev/sda5       1501184 2671771647 2670268416 83 Linux 

The solution

The trick is to create the sda2 extended partition aligned on the existing one, then to create the sda5 partition AND then to move the beginning of the latter.

First of all a list of partition, with beginning, ending and partition type, will be displayed then the partitions will be deleted, the new ones will be created, starting sectors will be created aligned to the same starting sector of the original condition, will be checked that the type of partition is respected and THEN, only after all the operations will be finished, the new partition table will be written.

Since all these operations will be conducted on the partition table, all data won’t be destroyed and system never loses the disk.

After enlarging the disk on the hypervisor, partprobe must be run to reload all disk sizes and new partitions, then fdisk can be used.

Now partition listing will be taken and partitions 5 and 2 will be deleted.

# 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: 1.3 TiB, 1367947083776 bytes, 2671771648 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: 0xf5a0eaaf

Device     Boot   Start        End    Sectors    Size Id Type
/dev/sda1  *       2048    1499135    1497088    731M 83 Linux
/dev/sda2       1501182 2147483647 2145982466 1023.3G  5 Extended
/dev/sda5       1501184 2147483647 2145982464 1023.3G 8e Linux LVM

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

Partition 5 has been deleted.

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

Partition 2 has been deleted.

Now the partition 2 will be recreated with the correct starting sector. The default for fdisk would be 1499136, but it was created at 1501182

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): 2
First sector (1499136-2671771647, default 1499136): 1501182
Last sector, +sectors or +size{K,M,G,T,P} (1501182-2671771647, default 2671771647):

Created a new partition 2 of type 'Extended' and of size 1.2 TiB.

Now, the partition 5 will be created with the default starting block and then, in the expert menu (press x) the beginning will be moved on the original location (press b). Finally will be reassigned the correct partition type back to 8e (Linux LVM) and, after a last check, the new partition table will be written.

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

Created a new partition 5 of type 'Linux' and of size 1.2 TiB.

Command (m for help): x
Expert command (m for help): b
Partition number (1,2,5, default 5): 5
New beginning of data (1501183-2671771647, default 1503232): 1501184

Expert command (m for help): p

Disk /dev/sda: 1.3 TiB, 1367947083776 bytes, 2671771648 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: 0xf5a0eaaf

Device     Boot   Start        End    Sectors Id Type     Start-C/H/S End-C/H/S Attrs
/dev/sda1  *       2048    1499135    1497088 83 Linux        0/33/32  93/51/80    80
/dev/sda2       1501182 2671771647 2670270466  5 Extended   93/19/113 422/49/23
/dev/sda5       1501184 2671771647 2670270464 83 Linux      93/53/145 422/49/23
Expert command (m for help): r

Command (m for help): t
Partition number (1,2,5, default 5): 5
Partition type (type L to list all types): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): p
Disk /dev/sda: 1.3 TiB, 1367947083776 bytes, 2671771648 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: 0xf5a0eaaf

Device     Boot   Start        End    Sectors  Size Id Type
/dev/sda1  *       2048    1499135    1497088  731M 83 Linux
/dev/sda2       1501182 2671771647 2670270466  1.2T  5 Extended
/dev/sda5       1501184 2671771647 2670270464  1.2T 8e Linux LVM

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

We will get a warning of the system still using the old partition table, no worry, just run partprobe a second time.

# paprtprobe

Resizing PV and LV contained in the resized partition

First of all we will run pvresize, to acknowledge LVM that the physical volume has changed, then vgs to get the correct amount of data available, then lvresize to resize the logical volume.

# pvresize /dev/sda5
  Physical volume "/dev/sda5" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

# vgs
  VG      #PV #LV #SN Attr   VSize VFree
  vg        1   2   0 wz--n- 1.24t 250.00g

# lvresize -L+250G /dev/vg/root
  Size of logical volume vg/root changed from 1022.32 GiB (261715 extents) to 1.24 TiB (325715 extents).
  Logical volume root successfully resized.

Now the filesystem inside the logical volume can be resized as you whish. In this case was ext-4 filesystem so, after another partprobe (probably redundant) resize2fs will be used.

# partprobe

# resize2fs /dev/mapper/vg-root
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/mapper/vg-root is mounted on /; on-line resizing required
old_desc_blocks = 64, new_desc_blocks = 80
The filesystem on /dev/mapper/vg-root is now 333532160 (4k) blocks long.

That’s all!

Leave a 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.