Sitemap
Newton

Newton

Linux LVM (Logical Volume Manager)

5 min readJul 5, 2022

Introduction to the Logical Volume Manager (LVM)

LVM stands for Logical Volume Manager. The logical volume manager introduces extra layers of abstraction between the disks or storage devices presented to a Linux system and the file systems placed on those disks or storage devices.

Before we get into those layers of abstraction and how to use them, let’s talk about why you would want to use LVM in the first place.

Why use LVM?

Flexible Capacity

One benefit of using LVM is that you can create file systems that extend across multiple storage devices. With LVM, you can aggregate multiple storage devices into a single logical volume.

Easily Resize Storage While Online

LVM allows you to expand or shrink filesystems in real-time while the data remains online and fully accessible. Without LVM you would have to reformat and repartition the underlying storage devices. Of course, you would have to take the file system offline to perform that work. LVM eliminates this problem.

Online Data Relocation

LVM also allows you to easily migrate data from one storage device to another while online. For example, if you want to deploy newer, faster, or more resilient storage, you can move your existing data from the current storage devices to the new ones while your system is active.

Convenient Device Naming

Instead of using abstract disk numbers, you can use human-readable device names of your choosing. Instead of wondering what data is on /dev/sdb, you can name your data with a descriptive name.

Disk Striping

With LVM, you can stripe data across two or more disks. This can dramatically increase throughput by allowing your system to read data in parallel.

Data Redundancy / Data Mirroring

If you want to increase fault tolerance and reliability, use LVM to mirror your data so that you always have more than one copy of your data. Using LVM mirroring prevents single points of failure. If one storage device fails, your data can be accessed via another storage device. You can then fix or replace the failed storage device to restore your mirror, without downtime.

Snapshots

LVM gives you the ability to create point-in-time snapshots of your filesystems. This can be perfect for when you need consistent backups. For example, you could pause writes to a database, take a snapshot of the logical volume where the database data resides, then resume writes to the database. That way you ensure your data is in a known-good state when you perform the backup of the snapshot.

Layers of Abstraction in LVM

As I mentioned earlier, the logical volume manager introduces extra layers of abstraction between the storage devices and the file systems placed on those storage devices.

Press enter or click to view image in full size
LVM STRUCTURE

The first layer of abstraction is physical volumes. These are storage devices that are used by LVM. The name is a bit of a legacy name. To be clear, these storage devices do not have to be physical. They just have to be made available to the Linux operating system. In other words, as long as Linux sees the device as a block storage device, it can be used as a physical volume (PV). Physical hard drives, iSCSI devices, SAN disks, and so on can be PVs. You can allocate an entire storage device as a PV or you can partition a storage device and use just that one partition as a PV. The next layer of abstraction is the Volume Group. A volume group is made up of one or more PVs. You can think of a volume group as a pool of storage. If you want to increase the size of the pool, you simply add more PVs. Keep in mind that you can have different types of storage in the same volume group if you want. For example, you could have some PVs that are backed by hard drives and other PVs that are backed by san disks. We’ll talk about how this can come in handy when performing data migrations later (in this lesson). The next layer of abstraction is the Logical Volume layer. Logical Volumes are created from a volume group. File systems are created on Logical Volumes. Without LVM you would create a file system on a disk partition, but with LVM you create a file system on a logical volume. As long as there is free space in the Volume Group, logical volumes can be extended. You can also shrink logical volumes to reclaim unused space if you want, but typically you’ll find yourself extending logical volumes.

Get Newton’s stories in your inbox

Join Medium for free to get updates from this writer.

Logical Volume Creation Process

At a high level, the process for creating a logical volume is this:

  1. Create one or more physical volumes.
  2. Create a volume group from those one or more physical volumes.
  3. Finally, you can create one or more logical volumes from the volume group.

Creating Physical Volumes

Because we are going to be working on system administration type of tasks that will require root privileges, I’m going to switch to the root user first.

su -

Before you can create a physical volume, you need to know what storage devices are available. The easiest way to do that is with the lvmdiskscan command:

lvmdiskscan

This command shows you all the storage devices that have the ability to be used with LVM. At least one of these storage devices is currently being used to host the root filesystem, but the output we see here doesn’t tell us which one that is. We could guess, but let’s don’t do that. Let’s find out some more information about our block devices with the “lsblk” command.

lsblk

Here you can see that /dev/sda1 is actually mounted on /. It contains the root filesystem. If you want to display the full paths to the devices shown with lsblk, use the -p option:

lsblk -p

Know it’s clear that sda1 is really /dev/sda1. We can further confirm by using the df command which shows disk space usage. I like using the -h option to force df to display sizes in a human readable format.

df -h

So, lvmdiskscan is a great way to see what disks are available. You can also use the fdisk -l command to see the storage attached to your Linux system.

fdisk -l

I prefer lvmdiskscan, but again, there are multiple ways to view the attached storage devices.

lvmdiskscan

To create a PV, a physical volume, we use the pvcreate command and give it the path to the storage device.

pvcreate /dev/sdb

The pvcreate command initializes the disk for use by the logical volume manager. You can view the list of pvs with the pvs command.

pvs

--

--