18. LVM Storage
Traditional disk partitioning (MBR and GPT) binds filesystems directly to physical disk boundaries. When a partition runs out of space, resizing it is complex, risky, and often requires taking systems offline.
Logical Volume Management (LVM) introduces an abstraction layer between physical storage devices and the operating system filesystems. It allows multiple physical drives to be aggregated into a shared storage pool, enabling online partition resizing, non-disruptive disk migrations, and point-in-time snapshots.
18.1 LVM Architecture & Extents
Section titled “18.1 LVM Architecture & Extents”LVM organizes storage into three foundational layers:
Physical Disks / Partitions (/dev/sdb, /dev/sdc) ↓ Physical Volumes (PV) [Disks/partitions initialized with an LVM header] ↓ Volume Group (VG) [Unified storage pool combining all PVs] ↓ Logical Volumes (LV) [Virtual partitions carved out of the VG pool] ↓ Filesystem (ext4, xfs) [Mounted into Linux directory tree]Physical Extents (PE) & Logical Extents (LE)
Section titled “Physical Extents (PE) & Logical Extents (LE)”Under the hood, LVM divides storage space into small, fixed-size contiguous chunks called Extents:
- Physical Extent (PE): The smallest allocatable storage block on a Physical Volume (default size is 4 MB).
- Logical Extent (LE): The corresponding storage block on a Logical Volume. Each LE maps to a PE.
Because an LV is simply an ordered collection of extents, an LV can be expanded by assigning additional PEs from anywhere in the Volume Group, even across different physical disks!
18.2 Advantages & Key Features of LVM
Section titled “18.2 Advantages & Key Features of LVM”- Dynamic Online Resizing: Expand logical volumes and filesystems on the fly without unmounting or rebooting.
- Unified Storage Pooling: Aggregate multiple physical drives (e.g., two 500 GB disks) into a single 1 TB Volume Group.
- Snapshots: Create point-in-time copy-on-write copies of logical volumes for consistent database backups or pre-update testing.
- Data Migration (
pvmove): Move live data extents from an old physical drive to a new physical drive with zero downtime. - Performance Striping: Distribute data blocks evenly across multiple physical disks (similar to RAID 0) to maximize read/write throughput.
- Redundancy (Mirroring): Store duplicate copies of extents across distinct physical disks (similar to RAID 1) for fault tolerance.
- Hardware Abstraction: Underlying disk geometries and controllers are hidden from applications and users.
18.3 Step-by-Step LVM Workflow: Create, Format & Mount
Section titled “18.3 Step-by-Step LVM Workflow: Create, Format & Mount”Step 1: Prepare Physical Volumes (PV)
Section titled “Step 1: Prepare Physical Volumes (PV)”Identify raw disks or partitions using lsblk or fdisk -l, then initialize them:
# Initialize physical partitions/diskssudo pvcreate /dev/sdb1 /dev/sdc1
# Verify creation (concise view)sudo pvs
# View detailed physical volume attributessudo pvdisplayStep 2: Create and Manage Volume Groups (VG)
Section titled “Step 2: Create and Manage Volume Groups (VG)”Combine one or more PVs into a unified storage pool:
# Create Volume Group named 'vg_data' from the initialized PVssudo vgcreate vg_data /dev/sdb1 /dev/sdc1
# Verify Volume Groupsudo vgssudo vgdisplay
# Extend a Volume Group by adding a new disk later:sudo pvcreate /dev/sdd1sudo vgextend vg_data /dev/sdd1Step 3: Create Logical Volumes (LV)
Section titled “Step 3: Create Logical Volumes (LV)”Carve out virtual partitions from the VG pool:
# Create a 20 GB Logical Volume named 'lv_app'sudo lvcreate -L 20G -n lv_app vg_data
# Alternatively, allocate 100% of remaining free space in the VG:# sudo lvcreate -l 100%FREE -n lv_app vg_data
# Verify Logical Volumessudo lvssudo lvdisplayStep 4: Format and Mount the Logical Volume
Section titled “Step 4: Format and Mount the Logical Volume”The Logical Volume is accessible as a block device at /dev/vg_name/lv_name or /dev/mapper/vg_name-lv_name.
# Format with ext4 or xfssudo mkfs.ext4 /dev/vg_data/lv_app
# Create mount point and mount the volumesudo mkdir -p /mnt/appdatasudo mount /dev/vg_data/lv_app /mnt/appdata
# Verify mount statusdf -h | grep /mnt/appdataStep 5: Persistent Mounting (/etc/fstab)
Section titled “Step 5: Persistent Mounting (/etc/fstab)”Add an entry to /etc/fstab so the volume mounts automatically on system boot:
/dev/vg_data/lv_app /mnt/appdata ext4 defaults 0 218.4 Resizing Logical Volumes
Section titled “18.4 Resizing Logical Volumes”Extending a Logical Volume (Online)
Section titled “Extending a Logical Volume (Online)”If storage utilization approaches capacity and the Volume Group has unallocated extents, extend the LV and its underlying filesystem immediately:
# Option A: Extend LV and resize filesystem in a single command using -rsudo lvextend -r -L +10G /dev/vg_data/lv_app
# Option B: Manual two-step process# 1. Extend the LV:sudo lvextend -L +10G /dev/vg_data/lv_app
# 2. Resize the filesystem:# For ext4:sudo resize2fs /dev/vg_data/lv_app# For XFS:sudo xfs_growfs /mnt/appdataReducing a Logical Volume (Offline & Caution Required)
Section titled “Reducing a Logical Volume (Offline & Caution Required)”[!WARNING] Shrinking a filesystem carries risk of data corruption. XFS filesystems cannot be shrunk. Only
ext4supports reduction, and the volume must be unmounted before shrinking.
- Unmount the filesystem:
Terminal window sudo umount /mnt/appdata - Force an integrity check on the filesystem:
Terminal window sudo e2fsck -f /dev/vg_data/lv_app - Shrink the filesystem first:
Terminal window sudo resize2fs /dev/vg_data/lv_app 10G - Shrink the Logical Volume second:
Terminal window sudo lvreduce -L 10G /dev/vg_data/lv_app - Remount the filesystem:
Terminal window sudo mount /dev/vg_data/lv_app /mnt/appdata
18.5 LVM Snapshots
Section titled “18.5 LVM Snapshots”An LVM snapshot is a point-in-time, copy-on-write image of an existing Logical Volume. It only consumes storage space as blocks on the origin volume change.
# Create a 5 GB snapshot named 'lv_app_snap' for 'lv_app'sudo lvcreate -s -L 5G -n lv_app_snap /dev/vg_data/lv_app
# Mount snapshot in read-only mode for consistent backupssudo mkdir -p /mnt/snapshotsudo mount -o ro /dev/vg_data/lv_app_snap /mnt/snapshot
# Once backup finishes, unmount and delete snapshotsudo umount /mnt/snapshotsudo lvremove -y /dev/vg_data/lv_app_snap18.6 Deleting an LVM Stack (Strict Tear-Down Order)
Section titled “18.6 Deleting an LVM Stack (Strict Tear-Down Order)”When decommissioning storage, LVM layers must be removed in strict reverse order to maintain metadata integrity:
1. Unmount Filesystem (umount) ↓ 2. Remove Logical Volume (lvremove) ↓ 3. Remove Volume Group (vgremove) ↓ 4. Remove Physical Volume (pvremove)Execution Commands
Section titled “Execution Commands”# Step 1: Unmount the filesystem and remove /etc/fstab entrysudo umount /mnt/appdata
# Step 2: Remove the Logical Volumesudo lvremove /dev/vg_data/lv_app
# Step 3: Remove the Volume Groupsudo vgremove vg_data
# Step 4: Remove the Physical Volume headerssudo pvremove /dev/sdb1 /dev/sdc118.7 LVM Operations Quick Reference
Section titled “18.7 LVM Operations Quick Reference”| Operation | Primary Command | Notes |
||||
| Create Physical Volume | sudo pvcreate /dev/sdX1 | Writes LVM identifier header |
| List Physical Volumes | sudo pvs / sudo pvdisplay | Shows PV size and PE allocation |
| Create Volume Group | sudo vgcreate vg_name /dev/sdX1 | Combines PVs into storage pool |
| Expand Volume Group | sudo vgextend vg_name /dev/sdY1 | Adds new PV to existing VG |
| Create Logical Volume | sudo lvcreate -L 20G -n lv_name vg_name | Allocates virtual partition |
| List Logical Volumes | sudo lvs / sudo lvdisplay | Shows LV sizes and snapshot states |
| Extend LV & Filesystem | sudo lvextend -r -L +10G /dev/vg/lv | Online resize (-r handles fs) |
| Create Snapshot | sudo lvcreate -s -L 5G -n snap /dev/vg/lv | Copy-on-write snapshot |
| Delete Logical Volume | sudo lvremove /dev/vg/lv | Must unmount filesystem first |
| Delete Volume Group | sudo vgremove vg_name | Requires all LVs to be removed first |
| Delete Physical Volume | sudo pvremove /dev/sdX1 | Clears LVM disk header |