Lesson 20 of 29 ↓
- 1.Rhel 9 Administration Guide
- 2.Ubuntu Boot Process Guide
- 3.Linux Basic Commands Guide
- 4.Comprehensive Unix Shells Guide
- 5.Mastering Vim Deep & Practical Guide
- 6.Magic Numbers in Linux Deep Systems Guide
- 7.Wildcards & Globbing in Linux Deep Cli Guide
- 8.Linux Users & Groups Deep Administration Guide
- 9.Journaling in Linux Filesystems Deep Systems Guide
- 10.Getopt & Getoptlong Deep System-level Option Parsing Guide
- 11.Su Command in Linux Complete Guide with Environment Variables
- 12.Linux File Permissions and Ownership Complete Practical Guide
- 13.Ubuntu Package Management Apt Complete Guide
- 14.Bash Scripting Basics
- 15.Linux File System Essentials
- 16.Process and Thread Management
- 17.Session 10 : Introduction to Networking & the Application Layer
- 18.Linux Networking Cli & Introduction to Yocto for Machine Routing
- 19.Comprehensive Guide to Ssh (secure Shell)
- 20.Logical Volume Management (lvm) Guide
- 21.C Build Process & Libraries Guide
- 22.Archiving and Compression in Linux
- 23.Linux Package Managers: a Detailed Guide
- 24.Misra-c Guidelines Detailed Cheat Sheet
- 25.Mqtt Server Setup (pc as Server)
- 26.Detailed Explanation of Tty and Pts in Linux
- 27.Vim Cheat Sheet
- 28.Difference Between apt and apt-get
- 29.Difference Between apt Upgrade and apt Full-upgrade
3 min read
Logical Volume Management (lvm) Guide
Logical Volume Management (LVM) provides a flexible way to manage disk space. It allows you to create virtual partitions that can be easily resized, even across multiple physical drives.
Physical Volumes (pv)
Physical volumes are the raw building blocks (partitions or full disks) that LVM uses.
Create a Physical Volume
Initialize a partition or disk for LVM use.
pvcreate /dev/nvme1n1p1Display Physical Volumes
Check the status and capacity of your physical volumes.
# Brief summary
pvs
# Detailed information
pvdisplayVolume Groups (vg)
A Volume Group is a pool of storage created by combining one or more Physical Volumes.
Create a Volume Group
Combine multiple physical partitions into a single pool named myvg.
vgcreate myvg /dev/nvme1n1p1 /dev/nvme1n1p2Display Volume Groups
# Brief summary
vgs
# Detailed information
vgdisplayLogical Volumes (lv)
Logical Volumes are the "virtual partitions" created from a Volume Group. You format these with a filesystem and mount them.
Create a Logical Volume
Create a volume named mylvm with a size of 2GB.
# -L: size (e.g., 2G, 500M)
# -n: name of the logical volume
lvcreate -L 2G -n mylvm myvgFormat the Volume
Before using the volume, create a filesystem (e.g., ext4).
mkfs.ext4 /dev/myvg/mylvmMount the Volume
Map the volume to a directory in your system.
# Check block devices
lsblk
# Create mount point and mount
mkdir /data
mount /dev/myvg/mylvm /data/Extending Storage
One of the main benefits of LVM is the ability to grow storage on the fly.
Step A: Prepare New Physical Storage
If you run out of space in the Volume Group, initialize a new partition.
pvcreate /dev/nvme1n1p3Step B: Extend the Volume Group
Add the new physical volume to your existing pool.
vgextend myvg /dev/nvme1n1p3Step C: Extend the Logical Volume
Increase the size of the logical volume.
# Add 2GB to the current size
lvextend -L +2G /dev/myvg/mylvmStep D: Resize the Filesystem
The LV is now larger, but the filesystem (ext4) needs to be told to fill that new space.
# Check for errors first
e2fsck -f /dev/myvg/mylvm
# Resize to fill the new LV capacity
resize2fs /dev/myvg/mylvmShrinking Storage
Warning: Shrinking a volume is risky. Always back up your data first. You must shrink the filesystem before reducing the Logical Volume size.
Step A: Unmount the Volume
You cannot safely shrink most filesystems while they are mounted.
umount /dataStep B: Check and Resize Filesystem
Reduce the filesystem to the target size (e.g., 2GB).
e2fsck -f /dev/myvg/mylvm
resize2fs /dev/myvg/mylvm 2GStep C: Reduce the Logical Volume
Now that the filesystem is small, you can reduce the LV container.
lvreduce -L 2G /dev/myvg/mylvmStep D: Re-mount and Verify
mount /dev/myvg/mylvm /data/
df -h /dataSummary of Useful Commands
| Level | Create | View | Extend |
|---|---|---|---|
| Physical (PV) | pvcreate |
pvs |
N/A |
| Group (VG) | vgcreate |
vgs |
vgextend |
| Logical (LV) | lvcreate |
lvs |
lvextend |
Comments
Loading the discussion…