Skip to main content
Understanding the advantages of LVM (Logical Volume Manager) begins with examining a common disk partitioning challenge. Consider a disk with three sections: one partition at the beginning, another in the middle, and unpartitioned space at the end. In traditional partitioning, if you need to expand partition 2, you can extend it into the free space to its right. However, if you want to grow partition 1—which has no adjacent free space—you face a significant limitation. LVM overcomes this issue by abstracting the physical disk layout. Instead of rigidly partitioning the disk, you add the disk to LVM, which then manages all available space. The diagram below illustrates a disk layout where a beginning partition, a middle partition, and free unpartitioned space are managed seamlessly by LVM:
The image shows a diagram of a disk partition layout with three sections: Beginning Partition, Middle Partition, and Free Space (Unpartitioned), labeled under LVM.
Now, imagine that same disk setup is managed using LVM. When you want to expand the first partition, LVM identifies available free space—even if it’s not contiguous—and logically combines it with partition 1. To the operating system, the partition appears as a continuous block, streamlining tasks like resizing and storage management for administrators. This core concept of LVM sets the stage for its broader capabilities. Let’s dive into practical exercises to learn how to manage and configure LVM storage.

Setting Up Virtual Disks

For this lesson, we will attach three virtual disks to a virtual machine, each with a capacity of 5 GB. The diagram below shows a virtual machine connected to three virtual disks to support our exercises:
The image is a diagram showing a virtual machine connected to three virtual disks, labeled as a practical exercise.
To install the necessary LVM tools, run the following command. On most Ubuntu systems, this package is already installed:
When executed, you might see an output like:

Key LVM Concepts: PV, VG, LV, and PE

LVM uses specific terms to describe its components:
  • PV (Physical Volume): The actual storage device (disk, SSD, or partition).
  • VG (Volume Group): A pool of storage created by grouping one or more PVs.
  • LV (Logical Volume): A virtual partition carved out within a VG.
  • PE (Physical Extent): The smallest allocation unit on a PV.
Understanding these terms is crucial since most LVM commands reference one or more of these elements.

Examining Physical Volumes (PVs)

Physical Volumes are the underlying devices that LVM manages. To list available disks and partitions—including those already used for LVM—run:
Example output:
In the example above, /dev/sda3 is already used as a PV by Ubuntu. For our lesson, we will focus on using the new disks. Create two PVs on your new disks with:
After creating the PVs, check their status:
Example output:
The “PFree” column indicates the unallocated storage in each PV.

Creating and Extending a Volume Group (VG)

After setting up your PVs, the next step is to combine them into a Volume Group (VG). Think of a VG as a single virtual disk composed of multiple PVs. For example, to create a VG named “my_volume” using the two 5 GB PVs, run:
If you later add another disk, follow these steps to extend the VG:
  1. Create a PV on the new disk:
  2. Extend the VG:
The output will indicate that “my_volume” now includes three PVs with a larger combined size. To remove a PV not used by any LV, use:
And if you no longer need this PV, remove it with:

Creating Logical Volumes (LVs)

Logical Volumes act like partitions but are more flexible than traditional ones. With your VG established, you can create LVs within it.
  1. Create a logical volume named “partition1” of 2 GB within “my_volume”:
  2. Verify your Volume Group status to see that “partition1” is allocated and free space remains.
  3. Next, create a second logical volume named “partition2” with a size of 6 GB:
  4. To view all logical volumes, use:
Example session:
In this setup, “my_volume” functions as a virtual 10 GB disk, containing two LVs (2 GB and 6 GB) with some free space remaining.

Resizing Logical Volumes

One of LVM’s key features is the ability to resize LVs without impacting their logical appearance. Data is stored in units called physical extents (PEs). Although an LV may span non-contiguous physical extents, the system sees it as a continuous disk. To extend an LV to use all available free space in a VG, use the —extents option with 100%VG. For example, resizing “partition1” is done by:
A sample session might look like this:
Using this command, “partition1” expands without interrupting its continuous appearance to the operating system.
Shrinking a logical volume that contains data is risky. Always ensure your data is backed up before resizing volumes.
If you need to shrink an LV—say, returning it to 2 GB—make sure you understand the risks and use:
The system will warn you:
Confirm the operation by typing “Y”.

Handling Filesystems on Logical Volumes

An empty logical volume has no filesystem, so it cannot store files until you create one. LVs are typically made available using paths formatted as:   /dev/<volume_group>/<logical_volume> For example, an LV in “ubuntu-vg” might appear as /dev/ubuntu-vg/ubuntu-lv. To inspect detailed information about LVs, run:
Once an LV is created, you can format it with an ext4 filesystem using:
After formatting an LV, ensure that the filesystem is managed carefully. Resizing the logical volume without adjusting the filesystem can result in the filesystem not utilizing the extra space.
For example, resizing the LV without altering the filesystem:
To resize both the LV and its ext4 filesystem simultaneously, use the —resizefs option:
A sample output after resizing might be:
Keep in mind that while many filesystems (such as ext4) support online expansion, they may not allow shrinking after data has been written.

Conclusion

This lesson has covered the fundamentals of managing and configuring LVM storage—from creating physical volumes, forming volume groups, setting up logical volumes, and resizing them along with their filesystems. By abstracting physical storage into flexible, dynamically adjustable volumes, LVM alleviates many limitations found in traditional partitioning schemes, reducing downtime and simplifying storage management. For further reference, consult the LVM manual by running man lvm or use terminal tab completion for hints. Happy managing!

Watch Video

Practice Lab