CentOS

CentOS 8 Extend Root Partition Using LVM

A few years ago, disk management was a resource-consuming task for system administrators. Scenarios such as running out of space were the bane of sysadmins, mainly because resolving it involved a lot of work. This would cause sysadmins to take servers offline, install new drives, partition and then extend them to the current system. It was not fun.

The Logical Volume manager solved this problem once and for all. LVM is a device mapper framework that allows for disk management in very easy steps.

LVM allows sysadmins to perform disk management tasks such as resizing, extending, and removing partitions flexibly and easily. Therefore sysadmins do not need to take the system offline to extend partitions and more.

In this tutorial, I will show you a method to use the LVM manager to extend the size of your root partition.

NOTE: This tutorial is not an introductory guide to LVM.

Pre-requisites

Before we get started with the tutorial, you will need to meet the following requirements.

  • Root or user with sudo privileges
  • Ensure you make a backup copy of all your files in case of trouble.

In the examples within this tutorial, I will be shrinking my home directory and add the extra space to the root directory. I’m also illustrating this tutorial on a CentOS 8 machine running on VMware.

Step 1 – Make Backups

The very first step before we proceed is to make a backup of your home directory. You don’t need to do this, but I cannot recommend it highly enough. Back up your files using file shares, copy them to a local drive, or compress them and move them to a new directory.

Step 2 – Unmount Home dir

The next step is to unmount the home directory. To view the location of the home partition, use the df command as:

$ sudo df

This shows the location of the home partition as shown:

Use the command below to unmount.

$ sudo umount /dev/mapper/cl-home

Step 3 – Delete Home Volume

Once successfully unmounted, we need to remove the home volume using lvm. Use the command as:

$ lvremove /dev/cl/home

This command will prompt you whether to proceed to remove the home volume. Press y to proceed.

Step 4 – Shrink Home Dir

The next step is to shrink the size of the home directory. That means we will recreate the home volume as a smaller-sized volume.

Start by getting the volume group name.

vgdisplay | grep “VG Name”
VG Name               cl

That gives us the volume group name as cl.

Now, all we have to do is create a new volume for /home using lvcreate command as:

$ lvcreate -L +5GB -n home cl

That will create the home volume with 5GB in size.

Step 5 – Format the volume

Now it’s time to format the home volume. Get the file type using the df command as:

$ df -T

Using mkfs utility format the volume as:

$ mkfs.xfs /dev/cl/home

Step 6 – Mount Home dir

Now mount the newly created volume as:

$ mount /dev/cl/home

Upon successful mount, restore your backups to the new home volume.

Step 7 – Extend Root

Now it’s time to extend the root partition with the free space available from shrinking the home directory. Start by getting the path of the root logical volume.

$ lvdisplay | grep “LV Path”

To extend the root partition with all the free space, the command as:

$ lvextend -rl +100%FREE /dev/cl/root

This command will extend the root partition with all the remaining free space from the home directory.

Conclusion

In this guide, we looked at how to extend the root partition by shrinking the home directory.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list