Resizing an EXT4 or XFS partition on CentOS without unmounting

Disclaimer: While resizing any live and especially root partition you may risk loosing data thus it is not recommended. Always ensure that you have a backup before performing any of these operations.

1. Resize the disk on your hypervisor

First start by adding more space in VMware or any other hypervisor of your choice.
In this case I want to grow the disk from 5GB to 10GB.

2. Check initial disk size and space

Let's check that the partition size in CentOS is still showing the old value of 5GB:

[root@server ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda2 4.7G 1018M 3.7G 22% /
/dev/vda1 240M 101M 123M 46% /boot

However in fdisk, we can see that the total disk size has been updated to 10GB:

[root@server ~]# fdisk -l
Disk /dev/vda: 10.5 GB, 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
Disk label type: dos
Disk identifier: 0x0008b577
Device Boot Start End Blocks Id System

/dev/vda1 * 2048 526335 262144 83 Linux
/dev/vda2 526336 20479999 9976832 83 Linux

Because it's a root partition, it can't be unmounted:

[root@server009 ~]# umount /
umount: /: device is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))

3. Delete the parition you want to resize

Run the fdisk command and point it at the entire disk (not the partition e.g. vda1).
Don't worry, you're not actually deleting the data but only the partition structure.

[root@server ~]# fdisk /dev/vda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Type "p" to list the parition table:

Command (m for help): p
Disk /dev/vda: 10.5 GB, 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
Disk label type: dos
Disk identifier: 0x0008b577
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 526335 262144 83 Linux
/dev/vda2 526336 20479999 9976832 83 Linux

Now delete the partition that you want to resize using "d".

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

If it's your only partition, make sure that you note down the start block as you will need to enter it when creating the new partition in the next step. In my case, I have a boot partition first, so it will automatically start from the next block and I don't need to remember it.

Create the new partition

Use "n" and then "p" to create a new primary partition. As first sector, use the previously noted block number:

Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2):
First sector (526336-20479999, default 526336):
Using default value 526336
Last sector, +sectors or +size{K,M,G} (526336-20479999, default 20479999):
Using default value 20479999
Partition 2 of type Linux and of size 9.5 GiB is set

Commit changes and reboot

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.

Now use forcefsck before rebooting to ensure the partition is checked before it's mounted:

touch /forcefsck

Resize the file system

After rebooting you will need to resize your filesystem:

If you have an EXT4 partition use the following command:
resize2fs /dev/vda2

If it's an XFS, you'll need to use this:
xfs_growfs /dev/vda2

Replace the partition name with the one you have just resized.

Finally, check that the space is showing correctly:

[root@server ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda2 9.6G 1018M 8.6G 11% /
/dev/vda1 240M 101M 123M 46% /boot