Pages

RHEL Linux Logical Volume Manager(LVM)

Linux storage can feel rigid when relying solely on traditional partitioning. Enter Logical Volume Manager (LVM): a device-mapper framework in the Linux kernel that abstracts physical disks into flexible, resizable volumes. Whether you’re consolidating servers, migrating to larger drives, or implementing snapshots for backups, LVM provides enterprise-grade control—often without downtime.
This guide covers LVM fundamentals, setup, migration (including root disks), resizing, snapshots, and pro tips—tailored for RHEL 6–10 environments.

Understanding LVM: The Core Hierarchy
Think of LVM like a Lego set for storage: you assemble pieces into something bigger, more flexible, and easier to manage.

Key Components
  • Physical Volumes (PVs): Raw disks or partitions that LVM uses.
  • Volume Groups (VGs): Aggregates PVs into a shared pool of storage.
  • Logical Volumes (LVs): Carve out virtual disks from the VG.
  • Extents: Small blocks (default 4MB) that map physical to logical space.
Why Use LVM?
  • Dynamic resizing: Grow or shrink volumes while online.
  • Span disks: Combine multiple drives into one logical volume.
  • Snapshots: Create instant, point-in-time copies for backups or testing.
  • Thin provisioning: Allocate space on demand (RHEL 8+).
  • Migration: Move data between disks without unmounting.

Setting Up LVM on RHEL
Assume you have two new disks: /dev/sdb (100GB) and /dev/sdc (200GB).

1. Prepare Physical Volumes
lsblk                 # Identify your disks
pvcreate /dev/sdb /dev/sdc  # Initialize for LVM
pvs                   # Verify creation

2. Create a Volume Group
vgcreate vgdata /dev/sdb /dev/sdc  # Combine ~300GB into a VG
vgs                                 # Check VG free space

3. Create Logical Volumes
lvcreate -L 50G -n lvstorage vgdata  # Fixed size LV
Or use all space:
lvcreate -l 100%FREE -n lvdata vgdata
lvs                                  # List logical volumes

4. Format and Mount
mkfs.xfs /dev/vgdata/lvstorage  # XFS recommended for performance
mkdir /mnt/storage
mount /dev/vgdata/lvstorage /mnt/storage
echo '/dev/vgdata/lvstorage /mnt/storage xfs defaults 0 0' >> /etc/fstab

Pro Tip: Use XFS for large volumes (>50GB) or high IOPS; ext4 works for smaller/general-purpose volumes.

Power Move: Online Storage Migration (No Downtime!)
LVM excels at moving data live. Add a new disk, migrate data, and remove the old disk—all without downtime.

Example: Migrate /dev/sdb (100GB) to /dev/sdc
pvcreate /dev/sdc
vgextend vgdata /dev/sdc        # Expand VG
pvmove /dev/sdb /dev/sdc        # Move extents live
vgreduce vgdata /dev/sdb
pvremove /dev/sdb                # Clean up

Monitoring progress:
lvs -o +devices

Root Disk Migration
Online (if enough space):
pvmove /dev/sda /dev/sdb → grub2-install /dev/sdb → grub2-mkconfig -o /boot/grub2/grub.cfg

Offline (live ISO):
Use rsync -aHAX to copy root, update /etc/fstab, and reinstall GRUB.
Resizing Volumes: Grow and Shrink

Extend (Easy, Online)
lvextend -L +20G /dev/vgdata/lvstorage
xfs_growfs /mnt/storage          # XFS auto-expands
ext4: resize2fs /dev/vgdata/lvstorage

Shrink (Backup First! Offline Only)
umount /mnt/storage
e2fsck -f /dev/vgdata/lvstorage
resize2fs /dev/vgdata/lvstorage 30G  # Shrink FS first
lvreduce -L 30G /dev/vgdata/lvstorage
mount /mnt/storage
Warning: Shrinking is risky—always backup first.

Snapshots: Your Backup Superpower
Snapshots are copy-on-write backups of LVs—great for testing or backups.
lvcreate -L 10G -s -n snap1 /dev/vgdata/lvstorage
mount /dev/vgdata/snap1 /mnt/backup
lvremove /dev/vgdata/snap1       # Remove when done

Snapshot size grows as the original LV changes.
Thin snapshots optimize space usage.

Clustered/Shared VGs (RHEL High Availability)
For HA clusters, LVM supports shared volume groups.

1. Edit /etc/lvm/lvm.conf:
global {
    use_lvmetad = 0
    use_lvmlockd = 1
}
activation {
    volume_list = [ "vg_cluster" ]
}

2. Enable services:
# systemctl enable --now lvmlockd.service lvm2-lvmlockd.socket
vgcreate --shared vg_cluster /dev/sdb
vgs -o +shared

Monitoring and Troubleshooting
Daily Commands:
pvs / vgs / lvs                    # Short overview
pvdisplay / vgdisplay / lvdisplay  # Verbose
lvs -a -o +segtype,discards        # Advanced

Common Fixes:

IssueFix
"PV not found"pvscan --cache or rescan SCSI: echo 1 > /sys/class/scsi_host/hostX/scan
Snapshot fullExtend snap LV or merge: lvconvert --merge snap1
Thin pool out of spacelvextend -L +10G vg/thinpool; thin_check /dev/vg/thinpool
Boot fails post-migrateBoot to rescue, chroot /mnt/sysimage grub2-install /dev/sda

Performance Tips:
  • PE size: 4MB default, 1GB for huge volumes.
  • Align data: pvcreate --dataalignment 1M /dev/sdb.
  • Use thin LVs for VM storage: lvcreate -T -L 100G -n thinpool vgdata.
When to Use LVM
  • Ideal for: Servers with changing storage, virtualization snapshots, SAN migrations.
  • Avoid for: Tiny embedded systems or workloads demanding maximum raw disk performance.
Conclusion
LVM turns static disks into a dynamic storage orchestra. Start small, experiment in VMs, and scale confidently. With LVM, RHEL administrators can resize, migrate, snapshot, and manage storage without disrupting production systems.

No comments:

Post a Comment