This content originally appeared on DEV Community and was authored by Aviral Srivastava
Logical Volume Management (LVM): Flexible Disk Space Management
Introduction
Logical Volume Management (LVM) is a powerful tool that provides a layer of abstraction over physical storage devices, enabling administrators to create, resize, and manage logical volumes as if they were single, contiguous hard drives, even though they might be spread across multiple physical disks. In essence, LVM transforms the limitations of traditional partitioning into a flexible and scalable storage management system. It allows you to pool physical storage capacity, allocate it to logical volumes as needed, and resize those volumes without downtime (in most cases), which greatly simplifies storage administration.
Imagine you have three physical disks of varying sizes and want to create a single large volume for your database. Without LVM, you’d need to partition the disks individually and configure a RAID array, which can be complex and inflexible. With LVM, you can easily combine these physical disks into a single volume group and then carve out a logical volume for your database, which can be resized as your data grows.
LVM is commonly used in server environments, particularly for databases, virtual machines, and other applications that require flexible and scalable storage. Its ability to adapt to changing storage needs makes it an indispensable tool for system administrators.
Prerequisites
Before diving into LVM, ensure you have the following:
- Understanding of Basic Disk Concepts: Familiarity with concepts like physical disks, partitions, and file systems is essential.
-
Root Access or
sudo
Privileges: Most LVM commands require elevated privileges. - Unpartitioned Disk Space: LVM requires unused disk space or partitions to create Physical Volumes (PVs). Important: Back up any data on these disks before proceeding, as creating PVs will erase the existing data.
- LVM Packages Installed: Most Linux distributions have LVM packages readily available. If not installed, you can use your distribution’s package manager to install them. For example, on Debian/Ubuntu:
sudo apt update
sudo apt install lvm2
And on CentOS/RHEL/Fedora:
sudo yum update
sudo yum install lvm2
LVM Architecture and Components
LVM architecture consists of three key components:
Physical Volumes (PVs): PVs are the foundation of LVM. They represent the actual physical storage devices or partitions that LVM uses. A PV is created on a physical disk or partition using the
pvcreate
command.Volume Groups (VGs): VGs act as containers for PVs. One or more PVs are combined into a VG, creating a pool of storage space. This pool can then be divided into logical volumes. A VG is created using the
vgcreate
command.Logical Volumes (LVs): LVs are the virtual partitions that the operating system recognizes. They are carved out from the VG’s storage pool. LVs behave like traditional partitions and can be formatted with a file system (e.g., ext4, XFS) and mounted. LVs are created using the
lvcreate
command.
Basic LVM Commands and Usage
Here’s a step-by-step example of how to create an LVM setup:
Identify Available Disks/Partitions: Use
fdisk -l
orlsblk
to identify the disks or partitions you want to use for LVM. Let’s assume you have two disks:/dev/sdb
and/dev/sdc
.-
Create Physical Volumes (PVs):
sudo pvcreate /dev/sdb sudo pvcreate /dev/sdc
-
Verify PV Creation:
sudo pvdisplay # Displays detailed information about PVs sudo pvs # Displays a summary of PVs
-
Create a Volume Group (VG):
sudo vgcreate myvg /dev/sdb /dev/sdc
Here,
myvg
is the name of the volume group. -
Verify VG Creation:
sudo vgdisplay myvg # Displays detailed information about myvg sudo vgs # Displays a summary of VGs
-
Create a Logical Volume (LV):
sudo lvcreate -n mylv -L 20G myvg
This creates a logical volume named
mylv
with a size of 20GB within themyvg
volume group. The-n
option specifies the name, and-L
specifies the size. -
Verify LV Creation:
sudo lvdisplay /dev/myvg/mylv # Displays detailed information about mylv sudo lvs # Displays a summary of LVs
-
Format the LV:
sudo mkfs.ext4 /dev/myvg/mylv
This formats the logical volume with the ext4 file system. You can choose other file systems like XFS.
-
Mount the LV:
sudo mkdir /mnt/mylv sudo mount /dev/myvg/mylv /mnt/mylv
-
Make the Mount Permanent (Optional): Add an entry to
/etc/fstab
to automatically mount the LV on boot. First, get the UUID of the LV:
sudo blkid /dev/myvg/mylv
Then, edit
/etc/fstab
and add a line like this (replace with your actual UUID):
UUID=<YOUR_UUID> /mnt/mylv ext4 defaults 0 2
-
Resize the LV (Extend):
To extend the logical volume to 30GB:
sudo lvextend -L +10G /dev/myvg/mylv
This adds 10GB to the existing LV. Then, you need to resize the file system to match the LV’s new size:
sudo resize2fs /dev/myvg/mylv
(Use
xfs_growfs /mnt/mylv
for XFS.) -
Reduce LV:
Before reducing an LV, you must first unmount it:
sudo umount /mnt/mylv
Then run e2fsck
(or equivalent fsck for your FS)
sudo e2fsck -f /dev/myvg/mylv
Now, shrink the filesystem using resize2fs
with the new size. For example, to resize to 15G:
sudo resize2fs /dev/myvg/mylv 15G
Now, you can shrink the volume:
sudo lvreduce -L 15G /dev/myvg/mylv
After shrinking, remount the device:
sudo mount /dev/myvg/mylv /mnt/mylv
Important Note: Reducing LV size can be risky. Always back up your data before attempting it. Also ensure that your data in the file system doesn’t exceed the new size or data loss can occur.
-
Remove LV:
First, unmount it:
sudo umount /mnt/mylv
Then deactivate it:
sudo lvremove /dev/myvg/mylv
- Remove VG:
sudo vgremove myvg
- Remove PV:
sudo pvremove /dev/sdb
sudo pvremove /dev/sdc
Advantages of LVM
- Flexibility: Easily resize logical volumes without downtime (in most cases).
- Scalability: Add or remove physical storage without disrupting services.
- Snapshots: Create point-in-time snapshots of logical volumes for backups or testing. Snapshots allow you to revert to a previous state.
- Striping: Improve performance by striping data across multiple physical disks.
- Mirroring: Increase reliability by mirroring data across multiple physical disks.
- Concatenation: Combine multiple physical volumes into a single logical volume.
- Dynamic Allocation: Allocate storage space only when needed, reducing wasted capacity.
- Simplified Management: Provides a centralized view of storage resources, simplifying administration.
Disadvantages of LVM
- Complexity: Adds a layer of abstraction, which can increase complexity for novice users.
- Performance Overhead: While generally minimal, LVM can introduce a slight performance overhead compared to directly using partitions.
- Recovery Complexity: Recovering from LVM failures can be more complex than recovering from traditional partition failures.
- Boot Issues: If the root filesystem
/
resides on an LVM volume and something goes wrong with the LV, the system may not boot correctly. This is less of an issue with modern bootloaders and distributions, but it’s a consideration. - Increased I/O Load on the Physical Volumes: If you create multiple LVs on the same physical volume, the I/O load can be significantly increased on that physical volume.
Features
- Thin Provisioning: Allows you to create logical volumes that appear larger than the available physical storage. Space is only allocated as data is written, which can save space but requires careful monitoring to avoid running out of physical storage.
- RAID Integration: LVM can be used in conjunction with RAID to provide both flexibility and data redundancy.
- Caching: LVM can be configured to use faster storage devices (e.g., SSDs) as a cache for slower storage devices (e.g., HDDs), improving performance.
- Snapshot Functionality: LVM snapshots are copy-on-write, making them space efficient. They are often used for backup operations or for testing changes before applying them to the live system.
Conclusion
LVM is a powerful and versatile tool for managing storage in modern systems. Its ability to provide flexibility, scalability, and advanced features like snapshots makes it a valuable asset for system administrators. While it introduces a layer of complexity, the benefits it offers in terms of manageability and efficiency often outweigh the drawbacks. By understanding the basic concepts and commands of LVM, you can significantly improve your ability to manage storage resources effectively and adapt to changing needs. Remember to always back up your data before making any significant changes to your storage configuration, especially when working with LVM.
This content originally appeared on DEV Community and was authored by Aviral Srivastava