Using LVM in Linux to manage storage devices

Posted on by Luke Cavanagh | Updated:
Reading Time: 10 minutes

Logical volume management (LVM) is a powerful storage administration framework that abstracts the physical layout of storage devices by introducing logical volumes. Taking advantage of the Linux device mapper kernel driver, LVM offers a software version of a storage area network (SAN), allowing for more efficient and flexible storage utilization and management through dynamic resizing and storage allocation.

Its high versatility and ease of use make logical volume management an indispensable storage management solution for mission-critical systems. In this article, you will learn what LVM in Linux is and how to leverage it to optimize storage efficiency and adapt to the ever-changing storage needs of your business.

Key points

After reading this article, you will have a better grasp of the following topics and tasks:

  • Knowing what logical volume management (LVM) in Linux is, including its primary components of physical volumes (PVs), volume groups (VGs), ad logical volumes (LVs).
  • Setting up LVM in Linux, including the detailed steps involved.
  • Resizing logical volumes with LVM in Linux.
  • Removing logical volumes with LVM in Linux.
  • Taking advantage of LVM snapshots.
  • Exploring the power of logical volume management with Liquid Web.

What is LVM in Linux?

LVM, which stands for logical volume management, is a robust storage management technology that allows you to virtualize storage devices to pool and allocate storage resources dynamically. Using Linux local volume management, system administrators can overcome most limitations of traditional storage management. This includes rebooting the server to resize filesystems and add additional storage space to the server.

With Linux logical volume management, an administrator can aggregate available storage resources into groups and allocate them into logical volumes. Logical volumes will then be formatted with file systems and used to store data just as if they were partitions of a physical hard drive. However, while you can not easily resize a partition or add additional physical storage to your system, you can dynamically resize a logical volume without causing any significant downtime to your system or disrupting any services. The use of LVM increases the reliability of your system and the overall server security.

Three core components of LVM in Linux

Linux logical volume management encompasses three main components, which are physical volumes, volume groups, and logical volumes. Before setting up logical volume management on your Linux server, let’s take a closer look at each of the three components of an LVM implementation.

Physical volumes (PVs)

Physical volumes (PVs) are physical or virtual storage devices that provide storage resources that will be accumulated into a shared pool. Physical volumes can be added to logical volume management in the form of hard drives, RAID devices, or partitions.

Please note that only unused devices or partitions can be added as physical volumes. If a hard disk or partition is already in use by a mount point, you will not be able to add it to LVM without causing a data loss.

Once added as physical volumes, all devices and partitions are treated equally, regardless of how they are configured and recognized by the operating system. In Linux, LVM abstracts the physical layout of storage devices under its management, providing the system administrator with available pools of storage resources, known as volume groups.

Volume groups (VGs)

Volume groups (VGs) consolidate available physical volumes into pools of storage resources, also referred to as buckets of storage. LVM in Linux allows for the implementation of multiple volume groups, each of which usually serves a specific purpose, providing storage resources for different system components. As volume groups act as containers for physical or virtual storage devices, you can move physical volumes between various groups.

For example, the hypothetical vg-home volume group may include three 100 GB hard drives, each added as a physical volume (PV). Even if the disks are virtual, they will still be referred to as physical volumes in the context of LVM in Linux.

Logical volumes (LVs)

As a central component of LVM in Linux, logical volumes (LVs) are created from pools of storage resources accumulated by volume groups. Once created, a logical volume is formatted with a file system and attached to the file system hierarchy to store data.

Logical volumes are considered similar in concept to hard drive partitions. A logical volume can take up a portion of a disk, a whole disk, or even span multiple disks, while your users will still see it as a single storage area. Once mounted, users can store files on a logical volume just as they would a partition on a standard disk.

However, unlike partitions that can not easily be resized, logical volumes support dynamic resizing. Linux logical volume management allows you to allocate more storage to a logical volume at any time without having to unmount it or causing any loss of data.

Setting up logical volume management in Linux

Setting up logical volume management on your Linux system includes adding storage devices to your LVM implementation as physical volumes (PVs), combining storage resources into pools using volume groups (VGs), and allocating the consolidated resources to logical volumes (LV) to meet the storage requirements of the target system.

Important prerequisites for using LVM in Linux

There some essential setup prerequisites to have established before getting started with LVM:

  • Root, or administrative, access to your Linux server.
  • At least one unused storage volume. This can be a partition of a hard drive that has not been yet been formatted with a file system or mounted, a RAID array, or an unpartitioned hard drive.
  • The lvm2 software package installed. Although most systems will have lvm2 present by default, it is still recommended that you check whether it’s installed on your server using the package management tool supported by your operating system.

Step #1. Adding physical volumes (PVs)

The first step toward implementing LVM is taking an inventory of all available storage devices. You can use the fdisk -l or df -h commands to list all disks attached to your Linux server.

In the example below, we have added three new volumes to our system, so now we have available /dev/sdb, /dev/sdc, and /dev/sdd to use in the context of our implementation of LVM in Linux. Please note that the names of your disks will vary depending on the hardware or virtualization platform.

To configure the new disks to be recognized by Linux logical volume management, you must add each as a physical volume. The pvcreate command allows you to configure storage devices as new physical volumes. You can configure each PV separately or supply all storage volumes you wish to set up as a physical volume to the pvcreate command at once:

# pvcreate /dev/sdb /dev/sdc /dev/sdd
Physical volume "/dev/sdb" successfully created.
Physical volume "/dev/sdc" successfully created.
Physical volume "/dev/sdd" successfully created.

After new storage volumes have been added as LVM physical volumes, you can execute the pvdisplay command to display all physical volumes recognized by Linux logical volume management. Each physical volume will be displayed similarly to what you can see in the output below. Please note that newly created physical volumes are not yet associated with any volume groups:

# pvdisplay
"/dev/sdb" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name               /dev/sdb
VG Name
PV Size               10.00 GiB
Allocatable           NO
PE Size               0
Total PE              0
Allocated PE          0

Step #2. Defining and resizing volume groups (VGs)

After new physical volumes have been added to logical volume management, you can create a new volume group that will accumulate the available storage resources into a pool. The vgcreate command, followed by the name of the new volume group and the list of physical volumes you wish to assign to it, is used to add volume groups to LVM in Linux. Typically, you would prefix volume group names with vg. However, you may choose not to follow this naming convention:

# vgcreate vg-main /dev/sdb /dev/sdc /dev/sdd
Volume group "vg-main" successfully created

Using the vgdisplay and vgs commands allows you to view all volume groups created in your implementation of Linux logical volume management. The vgdisplay command offers a more detailed overview of volume groups. Metadata Areas and Act PV will show the number of physical volumes assigned to the volume group. VG Size shows you the total amount of storage resources the volume group provides:

# vgdisplay
--- Volume group ---
VG Name               vg-main
System ID
Format                lvm2
Metadata Areas        3
Metadata Sequence No  1
VG Access             read/write
VG Status             resizable
Max LV                0
Cur LV                0
Cur PV                3
Act PV                3
VG Size               29.99 GiB
PE Size               4.00 MiB
Total PE              7678
Allocated PE / Size   0
Free PE / Size        7678 / 29.99 GiB

If, at any point in time, you would like to extend the volume group you created by assigning more physical volumes to it, you can use the vgextend command to do it:

# vgextend vg-main /dev/sde
Volume group "vg-main" successfully extended

Step #3. Creating logical volumes (LVs)

Once you have created a volume group, you can allocate the storage resources available to new logical volumes using the pvcreate command. The -n flag will specify the name of the new logical volume, while the value following the -L flag will define how much storage space will be allocated to it. Don't forget to supply the name of the volume group you wish to take the storage space from for the new logical volume:

# lvcreate -n home -L 25G vg-main
Logical volume "home" created

You can use the lvdisplay command to see more information about the logical volumes you have created:

# lvdisplay
--- Logical volume ---
LV Path                 /dev/vg-main/home
LV Name                 home
VG Name                 vg-main
LV UUID                 yOR0c1-CX6d-a55T-Wktp-Pbtf-zcfMKv
LV Write Access         read/write
LV Creation host, time  my-server, 2024-01-03 01:20:58 +0000
LV Status               available
# open                  0
LV Size                 25.00 GiB
Current LE              6400
Segments                0
Allocation              Inherit

Step #4. Formatting logical volumes (LVs)

To use the new logical volume, you need to format it with a filesystem as you would a partition of a hard disk. You will need to know the name of the device representing the logical volume, which is specified by LV Path. In the example below, we format our new home logical volume with the ext4 filesystem:

# mkfs.ext4 /dev/vg-main/home

Step #5. Mounting logical volumes (LVs)

After the logical volume has been formatted with a filesystem, it can be mounted as any other storage device. You can mount a logical volume using the mount command. To configure the device to be mounted on boot, add a new entry to the /etc/fstab file:

# mount -t ext4 /dev/vg-main/home /mnt/home

Resizing logical volumes with LVM in Linux

One of the most prominent benefits of using LVM in Linux is the ability to dynamically resize logical volumes. Resizing a logical volume includes two steps:

  1. Allocating more space to the LV or decreasing its size.
  2. Resizing the file system it has been formatted with.

Please note that the possibility of having a logical volume resized while it's mounted and in use depends on multiple factors, including the file system it has been formatted with. Most major Linux file systems, including ext4 and xfs, can be extended online, although most do require to be unmounted to be shrunk. Furthermore, it is of the utmost importance to always take a backup of your data before making any changes to it, including resizing logical volumes it's stored on.

The lvextend command allows you to allocate more storage space to a logical volume. The -L flag can be used to specify the new size of the logical volume you wish to extend. Using the -r argument enables you to perform a resize of the file system the PV has been formatted with. However, it is often recommended to resize the file system separately from the logical volume it resides on.

