How to enlarge root partition and filesystem size of cloud Linux VM at runtime without rebooting Linux

It is common that the root disk space is not enough when running a Virtual Machine in the cloud such as Amazon Web Service (AWS). The cloud storage usually provides tools or facilities to enlarge a virtual disk size. However, to make the Linux recognize and and use the enlarged disks without rebooting the OS, some actions are needed. In this post, we will use Ubuntu 18.04 as one example, to show how to enlarge Linux root partition and filesystem sizes at runtime without rebooting Linux.

hard drive photo

Please note that filesystem and partitions resizing are dangerous operations and may lead to data lost. Please backup you data first before doing so by using snapshoting mechanism in the cloud service or using common backup tools.

Here is one example on a AWS EC2 node, named n8, which has 20GB root storage while it is almost used up as shown by df -hT:

root@n8:/var/log# df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs   16G     0   16G   0% /dev
tmpfs          tmpfs     3.1G  8.8M  3.1G   1% /run
/dev/nvme0n1p1 ext4       20G   20G  1.5M 100% /
tmpfs          tmpfs      16G     0   16G   0% /dev/shm
tmpfs          tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs          tmpfs      16G     0   16G   0% /sys/fs/cgroup
/dev/loop0     squashfs   88M   88M     0 100% /snap/core/5328
/dev/loop2     squashfs   17M   17M     0 100% /snap/amazon-ssm-agent/734
/dev/loop1     squashfs   88M   88M     0 100% /snap/core/5548
/dev/loop3     squashfs   13M   13M     0 100% /snap/amazon-ssm-agent/295
/dev/loop4     squashfs   13M   13M     0 100% /snap/amazon-ssm-agent/495
/dev/loop5     squashfs   87M   87M     0 100% /snap/core/5145
tmpfs          tmpfs     3.1G     0  3.1G   0% /run/user/1000

In the AWS Volume management console, we increased the size of the root partition disk to 100GB. Now, let’s look at the steps to add the storage to the root filesystem (/).

First, use growpart to grow the partition size. Here, the root partition is the partition number 1 of disk /dev/nvme0n1, we run the command:

root@n8:/var/log# growpart /dev/nvme0n1 1
CHANGED: partition=1 start=2048 old: size=41940959 end=41943007 new: size=209713119,end=209715167

From the STDOUT, the size has been changed.

However, the filesystem size is still the same although the partition size changed. We need to update the filesystem to use the more storage capacity available.

We can sue the resizee2fs command as follows.

root@n8:/var/log# resize2fs /dev/nvme0n1p1
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/nvme0n1p1 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 13
The filesystem on /dev/nvme0n1p1 is now 26214139 (4k) blocks long.

After it executed successfully, we can check the filesystem size by df -hTgain.

root@n8:/var/log# df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs   16G     0   16G   0% /dev
tmpfs          tmpfs     3.1G  724K  3.1G   1% /run
/dev/nvme0n1p1 ext4       97G   20G   78G  20% /
tmpfs          tmpfs      16G     0   16G   0% /dev/shm
tmpfs          tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs          tmpfs      16G     0   16G   0% /sys/fs/cgroup
/dev/loop0     squashfs   88M   88M     0 100% /snap/core/5328
/dev/loop2     squashfs   17M   17M     0 100% /snap/amazon-ssm-agent/734
/dev/loop1     squashfs   88M   88M     0 100% /snap/core/5548
/dev/loop3     squashfs   13M   13M     0 100% /snap/amazon-ssm-agent/295
/dev/loop4     squashfs   13M   13M     0 100% /snap/amazon-ssm-agent/495
/dev/loop5     squashfs   87M   87M     0 100% /snap/core/5145
tmpfs          tmpfs     3.1G     0  3.1G   0% /run/user/1000

Now you can see the new root partition size is around 100GB now. Cheers.

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *