Guide to managing Logical Volumes (LVM) on Linux: Create, extend, and shrink partitions flexibly

Linux tutorial - IT technology blog
Linux tutorial - IT technology blog

Logical Volume (LVM) on Linux: A flexible solution for storage space

For those new to Linux servers, managing disk space can be quite confusing. While physical drives or traditional partitions are limited by fixed sizes, LVM (Logical Volume Manager) opens up an entirely different world: superior flexibility. Imagine LVM as an intermediary layer between physical disks and the operating system. It allows you to manage “virtual partitions” easily, without worrying about the complex physical structure underneath.

I still remember, when I first started working, on the company’s old CentOS 7 server, I spent a lot of effort optimizing it to achieve the desired performance, especially for services that required large and constantly changing storage space.

LVM was truly a “savior” back then; it helped me extend capacity for important directories like <a href="https://itfromzero.com/en/linux-vi-en/log-rotation-on-linux-with-logrotate-rescuing-your-disk-drive-in-the-middle-of-the-night.html">/var/log</a> or /home without having to shut down the server or deal with the hassle of reformatting the drive. Thanks to LVM, I saved hours of downtime and avoided the unnecessary risk of data loss.

Quick Start: Get started with LVM in 5 minutes

Want to dive right into LVM? I get it! Here’s a super quick guide for you to create, format, and mount a basic LVM partition in just a few steps. Let’s assume you have an empty, unpartitioned drive like /dev/sdb.

Step 1: Prepare the disk

First, you need to create an LVM-type partition on the new disk. I’ll use fdisk, but you can choose parted if you’re more familiar with it.


sudo fdisk /dev/sdb

Inside fdisk, follow these steps:

  • Press n to create a new partition.
  • Choose p for a primary partition.
  • Enter the partition number (usually 1).
  • Accept default values for the first and last sectors to use the entire disk.
  • Press t to change the partition type.
  • Enter code 8e (Linux LVM).
  • Press w to write changes and exit.

After exiting fdisk, you sometimes need to run partprobe for the kernel to recognize the new partition:


sudo partprobe

Step 2: Create a Physical Volume (PV)

Convert the partition /dev/sdb1 into a Physical Volume (PV) so LVM can manage it:


sudo pvcreate /dev/sdb1

Step 3: Create a Volume Group (VG)

Next, group the newly created PV into a Volume Group (VG). For example, I’ll name it vg_data:


sudo vgcreate vg_data /dev/sdb1

Step 4: Create a Logical Volume (LV)

Now, let’s create a Logical Volume (LV) from the VG vg_data. I’ll create an LV named lv_storage with a size of 10GB:


sudo lvcreate -L 10G -n lv_storage vg_data

Step 5: Format and Mount the LV

Finally, format the newly created LV with the ext4 filesystem and mount it to the /mnt/data directory:


sudo mkfs.ext4 /dev/mapper/vg_data-lv_storage
sudo mkdir /mnt/data
sudo mount /dev/mapper/vg_data-lv_storage /mnt/data

To ensure the LV automatically mounts on reboot, you need to add the following line to the <a href="https://itfromzero.com/en/linux-vi-en/linux-disk-mount-and-management-guide-from-lsblk-to-fstab.html">/etc/fstab</a> file:


sudo echo "/dev/mapper/vg_data-lv_storage /mnt/data ext4 defaults 0 0" | sudo tee -a /etc/fstab

Done! Now you have an LVM partition ready to use.

Detailed Explanation: What is LVM and why do we need it?

Imagine this: our physical hard drive is like a large cabinet with fixed compartments. When you create traditional partitions (like /dev/sda1, /dev/sda2), you are dividing the cabinet into compartments of predefined sizes. If one compartment becomes full, getting more space from an empty adjacent compartment is almost impossible. You would have to move all contents, tear down walls, and rebuild, which is very complicated.

LVM emerged to thoroughly address this limitation. It provides a significantly more flexible method of disk space management. Instead of dividing the cabinet into rigid compartments, LVM allows you to create a “common space” (pool) from multiple drives. From this, you can easily “carve out” virtual compartments (Logical Volumes) of any desired size. The great thing is that you can extend or shrink these virtual compartments at any time, without affecting the data or the overall structure of the cabinet.

Basic LVM Concepts

To help you visualize better, I will explain the core components of LVM:

  • Physical Volume (PV): These are the physical disks or traditional partitions that you want LVM to manage. LVM will “transform” them into usable blocks. You can think of PVs as the basic “bricks” that LVM uses to build.
  • Volume Group (VG): A VG is a collection of PVs. It’s like grouping many “bricks” together to create a massive “material store.” All the space from the PVs within a VG forms a large pool, from which you can allocate to Logical Volumes.
  • Logical Volume (LV): These are the virtual “partitions” created from the space of a VG. The operating system will see and use an LV like a regular disk (e.g., /dev/mapper/myvg-mylv). The outstanding advantage is that you can easily change the size of this LV without touching the physical structure of the hard drive.

Outstanding Benefits of LVM

Why is LVM a top choice for many system administrators? Here are its most prominent benefits:

  • Flexible Space Management: You can easily extend or shrink partitions (LVs) without needing to reboot the system or complex data migration. This is extremely useful when storage needs constantly change.
  • Combine Multiple Drives: LVM allows you to pool space from multiple small physical drives into a single large VG. From this VG, you can divide it into LVs as needed, optimizing resource utilization.
  • Instant Snapshots: LVM provides the ability to create “snapshots” of an LV. This is an incredibly valuable feature for instant data backups or testing system changes without fear of corrupting original data.

