In this article, we’ll look at how to extend a disk partition in Linux online without shutting down the system or unmounting the partition. You can use the fdisk tool to delete a partition and create a new larger partition in its place on the fly without losing data. In my case, a Linux VM running on VMware ESXi ran out of space on the ext4 data partition.
Print information about used space in Linux:
df -hT
Increase the size of the virtual disk (vmdk) using VMware vSphere Client (from 5 GB to 10 GB in this example).
Rescan the drive to see a new size:
echo 1>/sys/class/block/sdb/device/rescan
Make sure Linux sees the new disk size:
lsblk
Now we will use the fdisk tool to remove and recreate the partition without losing any data.
fdisk /dev/sdb
List partitions (print):
p
In our example, there is one partition /dev/sdb1 on the disk
Delete the current partition (the data remains on the disk):
d
Create a new partition:
n
Partition type (primary):
p
Specify partition number:
1
Now specify the starting sector of the partition. It must match the start sector of the partition that was removed earlier. In our example, this is 2048.
Then specify the last sector of the partition (by default, it is proposed to use all available space for the partition).
Check again that the partition has grown to the correct size:
p
Save changes:
w
A warning will appear:
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)
Update the partition table on the sdb disk:
partx -u /dev/sdb
Now you can expand the ext4 file system on the sdb1 partition:
resize2fs /dev/sdb1
Disable the space reserved for root:
tune2fs -m 0 /dev/sdb1
Check the used space:
df -hT
So you’ve expanded a partition in Linux without unmounting or rebooting the host.