Expanding volumes using LVM

Expanding volumes using LVM

Running out of disk space on a critical volume can be an issue.  Fortunately, if you are using Logical Volume Manager (LVM) it is fairly simple to increase your available space.  In this article we will demonstrate how to expand volumes with LVM. We will be using the command fdisk, pvcreate, vgextend, lvextend, and resize2fs to accomplish our goals.  All with no downtime.

Preparing the disks

Our first step will be providing more space to expand your LVM volume into.  This can be done with virtual disks, or other physical disks, depending on your environment.  In this example, we will be doing so by providing additional physical storage space in which we can expand the LVM volume.   On the physical side, you can either add an additional hard drive to your system or in my case shrink the Windows portion to gain more available disk space on the current hard drive.  When performing these tasks be sure you have made your needed backups of any of your important data before preceding.  As we are messing with partitions, and in the case of this example the root partition, a mistake could cause data loss.

Windows 10 Disk Management screen shot

This example will start within the Windows 10 partition where I used Disk Manager to shrink the size of one of my data drives by 150 GB’s.  This is done by using the shrink option.  You are then able to see how much you are able to adjust the size of the chosen partition.

Once the drive has been adjusted you can leave it unallocated as the remaining steps will be taken care of on the Linux side of things.

Once you have chosen the space you require you can reboot the system and boot back into your Linux distro and proceed to the next phase of the project.

Partition to volume with fdisk

Issuing the fdisk -l command

The next step you can run fdisk -l which will allow you to view the current setup of the disks, and the disk in which you are planning to expand.  In this example, we are looking at the /dev/mapper/fedora_fedora-root which shows a current allocation of 43.3 GBs.

Next, we will need to create a partition from the available drive space we made at the start of this tutorial.

GUI fdisk option

You can use the disk partition utility within Linux to create a Linux partition.  You will also need to change the type of the partition to be LVM.  This can be done by clicking on the gears icon where you can then make the required changes.

Command Line option of fdisk

If you wish to create the partition in the CLI you can do so with the fdisk /dev/sdc6

  • Choose n to create a new partition
  • Select the number of the partition to create, options will be from 1 to 4
  • Press enter to accept the default starting sector
  • Press enter to accept the default ending sector
  • Change the type with the t command
  • Type 8e to change the partition to the LVM type.
  • Type p to print out the changes to verify everything is how you wish
  • Type w to write the changes.

Partition verification

Once you have the partitions created and properly configured we can verify this with the command fdisk -l.

fdisk -l results

As you can now see from the output of the fdisk -l command you will see your original partition (shown as /dev/sdc5 with a size of 48.1 G), and the new partition (shown as /dev/sdc6 with a size of 146.5 G).

*For the record you do not even need to use fdisk, you can just create the new physical volume with pvcreate /dev/sdc6 and skip the entire fdisk portion.

Creating the LVM using pvcreate

The next step involves increasing the logical volume.  We will use the pvcreate command to create a physical volume for later use by the logical volume manager (LVM).  In this case, the physical volume is our /dev/sdc6 partition.

pvcreate to create the physical volume

Issue the command pvcreate /dev/sdc6.

In this example, it first failed as the drive was currently mounted.  After unmounting the drive in the disk utility I ran the command again.  Again, there was a warning as a partition was already in place on this drive and asked if I wanted to wipe it.  I said yes to continue which resulted in the message that the Physical volume /dev/sdc6 was successfully created.  Use the pvs command to verify everything looks as it should.

Extending our volume using vgextend

Our next step is to issue the command vgdisplay to verify the name of the volume group.  This example has the volume group name set to fedora_fedora.  We can then extend the volume group with the command vgextend fedora_fedora /dev/sdc6.  You will get a response indicating that the Volume Group fedora_fedora has been successfully extended.

The pvscan command will scan all physical volumes so you can confirm both partitions.  You will see the original /dev/sdc5 partition and the new /dev/sdc6 partition.

vgextend to expand the volume group

The lvdisplay command will show you the logical volume name, which is shown as /dev/fedora_fedora/root.  The vgs command will verify everything is set on the volume group side.

Extending our logical volume using Lvextend

We now extend the logical volume using the lvextend command.  lvextend -r /dev/fedora_fedora/root /dev/sdc6.  It shows it changed from 43.25 G to 189.73 G and that the logical volume root was successfully resized.  The -r flag also will resize the file system, and if you include this you will be finished and the volume will be ready to go.

Resizing the file system

If you forget the -r flag when issuing the lvextend command, which I’ve done a few times myself.  You still have the option of manually resizing the file system with the command resize2fs /dev/fedora_fedora/root. You will get a report that the filesystem on /dev/fedora_fedora/root is now 49737728 blocks long.  If the file system is XFS, you will need to use xfs_resizefs dev/fedora_fedora/root instead.  In all, it is best to just remember to use -r when using the lvextend command.

Final verification using df

We can now run the df command to view all the filesystem on the Linux system.  You will notice that the available disk space on your selected drive has increased by the amount you picked at the beginning of this tutorial.

For myself, this came about as I was installing Fedora as a dual boot with Windows 10.  This article was originally written two years ago.  I’ve made some adjustments to the post as I’ve done this a number of times on productions servers at this point.  I no longer have that dual boot with Fedora, it’s now a dual boot with RHEL Workstation 7.4.  I do plan on reposting the article on dual booting with Windows 10.  Others that will be posted again is how to install NVIDIA drivers on RHEL, CentOS, and Fedora.

If you have any comments or questions, please feel free to ask them in the comment section below.

Cheers,

Ivan Windon – RHCSA

Share

Ivan Windon

Ivan Windon is a Site Reliability Engineer at IBM. Ivan is actively engaged in Cloud Technologies with AWS, Google, and Azure. Ivan has extensive experience with Linux and Windows administration, DNS, Networking, IDM, and Security. In his free time, he enjoys being with his wife and two children. The family enjoys hiking, and traveling when able. His favorite locations are Yosemite NPS, and San Francisco, California.

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.