Master LVM: A Complete Step‑by‑Step Guide to Linux Logical Volume Management
This comprehensive tutorial explains LVM concepts, including physical volumes, volume groups, logical volumes, snapshots, and detailed procedures for creating, extending, shrinking, moving, and removing volumes on Linux, complete with command examples and illustrative screenshots.
Overview
LVM (Logical Volume Manager) provides flexible file‑system capacity management by aggregating multiple physical partitions into a single storage pool that can be dynamically resized.
LVM has two versions: lvm and lvm2.
LVM Concepts
1. Physical Volume (PV)
A PV is the lowest‑level element in LVM, representing a physical partition.
2. Volume Group (VG)
Combining multiple PVs creates a VG, whose size equals the total space of its PVs.
3. Logical Volume (LV)
An LV is the usable storage unit that can be formatted, mounted, and holds data; it can be resized flexibly.
4. Physical Extent (PE)
A PE is the smallest allocation unit in LVM, defaulting to 4 MiB; LV size is defined by the number of PEs.
5. Snapshot
A snapshot captures the state of an LV at a point in time, storing only changes made after its creation.
Practical LVM Operations
1. Create PV
First create partitions and set their type to 8e.
Four partitions are created as four PVs; use pvs to view them.
2. Create VG
Two PVs are combined into a VG named "testvg"; vgdisplay shows details.
When creating a VG, the PE size can be specified.
3. Create LV
First view available VGs.
Two VGs of 5 GiB and 2 GiB are shown; LVs are created on each.
Parameters:
-L: specify LV size; -n: LV name; -l: number of PEs.
Creating an LV with 40 PEs (16 MiB each) yields a 640 MiB LV.
4. Mount LV
LVs can be accessed via two symbolic links:
a) /dev/VG_NAME/LV_NAME (e.g., /dev/testvg/tlv)
b) /dev/mapper/VG_NAME-LV_NAME (e.g., /dev/mapper/testvg_tlv)
5. Extend LV
Steps: determine target size, ensure VG space, use lvextend, then resize filesystem with resize2fs.
Example: extend LV tlv to 1 GiB.
Parameters: -L [+]size or -l [+]PEs.
Resize filesystem to 1 GiB:
If the VG is full, add a new PV with vgextend before extending.
6. Reduce LV
Steps: determine target size, unmount LV, run e2fsck -f, shrink filesystem with resize2fs, then reduce LV with lvreduce.
Example: shrink LV tlv to 1 GiB.
Note: lvresize can also be used for both extending and shrinking.
7. Remove Volumes
Remove LV, then VG, then PV using lvremove, vgremove, and pvremove respectively.
To remove a specific PV from a VG, use pvmove to migrate its PEs before removal.
Example: moving PV /dev/sdd1 from VG test1vg without affecting LV t1lv.
Attempting to remove the PV shows it is in use; view its PE allocation.
Move the occupied PEs to another PV, then remove /dev/sdd1.
Command summary:
PV: pvcreate, pvs, pvdisplay, pvremove, pvmove, pvscan VG:
vgcreate, vgs, vgdisplay, vgremove, vgscan, vgextend, vgreduceLV:
lvcreate, lvs, lvdisplay, lvremove, lvextend, lvreduce, lvresize, lvscanCreate Snapshot Volume
Creating a snapshot uses the -s option with lvcreate targeting the source LV.
The snapshot is identical to the original LV; its usage can be inspected.
Modifying the original LV automatically backs up changes to the snapshot; restoring simply copies data from the snapshot.
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.
