This article explains how to manage and configure LVM storage, covering setup, volume creation, resizing, and filesystem handling.
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:
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.
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:
To install the necessary LVM tools, run the following command. On most Ubuntu systems, this package is already installed:
Copy
sudo apt install lvm2
When executed, you might see an output like:
Copy
jeremy@kodekloud:~$ sudo apt install lvm2[sudo] password for jeremy:Reading package lists... DoneBuilding dependency tree... DoneReading state information... Donelvm2 is already the newest version (2.03.16-3ubuntu3).lvm2 set to manually installed.0 upgraded, 0 newly installed, 0 to remove and 4 not upgraded.jeremy@kodekloud:~$
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 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:
Copy
sudo vgcreate my_volume /dev/sdc /dev/sdd
If you later add another disk, follow these steps to extend the VG:
Create a PV on the new disk:
Copy
sudo pvcreate /dev/sde
Extend the VG:
Copy
sudo vgextend my_volume /dev/sde
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:
Copy
sudo vgreduce my_volume /dev/sde
And if you no longer need this PV, remove it with:
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:
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:
Copy
sudo lvresize --size 2G my_volume/partition1
The system will warn you:
Copy
WARNING: Reducing active logical volume to 2.00 GiB.THIS MAY DESTROY YOUR DATA (filesystem etc.)Do you really want to reduce my_volume/partition1? [y/n]: Y
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:
Copy
sudo lvdisplay
Once an LV is created, you can format it with an ext4 filesystem using:
Copy
sudo mkfs.ext4 /dev/my_volume/partition1
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:
Copy
sudo lvresize --size 3G my_volume/partition1
To resize both the LV and its ext4 filesystem simultaneously, use the —resizefs option:
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!