Advanced: LVM Extension, Shrinkage, and Management

Now that you’ve grasped the basic concepts, let’s delve into the important management tasks that LVM offers.

Extending a Logical Volume (LV)

This is the task I use most often in practice. Suppose your lv_storage is almost full, and you want to extend it by another 5GB. First, check if your VG (vg_data) has enough free space:


sudo vgs

If there’s enough space, you can extend the LV as follows:


sudo lvextend -L +5G /dev/mapper/vg_data-lv_storage

After extending the LV, you need to extend the filesystem within it to utilize the entire new space. For an ext4 filesystem, use the command:


sudo resize2fs /dev/mapper/vg_data-lv_storage

If using XFS (common on CentOS/RHEL 7+), the command will be:


sudo xfs_growfs /mnt/data # Or the mount point directory of that LV

Finally, check the results to ensure the changes have been applied:


df -h /mnt/data

Shrinking a Logical Volume (LV)

Shrinking an LV is a much more complex and riskier operation than extending. Therefore, you ABSOLUTELY must back up all data before proceeding. You need to shrink the filesystem first, and only then shrink the LV.


# 1. Unmount the Logical Volume to ensure data safety
sudo umount /mnt/data

# 2. Check for filesystem errors (this step is extremely important!)
sudo e2fsck -f /dev/mapper/vg_data-lv_storage

# 3. Shrink the filesystem (e.g., down to 8G). Ensure this size is NOT smaller than the existing data.
sudo resize2fs /dev/mapper/vg_data-lv_storage 8G

# 4. Shrink the Logical Volume. The size must match or be larger than the shrunk filesystem size.
sudo lvreduce -L 8G /dev/mapper/vg_data-lv_storage

# 5. Remount and check the capacity after shrinking
sudo mount /dev/mapper/vg_data-lv_storage /mnt/data
df -h /mnt/data

Important Note: For XFS filesystems, direct shrinking is NOT supported. In this case, you are forced to back up your data, recreate the LV with a smaller size, and then restore the backed-up data.

Adding a Physical Volume (PV) to a Volume Group (VG)

When your current VG runs out of space but you have a new disk (e.g., /dev/sdc1), you can easily extend the VG by adding the new PV:


# 1. Create a new Physical Volume from the disk /dev/sdc1
sudo pvcreate /dev/sdc1

# 2. Add this PV to the existing Volume Group (vg_data)
sudo vgextend vg_data /dev/sdc1

# 3. Check the VG space to confirm the change
sudo vgs

Now, your vg_data VG has additional space from /dev/sdc1. You can use this capacity to extend existing LVs or create new LVs.

Creating and Managing LVM Snapshots

An LVM snapshot is a point-in-time copy of an original LV. It only stores changes that occur since the snapshot was created, saving disk space. This is an extremely useful feature for data backup or testing system changes.

To create a snapshot of lv_storage, with a size of 1GB (allowing a maximum of 1GB changes on the original LV before the snapshot becomes full):


sudo lvcreate --size 1G --snapshot --name lv_storage_snap /dev/mapper/vg_data-lv_storage

You can mount this snapshot to access data at the time the snapshot was created:


sudo mkdir /mnt/snapshot
sudo mount /dev/mapper/vg_data-lv_storage_snap /mnt/snapshot

To restore (rollback) the original LV to the state of the snapshot:


# Unmount both the original LV and the snapshot before restoring
sudo umount /mnt/data
sudo umount /mnt/snapshot

# Perform the restore
sudo lvconvert --merge /dev/mapper/vg_data-lv_storage_snap

# Remount the original LV after successful restoration
sudo mount /dev/mapper/vg_data-lv_storage /mnt/data

When the snapshot is no longer needed, you can delete it:


sudo lvremove /dev/mapper/vg_data-lv_storage_snap

Practical Tips and Personal Experience

Checking and Managing LVM

There are always commands to help you quickly check the status of LVM:

  • pvs: Displays summary information about Physical Volumes.
  • vgs: Displays summary information about Volume Groups.
  • lvs: Displays summary information about Logical Volumes.
  • For more detailed information, you can use pvdisplay, vgdisplay, lvdisplay, respectively.

sudo pvs
sudo vgs
sudo lvs

When to Use LVM?

In my experience, LVM is particularly useful in the following situations:

  • Development/Testing Servers: Where disk space often needs to be resized without interrupting work.
  • Services with Large Logs: For example, a web server with high traffic will generate log files very quickly. LVM helps you easily extend the /var/log partition without worrying about running out of space.
  • Databases: When database size can grow unpredictably, LVM is an ideal solution for managing capacity.
  • Homelab/Personal VPS: To make the most of small drives and easily change storage configurations as desired.

Caution when shrinking

I want to emphasize this once again: always back up your data thoroughly before shrinking any Logical Volume! A small mistake in this process can lead to permanent, unrecoverable data loss. Never neglect this step. Take the time to read the documentation carefully and understand each step before applying it in a real production environment.

I hope that through this article, you have gained an overview and more confidence in managing LVM on your Linux system. LVM offers high efficiency; when used correctly, it will save you a lot of time and effort in managing storage space and optimizing your system.

Share: