Logical Volume Management (LVM) in Linux is surprisingly flexible, allowing you to treat storage devices as a pool of resources you can carve up and reconfigure on the fly, even while your system is running.

Let’s see LVM in action. Imagine you have two physical disks, /dev/sda and /dev/sdb, and you want to create a single, large storage volume from them that you can later expand.

First, we need to make sure our disks are ready. This usually means partitioning them with a type that LVM understands. We’ll use fdisk for this, but parted is another option.

sudo fdisk /dev/sda
# In fdisk:
# n (new partition)
# p (primary)
# 1 (partition number)
# Enter (default first sector)
# Enter (default last sector)
# t (change partition type)
# 8e (Linux LVM)
# w (write changes and exit)

sudo fdisk /dev/sdb
# Repeat the same steps for /dev/sdb

Now, we create Physical Volumes (PVs) from these partitions. A PV is a block device that LVM can use.

sudo pvcreate /dev/sda1
sudo pvcreate /dev/sdb1

Next, we group these PVs into a Volume Group (VG). Think of a VG as a pool of storage.

sudo vgcreate my_volume_group /dev/sda1 /dev/sdb1

With our volume group created, we can now carve out Logical Volumes (LVs) from it. An LV is what you’ll actually format and mount like a regular partition. Let’s create a 50GB LV named my_logical_volume within my_volume_group.

sudo lvcreate -L 50G -n my_logical_volume my_volume_group

Now, we format this LV and mount it. We’ll use ext4 here.

sudo mkfs.ext4 /dev/my_volume_group/my_logical_volume
sudo mkdir /mnt/my_data
sudo mount /dev/my_volume_group/my_logical_volume /mnt/my_data

You can check the status of your LVM setup with sudo pvs, sudo vgs, and sudo lvs. pvs shows physical volumes, vgs shows volume groups, and lvs shows logical volumes.

The real power comes when you need more space. Let’s say you add another disk, /dev/sdc, partition it as /dev/sdc1 with type 8e, and create a PV from it.

sudo pvcreate /dev/sdc1

Now, you extend your existing volume group with this new PV.

sudo vgextend my_volume_group /dev/sdc1

To expand your logical volume, you can either specify an exact size or use a percentage of the free space in the volume group. Let’s add 20GB to our existing LV.

sudo lvextend -L +20G /dev/my_volume_group/my_logical_volume

After resizing the LV, you need to tell the filesystem to use that new space. For ext4, this is done with resize2fs.

sudo resize2fs /dev/my_volume_group/my_logical_volume

Your /mnt/my_data mount point will now show the increased size.

One of the less obvious benefits of LVM is its snapshotting capability. You can create a point-in-time copy of a logical volume, which is incredibly useful for backups or before making risky changes. For example, to create a snapshot named my_snapshot of my_logical_volume with a maximum size of 10GB:

sudo lvcreate --size 10G --snapshot --name my_snapshot /dev/my_volume_group/my_logical_volume

This snapshot is a writable copy, but it only stores the differences from the original. As the original LV changes, the snapshot grows to accommodate those changes, up to its defined maximum size. If the snapshot reaches its maximum size, it becomes inconsistent and unusable.

The next step in mastering LVM is often understanding how to move logical volumes between physical volumes or how to set up thin provisioning for more efficient storage utilization.

Want structured learning?

Take the full Linux & Systems Programming course →