In the example below, we are first resizing the home logical volume to 28 gigabytes. Then, using the 100%FREE option, we are allowing the logical volume to take up the entirety of the free storage space the vg-main group offers. Using the resize2fs command allows us to resize the ext4 filesystem it has been formatted with according to the new amount of storage space allocated to the logical volume:

# lvextend -L 28G -n /dev/vg-main/home 
Logical volume vg-main/home successfully resized

# lvextend -l +100%FREE -n /dev/vg-main/home
Logical volume vg-main/home successfully resized

# resize2fs /dev/vg-main/home 

Removing logical volumes with LVM in Linux

Removing a logical volume completely destroys it, moving the storage space that was allocated to it back to the resource pool managed by the volume group. Please note that if any data is stored on the logical volume, it will be lost once the volume has been removed. If the logical volume is in use, move all data to another storage volume and unmount it prior to deleting it:

# lvremove /dev/vg-main/home
Do you really want to remove active logical volume vg-main/home? [y/n]: y
Logical volume vg-main/home successfully removed

Taking advantage of LVM snapshots

LVM in Linux supports the use of snapshots that enable you to capture a logical volume at a specific point in time and preserve it. Having a snapshot of a logical volume allows you to revert it to the state it was in when the snapshot was taken.

An LVM snapshot is created in the form of a new logical volume that mirrors the original logical volume. Initially, no space is occupied by the snapshot. However, as server operations involving the manipulation of the files stored on the logical volume are performed, the original storage blocks are copied to the snapshot as you change them to preserve the original state of the LV.

It is important to note that as the snapshot is created in the form of another logical volume, it will occupy storage space on the volume group. However, the total amount of storage space allocated to the snapshot is determined at the time of its creation. If the snapshot fills up the allocated storage space, you will not be able to revert any changes that will be made to the original logical volume after that.

LVM snapshots can not be considered a viable form of backups. Instead, snapshots can be used as a temporary holding area for situations when you want to make some potentially risky changes to the files stored within the original logical volume or test how certain updates will affect your system. Once the changes have been implemented, you should remove the snapshot or merge it.

Creating an LVM snapshot

A snapshot of a logical volume can be created using the lvcreate command. The -s flag will tell LVM in Linux that the new LV is a snapshot of an existing one. The -n flag allows you to supply a name for the snapshot. The -L argument is used to designate a maximum size for the snapshot.

To create a snapshot of the home logical volume named snapshot use the following command:

# lvcreate -s -n snapshot -L 5G /dev/vg-main/home

Restoring from an LVM snapshot

LVM snapshots enable you to restore specific files or folders within the original logical volume or revert any changes made since the snapshot's creation.

As the snapshot you create is a new logical volume, you can mount it as you would any other logical volume. This aspect of how snapshots work can be useful if you would like to retrieve certain files without having to restore all data on the logical volume.

However, if you decide to restore the whole logical volume to the state it was in when the snapshot was created, you can use the lvconvert command to do it. The snapshot will be removed once it has been merged:

# lvconvert --merge vg-main/snapshot
Merging of volume snapshot started.
home: Merged: 100.0%

It’s important to note, that, unlike being able to dynamically resize a logical volume, you can not merge a snapshot while the original logical volume is in use. To merge a snapshot, you must either unmount the original logical volume before merging and remount it after it has been done or unmount and remount it after merging. Furthermore, if the snapshot is of the root file system, you will need to reboot the server for the merge to complete.

Removing LVM snapshots

If you choose not to restore the logical volume to the state it was in when the snapshot was taken by merging the snapshot, you have the option to permanently delete the snapshot using the lvremove command. Please note that removing the snapshot will finalize all changes that were made to the original logical volume since the snapshot was taken. Once a snapshot has been removed using the lvremove command, you won't be able to recover it.

# lvremove vg-main/snapshot 

Explore the power of logical volume management with Liquid Web

Linux logical volume management (LVM) offers a robust framework for storage administration by introducing logical volumes. Using LVM in Linux enables a more flexible and efficient approach to the utilization of storage resources by introducing the snapshot functionality, dynamic resizing, storage allocation on demand, and simplified storage management. With all the spectacular benefits provided by LVM described above, it is indispensable tool in optimizing storage efficiency and securing your website hosted on a Linux-based server.

Liquid Web dedicated server hosting plans and cloud hosting solutions are designed to meet the ever-growing needs of mission-critical businesses, offering unrivaled performance, reliability, and exceptional support. Whether you opt for a dedicated server for maximum control over your server environment or choose our cloud hosting solutions for flexibility, Liquid Web ensures a seamless hosting experience with a focus on security and innovation.

Avatar for Luke Cavanagh

About the Author: Luke Cavanagh

Product Operations Manager at Liquid Web. Devoted husband and Tween wrangler. Synthwave enthusiast. Jerry Goldsmith fan. Doctor Who fan and related gubbins.

Latest Articles

How to use kill commands in Linux

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change the root password in WebHost Manager (WHM)

Read Article