Introduction
Traditional disk partitioning on Linux involves dividing a physical hard drive into fixed segments. This method is rigid; if a partition runs out of space, resizing it can be dangerous and complex. The Logical Volume Manager (LVM) solves this problem by abstracting the physical storage into logical layers, allowing administrators to resize, span, and manage volumes dynamically without unmounting the file system or rebooting the server. This guide covers the fundamentals of creating and managing LVM storage.
Understanding LVM Architecture
LVM relies on three distinct layers of abstraction:
- Physical Volumes (PV): The underlying raw storage devices (e.g.,
/dev/sdb,/dev/nvme0n1). - Volume Groups (VG): A pool of storage created by combining one or more Physical Volumes.
- Logical Volumes (LV): The usable partitions created from the Volume Group’s storage pool, which are then formatted with a file system (like ext4 or XFS).
Step 1: Initialize Physical Volumes (PV)
Before LVM can use a disk, it must be initialized as a Physical Volume. Suppose you have added a new disk to your server recognized as /dev/sdb.
Run the pvcreate command to initialize it:
sudo pvcreate /dev/sdb
You can verify the creation by running sudo pvs or sudo pvdisplay. The disk is now ready to be added to a Volume Group.
Step 2: Create a Volume Group (VG)
Next, combine the Physical Volume(s) into a Volume Group. Think of the VG as a large virtual hard drive. Let’s create a Volume Group named data_vg using our new PV.
sudo vgcreate data_vg /dev/sdb
If you wanted to pool two disks together immediately, you could run sudo vgcreate data_vg /dev/sdb /dev/sdc. Use sudo vgs to see the total size and available free space in your new Volume Group.
Step 3: Create a Logical Volume (LV)
Now that you have a pool of storage, you can carve out Logical Volumes. Let’s create a 50GB Logical Volume named database_lv from the data_vg Volume Group.
sudo lvcreate -L 50G -n database_lv data_vg
If you want the Logical Volume to use all remaining free space in the Volume Group, use the -l flag with the 100%FREE argument:
sudo lvcreate -l 100%FREE -n database_lv data_vg
Verify the creation using sudo lvs.
Step 4: Format and Mount the Logical Volume
The Logical Volume is now ready to be formatted with a file system. We will format it with XFS, a robust journaling file system commonly used in enterprise environments.
sudo mkfs.xfs /dev/data_vg/database_lv
Create a mount point and mount the volume:
sudo mkdir -p /mnt/databasesudo mount /dev/data_vg/database_lv /mnt/database
To ensure the volume mounts automatically on boot, add an entry to /etc/fstab using the volume’s UUID.
Step 5: Expanding a Logical Volume
The true power of LVM is the ability to expand storage dynamically. If /mnt/database is running out of space, and there is free space in the data_vg Volume Group, you can extend the LV and resize the file system in one command.
To add 20GB to the existing volume:
sudo lvextend -L +20G -r /dev/data_vg/database_lv
The -r flag is crucial; it tells LVM to automatically resize the underlying file system (ext4 or XFS) to match the new size of the Logical Volume. Without this flag, you would have to run xfs_growfs or resize2fs manually afterward.