Resizing a root partition online, without rebooting

Posted on November 18, 2016 in systems, linux, generals, lvm2, disk, pvcreate, lvcreate

You need to be careful ! Do not do this on production environment :)

Source : This blog

Initial state

  • A disk of about 50 Gb
  • Root partition with a seen size of about 25 Gb

Goal

We want to obtain a root partition of 50 Gb without rebooting.

Procedure

Deleting and create new partition table

We connect fdisk to the choosen disk :

fdisk /dev/sda

We generate a new partition table :

Command (m for help): p

Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders, total 104857600 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
Disk identifier: 0x0009256d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    50329215    25163584   83  Linux
/dev/sda2        50329216    52426367     1048576   82  Linux swap / Solaris

Command (m for help): d
Partition number (1-4): 2

Command (m for help): d
Selected partition 1

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-104857599, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599): +103809023

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p):
Using default response p
Partition number (1-4, default 2):
Using default value 2
First sector (103811072-104857599, default 103811072):
Using default value 103811072
Last sector, +sectors or +size{K,M,G} (103811072-104857599, default 104857599):
Using default value 104857599

Command (m for help): p

Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders, total 104857600 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
Disk identifier: 0x0009256d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048   103811071    51904512   83  Linux
/dev/sda2       103811072   104857599      523264   83  Linux

Before writing changes to disk we have to set proper partition types and make root partition bootable :

Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): 83

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82
Changed system type of partition 2 to 82 (Linux swap / Solaris)

Command (m for help): a
Partition number (1-4): 1

Command (m for help): p

Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders, total 104857600 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
Disk identifier: 0x0009256d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   103811071    51904512   83  Linux
/dev/sda2       103811072   104857599      523264   82  Linux swap / Solaris

Finally, we write the new partition table :

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 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)
Syncing disks.

Resizing the partition and reformating the swap :

# resize2fs /dev/sda1
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 4
The filesystem on /dev/sda1 is now 12976128 blocks long.

# mkswap /dev/sda2
# swapon /dev/sda2

Final state

  • Root partition with a size of about 50 Gb