LVM Management
LVM (Logical Volume Manager) is a powerful disk management tool in Linux. It adds an abstraction layer between physical disks and filesystems, making storage allocation and resizing much more flexible.
LVM Core Concepts
Section titled “LVM Core Concepts”LVM consists of three layers:
| Layer | Abbreviation | Description |
|---|---|---|
| Physical Volume | PV (Physical Volume) | The underlying disk or partition; the foundational building block of LVM |
| Volume Group | VG (Volume Group) | A storage pool composed of one or more PVs |
| Logical Volume | LV (Logical Volume) | A logical partition allocated from a VG; can be formatted and mounted |
The layer relationships are as follows:
Disk/Partition --> PV --> VG --> LV --> Filesystem/dev/sdb1 --> PV1 -+ +--> vg_data --> lv_app --> /app/dev/sdc1 --> PV2 -+ --> lv_backup --> /backupInstalling LVM Tools
Section titled “Installing LVM Tools”Most Enterprise Linux distributions come with LVM pre-installed. If it is not installed:
sudo dnf install lvm2 -yCreating Physical Volumes (PV)
Section titled “Creating Physical Volumes (PV)”Physical volumes are the foundation layer of LVM. Disks or partitions must first be initialized as PVs.
Preparing Disk Partitions
Section titled “Preparing Disk Partitions”First, use fdisk or gdisk to create a partition and set its type to 8e (Linux LVM):
sudo fdisk /dev/sdb# n -> p -> 1 -> Enter -> Enter -> t -> 8e -> wsudo partprobe /dev/sdbInitializing Physical Volumes
Section titled “Initializing Physical Volumes”sudo pvcreate /dev/sdb1sudo pvcreate /dev/sdc1You can also create a PV directly on an entire disk (no prior partitioning needed):
sudo pvcreate /dev/sddViewing Physical Volume Information
Section titled “Viewing Physical Volume Information”# Brief listingsudo pvs
# Detailed informationsudo pvdisplay /dev/sdb1Creating Volume Groups (VG)
Section titled “Creating Volume Groups (VG)”Volume groups pool multiple PVs into a unified storage pool.
Creating a Volume Group
Section titled “Creating a Volume Group”sudo vgcreate vg_data /dev/sdb1 /dev/sdc1Viewing Volume Group Information
Section titled “Viewing Volume Group Information”# Brief listingsudo vgs
# Detailed informationsudo vgdisplay vg_dataExtending a Volume Group
Section titled “Extending a Volume Group”When new disks are added later, you can add new PVs to an existing VG:
sudo pvcreate /dev/sdd1sudo vgextend vg_data /dev/sdd1Removing a Physical Volume from a Volume Group
Section titled “Removing a Physical Volume from a Volume Group”# First migrate data to other PVssudo pvmove /dev/sdc1
# Then remove from the VGsudo vgreduce vg_data /dev/sdc1Creating Logical Volumes (LV)
Section titled “Creating Logical Volumes (LV)”Logical volumes are usable storage space carved out from a volume group that can be formatted and mounted.
Creating with a Specified Size
Section titled “Creating with a Specified Size”sudo lvcreate -L 50G -n lv_app vg_dataCreating with a Percentage of Remaining VG Space
Section titled “Creating with a Percentage of Remaining VG Space”# Use 100% of the remaining VG spacesudo lvcreate -l 100%FREE -n lv_backup vg_dataViewing Logical Volume Information
Section titled “Viewing Logical Volume Information”# Brief listingsudo lvs
# Detailed informationsudo lvdisplay /dev/vg_data/lv_appFormatting and Mounting a Logical Volume
Section titled “Formatting and Mounting a Logical Volume”# Format as XFSsudo mkfs.xfs /dev/vg_data/lv_app
# Create a mount point and mountsudo mkdir -p /appsudo mount /dev/vg_data/lv_app /app
# Verifydf -hT /appConfiguring fstab for Auto-Mounting
Section titled “Configuring fstab for Auto-Mounting”# Get the LV's UUID (you can also use the LV path directly)sudo blkid /dev/vg_data/lv_app
# Edit fstabecho '/dev/vg_data/lv_app /app xfs defaults 0 0' | sudo tee -a /etc/fstabExtending Logical Volumes
Section titled “Extending Logical Volumes”One of LVM’s most powerful features is online logical volume extension.
Extending an LV and Its Filesystem Simultaneously
Section titled “Extending an LV and Its Filesystem Simultaneously”The -r flag extends the filesystem automatically along with the LV:
# Add 20G of spacesudo lvextend -r -L +20G /dev/vg_data/lv_appExtending to a Specific Size
Section titled “Extending to a Specific Size”# Extend the LV to 100Gsudo lvextend -r -L 100G /dev/vg_data/lv_appExtending Using All Remaining Space
Section titled “Extending Using All Remaining Space”sudo lvextend -r -l +100%FREE /dev/vg_data/lv_appStep-by-Step Extension (manually extending the filesystem)
Section titled “Step-by-Step Extension (manually extending the filesystem)”If you do not use the -r flag, you need to extend the filesystem manually:
# Step 1: Extend the LVsudo lvextend -L +20G /dev/vg_data/lv_app
# Step 2: Extend the filesystem# For XFS filesystemssudo xfs_growfs /app
# For EXT4 filesystemssudo resize2fs /dev/vg_data/lv_appViewing LVM Status: pvs/vgs/lvs
Section titled “Viewing LVM Status: pvs/vgs/lvs”These three commands are the most commonly used LVM status queries in day-to-day operations.
pvs - Physical Volume Summary
Section titled “pvs - Physical Volume Summary”sudo pvsPV VG Fmt Attr PSize PFree/dev/sdb1 vg_data lvm2 a-- <50.00g 0/dev/sdc1 vg_data lvm2 a-- <50.00g 30.00gvgs - Volume Group Summary
Section titled “vgs - Volume Group Summary”sudo vgsVG #PV #LV #SN Attr VSize VFreevg_data 2 2 0 wz--n- 99.99g 30.00glvs - Logical Volume Summary
Section titled “lvs - Logical Volume Summary”sudo lvsLV VG Attr LSize Pool Origin Data% Meta%lv_app vg_data -wi-ao---- 50.00glv_backup vg_data -wi-ao---- 19.99gCustomizing Output Fields
Section titled “Customizing Output Fields”# Show LV name, size, volume group, and device pathsudo lvs -o lv_name,lv_size,vg_name,lv_pathLVM Snapshots
Section titled “LVM Snapshots”LVM snapshots create a read-only or read-write copy of a logical volume at a specific point in time. They are commonly used for backups and testing.
Creating a Snapshot
Section titled “Creating a Snapshot”# Create a 10G snapshot of lv_appsudo lvcreate -s -L 10G -n lv_app_snap /dev/vg_data/lv_appParameter descriptions:
-s: Create a snapshot-L 10G: Snapshot space size (for storing changed data)-n lv_app_snap: Snapshot name
Viewing Snapshot Status
Section titled “Viewing Snapshot Status”sudo lvsSnapshot volumes display the Origin (source volume) and Data% (percentage of snapshot space used).
Mounting a Snapshot (read-only access to historical data)
Section titled “Mounting a Snapshot (read-only access to historical data)”sudo mkdir -p /mnt/snapsudo mount -o ro /dev/vg_data/lv_app_snap /mnt/snapRestoring Data from a Snapshot
Section titled “Restoring Data from a Snapshot”You can copy needed files from the snapshot:
sudo cp /mnt/snap/important-file.conf /app/important-file.confOr merge the snapshot to roll back the entire logical volume:
# First unmount the source volume and the snapshotsudo umount /appsudo umount /mnt/snap
# Merge the snapshot (roll back to the state at snapshot time)sudo lvconvert --merge /dev/vg_data/lv_app_snap
# Remountsudo mount /dev/vg_data/lv_app /appDeleting a Snapshot
Section titled “Deleting a Snapshot”sudo umount /mnt/snapsudo lvremove /dev/vg_data/lv_app_snapRemoving LVM Resources
Section titled “Removing LVM Resources”When removing LVM resources, you must work in order: LV -> VG -> PV.
-
Unmount and remove the logical volume:
Terminal window sudo umount /appsudo lvremove /dev/vg_data/lv_app -
Remove the volume group:
Terminal window sudo vgremove vg_data -
Remove the physical volumes:
Terminal window sudo pvremove /dev/sdb1 /dev/sdc1
Hands-On: From Disk to LVM Mount
Section titled “Hands-On: From Disk to LVM Mount”The following demonstrates the complete process of setting up LVM storage using two new disks, /dev/sdb and /dev/sdc.
-
Create partitions on both disks:
Terminal window sudo parted /dev/sdb mklabel gptsudo parted /dev/sdb mkpart primary 0% 100%sudo parted /dev/sdc mklabel gptsudo parted /dev/sdc mkpart primary 0% 100%sudo partprobe -
Create physical volumes:
Terminal window sudo pvcreate /dev/sdb1 /dev/sdc1sudo pvs -
Create a volume group:
Terminal window sudo vgcreate vg_data /dev/sdb1 /dev/sdc1sudo vgs -
Create logical volumes:
Terminal window sudo lvcreate -L 80G -n lv_app vg_datasudo lvcreate -l 100%FREE -n lv_logs vg_datasudo lvs -
Format and mount:
Terminal window sudo mkfs.xfs /dev/vg_data/lv_appsudo mkfs.xfs /dev/vg_data/lv_logssudo mkdir -p /app /var/applogssudo mount /dev/vg_data/lv_app /appsudo mount /dev/vg_data/lv_logs /var/applogs -
Configure auto-mounting at boot:
Terminal window sudo cp /etc/fstab /etc/fstab.bakcat <<EOF | sudo tee -a /etc/fstab/dev/vg_data/lv_app /app xfs defaults 0 0/dev/vg_data/lv_logs /var/applogs xfs defaults 0 0EOFsudo mount -a -
Verify the results:
Terminal window df -hT /app /var/applogssudo pvs && sudo vgs && sudo lvs
The flexibility of LVM makes it the preferred solution for managing storage in Enterprise Linux environments. Mastering PV, VG, and LV operations along with online extension and snapshot capabilities will greatly improve storage management efficiency.