Managing storage on Linux servers and desktop systems can be challenging when relying on traditional disk partitioning. If a standard partition runs out of space, resizing it often requires downtime, complex procedures, and significant risk of data loss. This is where LVM (Logical Volume Manager) provides a much better workflow.
LVM introduces an abstraction layer between the physical disks and the file system. Instead of writing directly to fixed partitions, you pool your storage drives together and dynamically allocate space exactly where it is needed. If a volume fills up, you can simply add a new hard drive to the pool and expand the file system while the server remains online.
In this guide, you will learn how to set up LVM from scratch, create volume groups, allocate logical volumes, and format them for use. You will also learn how to extend a volume when you need more space.
Why Use LVM Instead of Traditional Partitions?
Before implementing LVM, it is important to understand the benefits it brings to system administration:
- Dynamic resizing: You can shrink or expand storage volumes on the fly without rebooting.
- Storage pooling: Multiple physical hard drives can be combined into a single large storage pool.
- Live migration: Data can be moved from one physical disk to another without interrupting user access.
- Snapshots: LVM supports point-in-time snapshots, making it incredibly useful for safe system backups and testing.
Understanding the Three Layers of LVM
LVM organises storage into three distinct layers. Understanding these concepts is essential before executing any commands:
- Physical Volumes (PV): These are the actual underlying storage devices (e.g.,
/dev/sdb,/dev/nvme0n1, or a specific partition). - Volume Groups (VG): One or more Physical Volumes are combined into a Volume Group. This acts as a unified storage pool.
- Logical Volumes (LV): These are the virtual partitions created from the Volume Group. You format Logical Volumes with a file system (like ext4 or XFS) and mount them.
Step 1: Identify Your Storage Devices
Before configuring LVM, you must identify the empty disks you want to use. You can list all block devices on your Linux system using the lsblk command.
lsblk
Locate the drive you wish to use (for example, /dev/sdb). Ensure this drive does not contain any important data, as the setup process will erase it.
Step 2: Create a Physical Volume (PV)
The first step in building the LVM structure is marking the disk as an LVM Physical Volume. This prepares the drive for inclusion in a Volume Group.
sudo pvcreate /dev/sdb
If the command is successful, the terminal will output a confirmation message. You can verify the creation of the Physical Volume by running:
sudo pvs
Step 3: Create a Volume Group (VG)
Next, you must create a Volume Group to act as the storage pool. In this example, we will create a Volume Group named data_pool using the Physical Volume we just initialised.
sudo vgcreate data_pool /dev/sdb
If you have multiple drives (e.g., /dev/sdb and /dev/sdc), you can add them to the pool simultaneously:
sudo vgcreate data_pool /dev/sdb /dev/sdc
To check the status and available space of your new Volume Group, use the following command:
sudo vgs
Step 4: Create a Logical Volume (LV)
Now that you have a storage pool (the Volume Group), you can carve out Logical Volumes. These act exactly like traditional partitions.
To create a 50GB Logical Volume named web_data from the data_pool Volume Group, use the lvcreate command:
sudo lvcreate -n web_data -L 50G data_pool
If you want to use 100% of the remaining free space in the Volume Group instead of specifying an exact size, you can use the extent flag:
sudo lvcreate -n web_data -l 100%FREE data_pool
You can verify the Logical Volume creation with:
sudo lvs
Step 5: Format and Mount the Logical Volume
The Logical Volume is now ready, but it needs a file system before you can store files on it. In most modern Linux distributions, ext4 or xfs are the standard choices.
To format the volume as ext4:
sudo mkfs.ext4 /dev/data_pool/web_data
Next, create a mount point directory:
sudo mkdir -p /mnt/web_data
Finally, mount the Logical Volume:
sudo mount /dev/data_pool/web_data /mnt/web_data
You can confirm the mount is successful by running df -h.
Step 6: Make the Mount Persistent
If you reboot your system now, the Logical Volume will not mount automatically. To make the mount persistent, you must add it to the /etc/fstab file.
First, find the UUID of your new Logical Volume:
sudo blkid /dev/data_pool/web_data
Open the /etc/fstab file in a text editor like Nano:
sudo nano /etc/fstab
Add the following line to the bottom of the file (replace the UUID with your actual output):
UUID=your-uuid-here /mnt/web_data ext4 defaults 0 2
Save and exit. Your LVM storage will now mount automatically upon boot.
How to Extend a Logical Volume
One of the primary reasons to use LVM is the ability to easily resize storage. If your web_data volume is running out of space, and you have free space remaining in the data_pool Volume Group, you can extend it seamlessly.
First, extend the Logical Volume by an additional 20GB:
sudo lvextend -L +20G /dev/data_pool/web_data
Next, you must resize the underlying file system to recognise the new space. For an ext4 file system, use:
sudo resize2fs /dev/data_pool/web_data
If you are using an XFS file system, you would use xfs_growfs instead.
Best Practices for LVM Management
- Always verify backups: Before performing operations like shrinking volumes or removing disks, ensure you have verified backups.
- Leave free space: Do not allocate 100% of your Volume Group immediately. Leaving free space allows you to create snapshots or expand volumes later.
- Use descriptive names: Name your Volume Groups and Logical Volumes clearly based on their purpose (e.g.,
database_vg,backup_lv) to prevent administrative confusion.
By implementing LVM, you transition from rigid storage constraints to a flexible, enterprise-grade storage management workflow.