Table of Contents
- LVM Overview
- Core Concepts
- Common Operations
- Disk Scanning
- Creating LVM Volumes
- Expanding Logical Volumes
- Shrinking Logical Volumes
- Swap Partition Expansion
- Command Reference
LVM Overview
LVM (Logical Volume Manager) is a storage device management technology that provides logical abstraction layer between physical storage devices and file systems. It allows for flexible disk space management in Linux environments.
Benefits of LVM
LVM is commonly used in systems with multiple disks, but it also provides significant advantages for smaller configurations.
Advantages for Small Systems: Traditional partitioning creates a one-to-one mapping between filesystems and partitions, which presents several challenges:
1. Partitions operate independently with no coordination, leading to imbalanced space utilization
2. When a filesystem reaches capacity, expanding it requires complex procedures such as:
- Repartitioning and recreating filesystems
- Moving data to larger partitions
- Using symbolic links to utilize space from other partitions
3. Merging multiple partitions requires complete repartitioning with data backup and restore
LVM addresses these issues by providing:
1. Unified volume group management allowing easy addition or removal of partitions
2. Dynamic resizing of logical volumes within volume group capacity limits
3. Cross-partition filesystem support for improved flexibility
Advantages for Large Systems:
1. Simplified administration in environments with numerous disks of varying capacities
2. Improved scalability when allocating space across different users and applications
3. Online resizing capabilities without service interruption
4. Easy integration of new storage devices without data migration
Core Concepts
Physical Volume (PV)
Physical volumes form the foundation of LVM. They can be either entire physical disks or individual disk partitions. Each physical volume contains LVM metadata headers that identify it as part of the LVM system.
Volume Group (VG)
A volume group aggregates one or more physical volumes into a unified storage pool. Volume groups provide the foundation for creating logical volumes and can be dynamically expanded by adding additional physical volumes. A single system may contain multiple volume groups.
Logical Volume (LV)
Logical volumes are created from the available space within volume groups. They function similarly to traditional partitions but offer greater flexibility, allowing dynamic expansion or reduction without service interruption. Multiple logical volumes can exist within a single volume group.
Physical Extent (PE)
Physical extents are the fundamental allocation units within physical volumes. When creating a physical volume, you specify the PE size, which remains fixed for the lifetime of that volume. All physical volumes within a volume group must use the same PE size.
Logical Extent (LE)
Logical extents correspond to physical extents in the underlying storage. The relationship between logical and physical extents determines how data is actually stored on physical devices.
Volume Group Descripter Area (VGDA)
The VGDA resides on each physical volume and contains metadata about the physical volume, its volume group, logical volumes within that group, and the allocation status of physical extents. This area is created automatically when initializing a physical volume with pvcreate.
Common Operations
Disk Scanning
When addding new storage devices to a Linux system, the operating system may not automatically detect them. The following methods enable disk rescanning.
Method 1: Using rescan-scsi-bus.sh
Install the required utilities:
yum install sg3_utils
Execute the scanning script:
rescan-scsi-bus.sh
Method 2: Manual SCSI Rescan
echo "- - -" > /sys/class/scsi_host/host0/scan
Verify the new devices:
fdisk -l | grep -i sd
Creating LVM Volumes
Partition Setup
LVM can use either raw disks or partitioned disks. This section covers both approaches.
Step 1: Verify Disk Detection
fdisk -l | grep -i /dev
Step 2: Create Partition using fdisk
fdisk /dev/sdc
n # Create new partition
p # Display partition table
1 # Partition number 1
w # Write changes
# Change partition type to LVM
fdisk /dev/sdc
t # Change partition type
8e # LVM partition type (Hex code)
w # Write changes
Step 3: GPT Partitioning for Disks Larger Than 2TB
# Create GPT partition spanning entire disk
parted -s /dev/sdc mklabel gpt mkpart primary ext4 1 100%
# Format the partition
mkfs.ext4 /dev/sdc1
Creating Physical Volumes
# Display existing disks
fdisk -l | grep -i /dev
# View current physical volumes
pvs
pvdisplay
# Initialize disk or partition as physical volume
pvcreate /dev/sdc
pvcreate /dev/sdc1
Removing a Physical Volume:
pvremove /dev/sdc
Creating Volume Groups
# Display existing volume groups
vgs
vgdisplay
# Create volume group from physical volume
vgcreate vgpool /dev/sdc
Removing a Volume Group:
vgremove vgpool
Creating Logical Volumes
# Display available logical volumes
lvs
lvdisplay
# Create logical volume using entire volume group
lvcreate -l 100%VG -n myvolume vgpool
# Create logical volume with specific size
lvcreate -L 1.5T -n myvolume vgpool
Removing a Logical Volume:
lvremove /dev/vgpool/myvolume
Formatting and Mounting
After creating the logical volume, obtain the device path from lvdisplay:
Formatting:
# EXT4 filesystem
mkfs.ext4 /dev/vgpool/myvolume
# XFS filesystem
mkfs.xfs /dev/vgpool/myvolume
Mounting:
mount /dev/vgpool/myvolume /mnt/storage
Configuring Automatic Mount at Boot
Edit /etc/fstab to ensure the logical volume mounts automatically on system startup:
vi /etc/fstab
Add the following entry:
# XFS filesystem
/dev/mapper/vgpool-myvolume /mnt/storage xfs rw,noatime,inode64,allocsize=16m 1 2
# EXT4 filesystem
/dev/mapper/vgpool-myvolume /mnt/storage ext4 defaults 1 2
Expanding Logical Volumes
Step 1: Identify Available Storage
fdisk -l | grep -i /dev
pvs
Step 2: Create Physical Volume from New Disk
pvcreate /dev/sdd
pvs
pvdisplay /dev/sdd
Step 3: Extend Volume Group
Option A: Add New Physical Volume to Volume Group
vgextend vgpool /dev/sdd
Option B: Check Existing Free Space
vgs
Step 4: Extend Logical Volume
Extend to use all available free space:
lvextend -l +100%FREE /dev/mapper/vgpool-myvolume
Extend by specific size:
lvextend -L +5G /dev/mapper/vgpool-myvolume
Extend by percentage of free space:
lvextend -l +30%FREE /dev/mapper/vgpool-myvolume
Step 5: Resize Filesystem
For EXT4 filesystems:
e2fsck -f /dev/mapper/vgpool-myvolume
resize2fs /dev/mapper/vgpool-myvolume
For XFS filesystems:
xfs_growfs /dev/mapper/vgpool-myvolume
Step 6: Verify Expansion
df -h
Quick Reference - Disk Expansion Process
# 1. Create physical volume
pvcreate /dev/sdd
# 2. Add to volume group
vgextend vgpool /dev/sdd
# 3. Extend logical volume
lvextend -l +100%FREE /dev/mapper/vgpool-myvolume
# 4. Resize filesystem (XFS example)
xfs_growfs /dev/mapper/vgpool-myvolume
# 5. Verify
df -h
Shrinking Logical Volumes
Warning: Reducing logical volume size carries risk of data loss. Ensure adequate backup before proceeding.
Step 1: Unmount Filesystem
umount /mnt/storage
Step 2: Verify Unmount
mount | grep myvolume | wc -l
Step 3: Check Logical Volume Status
lvs | grep myvolume
Step 4: Filesystem Check
e2fsck -f /dev/vgpool/myvolume
Step 5: Resize Filesystem
resize2fs /dev/vgpool/myvolume 1G
Step 6: Verify Size (Should Remain Unchanged)
lvs | grep myvolume
Step 7: Reduce Logical Volume
lvreduce -L 1GB /dev/vgpool/myvolume
Step 8: Verify New Size
lvs | grep myvolume
Step 9: Remount
mount /dev/vgpool/myvolume /mnt/storage
Step 10: Check Status
df -H | grep myvolume
Swap Partition Expansion
Step 1: Check Current Swap
swapon -s
Step 2: Disable Swap
swapoff -a
Step 3: Extend Swap Logical Volume
lvextend -L 32G /dev/centos/swap
Step 4: Format Swap
mkswap /dev/centos/swap
Step 5: Enable Swap
swapon -a
Step 6: Verify
free -g
Command Reference
Physical Volume Commands
# Scan for physical volumes
pvscan
# Display physical volume details
pvdisplay /dev/sdc
# Display VGDA information (debugging)
pvdata /dev/sdc
# Change physical volume allocation settings
pvchange -x n /dev/sdc
# Create physical volume
pvcreate /dev/sdc
# Move physical extent data
pvmove /dev/sdc
Volume Group Commands
# Scan for volume groups
vgscan
# Check volume group consistency
vgck vgpool
# Display volume group properties
vgdisplay vgpool
# Rename volume group
vgrename oldvg newvg
# Activate/deactivate volume group
vgchange -a y vgpool
# Set maximum logical volumes
vgchange -l 255 vgpool
# Create volume group
vgcreate vgpool /dev/sdc
# Remove volume group
vgmove vgpool
# Extend volume group
vgextend vgpool /dev/sdd
# Reduce volume group
vgreduce vgpool /dev/sdc
# Merge volume groups
vgmerge targetvg sourcevg
# Split volume group
vgsplit originalvg newvg /dev/sdc
# Export volume group
vgexport vgpool
# Import volume group
vgimport vgpool /dev/sdc
# Backup VGDA
vgcfgbackup vgpool
# Restore VGDA
vgcfgrestore -n vgpool /dev/sdc
Logical Volume Commands
# Scan for logical volumes
lvscan
# Display logical volume details
lvdisplay /dev/vgpool/myvolume
# Rename logical volume
lvrename /dev/vgpool/oldvolume /dev/vgpool/newvolume
lvrename vgpool oldvolume newvolume
# Change logical volume attributes
lvchange
# Extend logical volume
lvextend -L +10G /dev/vgpool/myvolume
# Reduce logical volume
lvreduce -L -5G /dev/vgpool/myvolume
# Create logical volume
lvcreate -L 10G -n myvolume vgpool
# Remove logical volume
lvremove /dev/vgpool/myvolume
General LVM Managemant
# Scan all storage devices
lvmdiskscan
# Reset LVM
lvmchange -R
# Collect LVM statistics
lvmsadc /var/log/lvm.log
# Report LVM statistics
lvmsar /var/log/lvm.log