Logical Volume Management
Objective
Create and manage LVM physical volumes, volume groups, and logical volumes. Extend an online filesystem without downtime.
Tools & Technologies
pvcreatevgcreatelvcreatelvextendresize2fspvdisplay
Key Commands
pvcreate /dev/sdbvgcreate vg0 /dev/sdblvcreate -L 10G -n lv_data vg0lvextend -L +5G /dev/vg0/lv_dataresize2fs /dev/vg0/lv_dataArchitecture Overview
graph LR
subgraph Physical Layer
PV1[/dev/sdb\nPV]
PV2[/dev/sdc\nPV]
end
subgraph Logical Layer
PV1 --> VG[vg0\nVolume Group\n30GB pool]
PV2 --> VG
VG --> LV1[lv_root\n15GB]
VG --> LV2[lv_data\n10GB]
VG --> FREE[5GB free]
end
subgraph Filesystems
LV1 --> FS1[ext4\n/]
LV2 --> FS2[ext4\n/data]
end
style VG fill:#1a1a2e,stroke:#00d4ff,color:#e0e0e0
Step-by-Step Process
01
Create Physical Volume
Initialize a disk or partition as an LVM physical volume.
# Initialize disk as PV
sudo pvcreate /dev/sdb
sudo pvdisplay /dev/sdb
sudo pvs # brief summary
02
Create Volume Group
A volume group pools one or more PVs into a storage pool.
sudo vgcreate vg0 /dev/sdb
# Add second disk to existing VG
sudo vgextend vg0 /dev/sdc
sudo vgdisplay vg0
sudo vgs
03
Create and Use Logical Volumes
Create logical volumes from the VG pool, format them, and mount.
sudo lvcreate -L 10G -n lv_data vg0
sudo mkfs.ext4 /dev/vg0/lv_data
sudo mkdir /data
sudo mount /dev/vg0/lv_data /data
# Persist in fstab
echo '/dev/vg0/lv_data /data ext4 defaults 0 2' | sudo tee -a /etc/fstab
04
Extend Online (Zero Downtime)
Grow a filesystem while it is mounted and in use.
# Extend LV by 5GB
sudo lvextend -L +5G /dev/vg0/lv_data
# Grow filesystem to fill LV
sudo resize2fs /dev/vg0/lv_data # ext4
# For XFS:
sudo xfs_growfs /data
# Verify
df -h /data
Challenges & Solutions
- resize2fs only works on ext2/3/4 — XFS uses xfs_growfs
- Shrinking LVM requires unmounting and offline resize2fs — risky without backup
Key Takeaways
- LVM snapshots use copy-on-write — perfect for pre-maintenance backups
- PE size determines minimum allocation granularity