Mastering LVM on Linux: Create, Extend, and Reduce Logical Volumes
This guide explains how to use Linux's Logical Volume Manager (LVM) to create physical volumes, volume groups, and logical volumes, then demonstrates expanding and shrinking a logical volume safely, including filesystem resizing and removing a physical volume from a volume group.
Introduction
LVM (Logical Volume Manager) is a Linux mechanism that adds a logical layer between physical disks/partitions and file systems, providing flexible volume management. Physical volumes (PVs) are disks or partitions, volume groups (VGs) aggregate PVs, and logical volumes (LVs) are created within VGs. Physical Extents (PE) and Logical Extents (LE) are the smallest addressable units, typically 4 MiB.
Experiment
Creating Logical Volumes
1. Create two partitions: sda5 (5 GiB) and sda6 (10 GiB). Example output of fdisk -l is shown.
2. Initialize sda6 as a physical volume: pvcreate /dev/sda6 3. Create volume group vg6 with 16 MiB PE size: vgcreate -s 16M vg6 /dev/sda6 4. Create a 6 GiB logical volume named ftpdata: lvcreate -L 6G -n ftpdata vg6 5. Format the LV with ext4 and mount it, then copy some configuration files.
mke2fs -T ext4 /dev/vg6/ftpdata
mount /dev/vg6/ftpdata /ftpdata
cp /etc/*.conf .
cp /usr/share/dict/linux.words .Extending the Volume
1. Add the remaining space on sda5 to vg6: vgextend vg6 /dev/sda5 2. Extend ftpdata by 4 GiB or to a total of 10 GiB:
lvextend -L +4G /dev/vg6/ftpdata
lvextend -L 10G /dev/vg6/ftpdata3. Resize the filesystem to match the new size:
resize2fs /dev/vg6/ftpdataReducing the Volume
Reducing is more complex. First unmount the filesystem and run a filesystem check:
umount /ftpdata
e2fsck -f /dev/vg6/ftpdataDetermine the target size (e.g., 6 GiB) and test the reduction with -t: lvreduce -L 6000M /dev/vg6/ftpdata -t After confirming, deactivate the LV, perform the reduction, and reactivate it:
lvchange -an /dev/vg6/ftpdata
lvreduce -L 6000M /dev/vg6/ftpdata
lvchange -ay /dev/vg6/ftpdata
mount -aVerify the filesystem size with df -h and that data remains intact.
Removing a Physical Volume
Before removal, ensure the PV contains no data, move data to another PV, then remove it from the VG:
pvscan
pvmove /dev/sda6 /dev/sda7
vgchange -an vg5
vgreduce vg5 /dev/sda6All commands and outputs are illustrative; actual device names may vary.
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.
