Mastering LVM on Linux: From Basics to Advanced Operations
This comprehensive guide explains what LVM is, its core concepts, installation steps, common commands, how to create and manage physical, volume and logical volumes, extend and shrink volumes, handle snapshots, and migrate volume groups across machines, all illustrated with practical command‑line examples.
What is LVM
LVM is the Logical Volume Manager for Linux operating systems. There are two versions, LVM1 (a mature, stable product) and LVM2 (the latest and best, almost fully backward compatible except for snapshots, which must be removed before upgrading to LVM2).
LVM Overview
Logical volume management provides a higher‑level view of disk storage than traditional disks and partitions, allowing administrators to allocate storage to applications and users more flexibly.
Volumes created under LVM can be resized and moved at will, although file‑system tools may need to be updated.
LVM also lets administrators group physical volumes into named volume groups (e.g., "development" or "sales") instead of using raw device names like sda and sdb.
Basic Terminology
Volume Group (VG)
A VG is the highest‑level abstraction in LVM, aggregating logical volumes and physical volumes into a single management unit.
Physical Volume (PV)
A PV is usually a whole disk or a partition, though it can also be a device that appears like a disk (e.g., a software RAID device).
Logical Volume (LV)
An LV is analogous to a partition in non‑LVM systems. It appears as a standard block device and can host a file system (e.g., /home).
Physical Extent (PE)
Each PV is divided into fixed‑size blocks called physical extents, which match the size of logical extents in the VG.
Logical Extent (LE)
Each LV is divided into logical extents; all LEs in a VG share the same size.
Mapping Modes
Linear mapping: Assign a range of PEs to a contiguous region of an LV (e.g., LE 1‑99 map to PV1, LE 100‑347 map to PV2).
Striped mapping: Interleave extents across multiple PVs to improve performance.
Snapshots
Snapshots create a read‑only copy of an LV at a point in time using copy‑on‑write. Metadata is stored initially; actual data blocks are copied only when the source block changes. Snapshot size grows as more source blocks are modified and must be sized appropriately to avoid overflow.
Installing LVM
# Check if lvm2 is installed (CentOS 7 example)
rpm -q lvm2
# Install if missing
yum -y install lvm2Common Commands Overview
# PV related commands (tab‑completion)
pvchange pvck pvcreate pvdisplay pvmove pvremove pvresize pvs pvscan
# VG related commands
vgcfgbackup vgck vgdisplay vgimport vgmknodes vgreduce vgremove vgscan
vgcfgrestore vgconvert vgexport vgimportclone vgsplit vgs vgchange vgcreate vgextend vgmerge vgremove vgscan
# LV related commands
lvchange lvdisplay lvmconf lvmdump lvmsadc lvremove lvs
lvconvert lvextend lvmconfig lvmetad lvmsar lvrename lvscan
lvcreate lvm lvmdiskscan lvmpolld lvreduce lvresizeKey prefixes indicate the object type: pv for physical volumes, vg for volume groups, and lv for logical volumes.
Using LVM
Creating a PV
Initialize a disk or partition for use as a PV. Do not initialize an entire disk if you need other OSes to recognize it.
# Initialize /dev/sdb1 as a PV
pvcreate /dev/sdb1When using a partition, set its type to 8e (Linux LVM) with fdisk or parted.
Viewing PV Information
# Show concise PV info
pvs
# Detailed info
pvdisplayCreating a VG
# Create VG "vg_test_01" from four PVs
vgcreate vg_test_01 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1Viewing VG Information
# Short summary
vgs
# Detailed view
vgdisplayActivating / Deactivating a VG
# Activate (make LV devices available)
vgchange -a y vg_test_01
# Deactivate
vgchange -a n vg_test_01Adding and Removing PVs from a VG
# Add a new PV to the VG
vgextend vg_test_01 /dev/sdb2
# Remove a PV (must be empty first)
vgreduce vg_test_01 /dev/sdb2
pvremove /dev/sdb2Creating an LV
# Create a 2 GiB LV named lv_test_01
lvcreate -L 2G -n lv_test_01 vg_test_01Removing an LV
# Remove the LV (confirm if active)
lvremove /dev/vg_test_01/lv_test_01Formatting and Mounting
# Create an ext4 file system on the LV
mkfs.ext4 /dev/vg_test_01/lv_test_01
# Mount point
mkdir /mnt/test_01
mount /dev/vg_test_01/lv_test_01 /mnt/test_01Extending an LV
# Add 1 GiB to the LV
lvextend -L +1G /dev/vg_test_01/lv_test_01
# Resize the file system to match
resize2fs /dev/vg_test_01/lv_test_01Reducing an LV
Before shrinking, unmount the LV, run e2fsck -f, resize the file system, then reduce the LV.
# Unmount
umount /dev/vg_test_01/lv_test_01
# Check file system
e2fsck -f /dev/vg_test_01/lv_test_01
# Shrink file system to 1 GiB
resize2fs /dev/vg_test_01/lv_test_01 1G
# Reduce LV size
lvreduce -L 1G /dev/vg_test_01/lv_test_01
# Remount
mount /dev/vg_test_01/lv_test_01 /mnt/test_01Migrating a VG to Another Machine
Analyze current layout.
Unmount file systems and deactivate the VG.
Move data off the PVs you plan to remove using pvmove.
Reduce the VG to keep only the PVs you will transport.
Export the VG with vgexport.
Physically move the remaining disk(s) to the new host.
On the new host, run vgscan, pvscan, then vgimport.
Activate the VG and mount the LV(s>.
Creating and Using Snapshots
# Create a 500 MiB read‑only snapshot of lv_test_01
lvcreate -L 500M -s -p r -n vg_test_01_snapshot /dev/vg_test_01/lv_test_01Mount the snapshot read‑only, back up the data, then remove the snapshot when finished.
# Mount snapshot
mkdir /mnt/snap
mount /dev/vg_test_01/vg_test_01_snapshot /mnt/snap
# Backup
tar -jcvf /root/snap_backup.tar.bz2 /mnt/snap
# Unmount and delete snapshot
umount /mnt/snap
lvremove /dev/vg_test_01/vg_test_01_snapshotReference Image
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
