Basics of LVM: Concepts, Terminology, and Applications

Introduction

LVM, or Logical Volume Management, is a technology for managing storage devices, allowing users to pool and abstract the physical structure of component storage devices for flexible management. Using the Device Mapper Linux kernel framework, the current version, LVM2, can combine existing storage devices into groups and allocate logical units from the combined storage space. The main advantages of LVM are increased abstraction, flexibility, and control. Logical volumes can have meaningful names like “Databases” or “Root Backup.” Volumes can also be dynamically resized as space requirements change and can be migrated or exported between physical devices within the pool on a running system. LVM also offers advanced features like snapshotting, striping, and mirroring. In this guide, you’ll learn how LVM works and practice basic commands to quickly get up and running on a bare-metal machine.

LVM Architecture and Terminology

Before delving into LVM management commands, it’s important to have a basic understanding of how LVM organizes storage devices and the terminology it uses.

LVM Storage Management Structures

LVM works by overlaying abstractions on physical storage devices. The basic layers used by LVM, starting with the most primitive, are:

  • Physical Volumes: The LVM utility prefix for physical volumes is “pv…”. These are physical block devices or other disk-like devices (e.g., other devices created by Device Mapper like RAID arrays) and are used by LVM as raw material for higher abstraction levels. Physical volumes are regular storage devices. LVM writes a header to the device to allocate it for management.
  • Volume Groups: The LVM utility prefix for volume groups is “vg…”. LVM combines physical volumes into storage pools called volume groups. Volume groups abstract the properties of the underlying devices and act as a unified logical device with the combined storage capacity of the component physical volumes.
  • Logical Volumes: The LVM utility prefix for logical volumes is “lv…”. A volume group can be divided into any number of logical volumes. Logical volumes are functionally equivalent to partitions on a physical hard drive but offer much more flexibility. Logical volumes are the main component that users and applications interact with. LVM can be used to combine physical volumes into volume groups to unify the available storage space on a system. Administrators can then subdivide the volume group into any number of logical volumes, which act as flexible partitions.

Understanding Extents

Each volume within a volume group is divided into small, fixed-size chunks called extents. The size of the extents is determined by the volume group. All volumes within the group correspond to the same extent size. Extents on a physical volume are referred to as physical extents, while the extents of a logical volume are referred to as logical extents. A logical volume is a mapping that LVM maintains between logical and physical extents. Because of this relationship, the extent size represents the smallest amount of space that can be allocated by LVM. Extents are an essential part of the flexibility and performance of LVM. The logical extents represented by LVM as a unified device do not need to correspond to continuous physical extents. LVM can copy and reorganize the physical extents comprising a logical volume without interrupting users. Logical volumes can also be expanded or shrunk by adding or removing extents from the volume.

Common Use Cases

Now that you’re familiar with some terminology and the structures used by LVM, you can explore some common ways to use LVM. You’ll start with a procedure involving two physical hard drives to create four logical volumes.

Marking the physical devices as physical volumes

Begin by scanning the system for block devices that LVM can access and manage. You can do this with the following command:

The output lists all available block devices that LVM can access. In this example, there are currently two hard drives and 17 partitions. The partitions are mostly /dev/ram* partitions used in the system as RAM disks for performance enhancement. The hard drives in this example are /dev/sda with 200G of storage space and /dev/sdb with 100G.
Warning: Make sure the devices you intend to use with LVM do not already have important data on them. Using these devices in LVM will overwrite their current contents. If you have important data on your server, make backups before proceeding. Now that you know the physical devices you want to use, mark them as physical volumes in LVM with the pvcreate command:

sudo pvcreate /dev/sda /dev/sdb

This writes an LVM header to the devices to indicate that they are ready to be added to a volume group. Check if LVM has registered the physical volumes by running pvs:

The output shows that both devices are present under the “PV” (physical volumes) column.

Adding the physical volumes to a volume group

Now that you’ve created physical volumes from your devices, you can create a volume group. Typically, you’ll have only one volume group per system to ensure maximum flexibility in allocation. The following example of a volume group is named “LVMVolGroup.” You can give your volume group any desired name. To create the volume group and add both physical volumes, execute:

sudo vgcreate LVMVolGroup /dev/sda /dev/sdb

The output shows that your physical volumes are now associated with the new volume group.

List a brief summary of the volume group with vgs:

The volume group currently has two physical volumes, no logical volumes, and the combined capacity of the underlying devices.

Creating logical volumes from the volume group pool

Now that you have a volume group available, you can use it as a pool to allocate logical volumes. Unlike traditional partitioning, when working with logical volumes, you don’t need to know the volume layout as LVM maps and manages it for you. You only need to specify the volume size and a name. In the following example, you’ll create four separate logical volumes from your volume group:
– 10G “projects” volume
– 5G “www” volume for web content
– 20G “db” volume for a database
– “workspace” volume filling the remaining space
To create logical volumes, use the lvcreate command. You need to specify the volume group from which to draw and can name the logical volume using the -n option. To specify the size directly, you can use the -L option. If you prefer to specify the size in terms of extents, you can use the -l option. Create the first three logical volumes using the -L option:

sudo lvcreate -L 10G -n projects LVMVolGroup
sudo lvcreate -L 5G -n www LVMVolGroup
sudo lvcreate -L 20G -n db LVMVolGroup

The output indicates that the logical volumes have been created.

List a brief summary of the volume group with vgs:

The output shows how much space has been allocated to your logical volumes.

Now you can assign the remaining space in the volume group to the “workspace” volume using the -l option, which works in extents. You can also specify a percentage and unit to better communicate your intentions. In this example, you’re assigning the remaining free space by passing 100%FREE:

sudo lvcreate -l 100%FREE -n workspace LVMVolGroup

The output shows that the “workspace” volume has been created, and the volume group “LVMVolGroup” is fully allocated.

Formatting and Mounting the Logical Volumes

Now that you have logical volumes, you can use them like regular block devices. The logical devices are available in the /dev directory like other storage devices. You can access them in two locations:

  1. /dev/volume_group_name/logical_volume_name
  2. /dev/mapper/volume_group_name-logical_volume_name

To format your four logical volumes with the Ext4 filesystem, execute the following commands:

sudo mkfs.ext4 /dev/LVMVolGroup/projects
sudo mkfs.ext4 /dev/LVMVolGroup/www
sudo mkfs.ext4 /dev/LVMVolGroup/db
sudo mkfs.ext4 /dev/LVMVolGroup/workspace

Alternatively, you can run:

sudo mkfs.ext4 /dev/mapper/LVMVolGroup-projects
sudo mkfs.ext4 /dev/mapper/LVMVolGroup-www
sudo mkfs.ext4 /dev/mapper/LVMVolGroup-db
sudo mkfs.ext4 /dev/mapper/LVMVolGroup-workspace

After formatting, create mount points:


sudo mkdir -p /mnt/{projects,www,db,workspace}

Then, mount the logical volumes at their respective locations:

sudo mount /dev/LVMVolGroup/projects /mnt/projects
sudo mount /dev/LVMVolGroup/www /mnt/www
sudo mount /dev/LVMVolGroup/db /mnt/db
sudo mount /dev/LVMVolGroup/workspace /mnt/workspace

To make the mounts persistent, add them to the /etc/fstab file with your preferred text editor. The following example uses nano:

Insert your entries into the file and save it. The operating system should now automatically mount the LVM logical volumes during boot.

Conclusion

You now have an understanding of the various components that LVM manages to create a flexible storage system and how to prepare storage devices in an LVM configuration. To learn more about working with LVM, check out our guide on using LVM with Ubuntu 18.04.

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: