Linux: How to expand an ext4 filesystem online

In the older times to expand a file system you would need to unmount the filesystem and apply some commands typically using the parted…

Linux: How to expand an ext4 filesystem online
Photo by Jan Canty on Unsplash
Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…

In the older times to expand a file system you would need to unmount the filesystem and apply some commands typically using the parted command, but there is another tool around which is often used in cloud environments, the growpart util! lets see how we can use growpart to expand our filesystems online!

Installing growpart

growpart can be installed on DEB like systems using

sudo apt-get install cloud-utils

On RPM systems using

sudo yum install cloud-utils-growpart

Identifying unallocated disk space

Next step is to identify unallocated disk space on our device in order to allocate this disk space to our partition, the easiest way to do this is by using the lsblk command which provides a very nice and clean tree like view of our file systems.

$ sudo lsblk 
NAME    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT 
loop0     7:0    0  63.5M  1 loop /snap/core20/1974 
loop1     7:1    0  63.3M  1 loop 
loop2     7:2    0  91.9M  1 loop /snap/lxd/24061 
loop3     7:3    0  55.7M  1 loop /snap/core18/2785 
loop4     7:4    0  49.9M  1 loop 
loop5     7:5    0  53.3M  1 loop /snap/snapd/19361 
loop6     7:6    0  55.7M  1 loop /snap/core18/2751 
loop7     7:7    0 345.3M  1 loop /snap/google-cloud-cli/153 
loop8     7:8    0  63.5M  1 loop /snap/core20/1950 
loop10    7:10   0  53.3M  1 loop /snap/snapd/19457 
loop11    7:11   0 345.8M  1 loop /snap/google-cloud-cli/155 
sda       8:0    0   200G  0 disk 
├─sda1    8:1    0   100G  0 part / 
├─sda14   8:14   0     4M  0 part 
└─sda15   8:15   0   106M  0 part /boot/efi

From the output we can see that sdadevice has 200G of space but the sum of the sda partitions allocated disk space is less than 101G, this means that we can allocate the remaining 100G to a partition of choice

Allocating disk space to a partition

To allocate all the unused disk space to the sda1 partition we will use the growpart command

sudo growpart /dev/sda 1

After using the growpart command we need to use the resize2fs command to utilize the newly allocated space

sudo resize2fs /dev/sda1

Verify expansion

To verify that the ext4 filesystem has been successfully expanded we can use the df or the lsblk command

$ sudo lsblk 
NAME    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT 
loop0     7:0    0  63.5M  1 loop /snap/core20/1974 
loop1     7:1    0  63.3M  1 loop 
loop2     7:2    0  91.9M  1 loop /snap/lxd/24061 
loop3     7:3    0  55.7M  1 loop /snap/core18/2785 
loop4     7:4    0  49.9M  1 loop 
loop5     7:5    0  53.3M  1 loop /snap/snapd/19361 
loop6     7:6    0  55.7M  1 loop /snap/core18/2751 
loop7     7:7    0 345.3M  1 loop /snap/google-cloud-cli/153 
loop8     7:8    0  63.5M  1 loop /snap/core20/1950 
loop10    7:10   0  53.3M  1 loop /snap/snapd/19457 
loop11    7:11   0 345.8M  1 loop /snap/google-cloud-cli/155 
sda       8:0    0   200G  0 disk 
├─sda1    8:1    0 199.9G  0 part / 
├─sda14   8:14   0     4M  0 part 
└─sda15   8:15   0   106M  0 part /boot/efi

This dill display the current status of the filesystems and if everything gone as it should you should see the partition that has allocated the whole disk space.

Conclusion

We saw how we can expand ext4 file systems online, but remember, doing such kind of operations are risky and you should always take a backup before.

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…