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:

This shows the location of the home partition as shown:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/CentOS-8-Extend-Root-Partition-Using-LVM-1.png" data-lazy- height="190" src="data:image/svg xml,” width=”661″>

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:

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/CentOS-8-Extend-Root-Partition-Using-LVM-2.png" data-lazy- height="57" src="data:image/svg xml,” width=”626″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/CentOS-8-Extend-Root-Partition-Using-LVM-3.png" data-lazy- height="79" src="data:image/svg xml,” width=”749″>

Step 5 – Format the volume

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/CentOS-8-Extend-Root-Partition-Using-LVM-4.png" data-lazy- height="184" src="data:image/svg xml,” width=”780″>

Using mkfs utility format the volume as:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/CentOS-8-Extend-Root-Partition-Using-LVM-5.png" data-lazy- height="199" src="data:image/svg xml,” width=”1129″>

Step 6 – Mount Home dir

Now mount the newly created volume as:

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”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/CentOS-8-Extend-Root-Partition-Using-LVM-6.png" data-lazy- height="79" src="data:image/svg xml,” width=”428″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/CentOS-8-Extend-Root-Partition-Using-LVM-7.png" data-lazy- height="250" src="data:image/svg xml,” width=”997″>

Conclusion

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

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/john-150×150.png60d8c8d562010.jpg" height="112" src="data:image/svg xml,” width=”112″>

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