How to Increase Size of Root Partition in CentOS

How to Increase Size of Root Partition in CentOS

Being a system engineer, I often face scenarios in which I need to resize various system partitions in my Linux servers. Although this task isn’t that difficult, it’s definitely a very critical one because it can lead to the dis-functioning of your whole server. So, we must be very careful and cautious while trying to resize our system volumes.

Logical Volume Management
Logical Volume Management

In this post, I’m going to take the extra space from the swap partition and take that space to my root partition. Note: it’s recommended to have swap size of twice the RAM size, so keep it in mind while you shrink your swap partition. In my case, I had accidentally configured way more swap space and insufficient root size. So, I had to shrink the swap volume and increase the root volume. Here are the steps I took to fulfill this objective:

Analyze Existing Volume Layout

There are various tools to analyze our existing volume layout. We can begin by using df -h, which displays the disk usage of our partitions and their respective mapped volumes in filesystem. Then, we can display the information of our physical disk and its partitions by using fdisk -l. This will list the available physical disks and its partitions.

Then, we can view the volume group and logical volumes using the commands vgdisplay and lvdisplay respectively. Based on the information displayed by these commands, we can understand the current disk space utilization for making the necessary changes.

Shrink Swap Volume

Before we shrink the Swap volume, we need to know what is its mapped name in LVM. It can be found using lvdisplay command. In my case, it’s called /dev/centos/swap. Then, I shrunk it as follows:

First, let’s turn off the Swap volume so that we don’t interfere with its functioning. The following command will echo the command itself upon successful execution.

swapoff -v /dev/centos/swap

Second, let’s reduce its size. In my case, I’m taking 3 GB out of it.

lvreduce /dev/centos/swap -L -3G

Third, let’s re-format the swap volume to implement the changed volume size.

mkswap /dev/centos/swap

Lastly, let’s turn the Swap on to bring it online.

swapon -va

This should resize our Swap volume. Let’s also verify it by using following commands:

cat /proc/swaps

free -h

Increase Root Volume

Until now, we’ve already shrunk the volume size of Swap, which takes the freed space back to the logical volume. Now, we can take this freed space in volume group and increase our root partition. The volume size incremented can be tested using following command. Note the -t flag.

lvextend -t -r -l+100%FREE /dev/mapper/centos-root

If this test command completes successfully, we can re-run the command without using the -t flag. This will increase the size of root partition to claim all the available free space in volume group.

lvextend -r -l+100%FREE /dev/mapper/centos-root

Then, we can verify it by again using the commands df -h and lvdisplay. We should have larger root volume and smaller swap volume.

That’s it for this post. I hope this has been useful and informative for you. Please let me know of your opinion in the Comments section below. And as always, thanks for reading! 🙂

Comments are closed.