What is LVM
The Logical Volume Manager (LVM) is a powerful storage management tool in Linux that provides an abstraction layer over physical storage devices. It allows for the dynamic allocation, resizing, and management of disk space without disrupting system operations.
Why use LVM
Using LVM, you can create storage spaces that can grow or shrink based on your needs, rather than being stuck with fixed partitions that are hard to resize or adjust. LVM helps make storage management flexible.
Architecture
LVM acts as the layer between the physical devices and the file system. It is structured in three layers:
Physical Volume (PV): A single disk or partition of a disk.
Volume Group (VG): A combination of multiple PVs to create a large storage pool, providing flexibility to allocate storage across multiple logical volumes.
Logical Volume (LV): Virtual partitions taken from the volume group for different purposes. They are flexible and can be resized or moved.
Key Components of LVM
Physical Volumes (PVs):
The physical storage devices (disks or partitions) are formatted for LVM use.
Serve as the foundation for storage in LVM.
Volume Groups (VGs):
Combine one or more physical volumes into a single storage pool.
Act as the primary space for creating logical volumes.
Logical Volumes (LVs):
Virtual storage partitions are created from volume groups.
It can be resized or moved dynamically and is used for file systems or raw data.
Physical Extents (PEs):
Fixed-size blocks of storage within physical volumes.
Form the building blocks for logical volumes.
The default size of PE is 4MiB.
Logical Extents (LEs):
Mapped directly to physical extents.
Define the storage units within logical volumes for flexible management.
The default size of LE is 4MiB.
Commands
Command | Description |
pvs | Display a summary of physical volumes. |
vgs | Display a summary of volume groups. |
lvs | Display a summary of logical volumes. |
pvdisplay | Show detailed information about a physical volume. |
vgdisplay | Show detailed information about a volume group. |
lvdisplay | Show detailed information about a logical volume. |
pvcreate | Create a new physical volume. |
vgcreate | Create a new volume group. |
lvcreate | Create a new logical volume. |
vgextend | Add a physical volume to an existing volume group. |
lvextend | Increase the size of a logical volume. |
lvreduce | Decrease the size of a logical volume. |
pvremove | Remove a physical volume from LVM management. |
vgremove | Remove a volume group. |
lvremove | Remove a logical volume. |
Steps for LVM Management
Add a Hard Disk
First, add a hard disk of SCSI type of a specific disk size and create
- Scan the hosts or reboot the system
echo "- - -" > /sys/class/scsi_host/host0/scan # scan all hosts in /sys/class/scsi_hosts
Verify that the disk is added
Creating Linux LVM
Partition the Disk
fdisk /dev/sdb
Creating Physical Volume
Create Physical Volume:
pvcreate /dev/sdb1
Display/Verify:
pvs pvdisplay
Creating Volume Group
Create Volume Group:
vgcreate vg001 /dev/sdb1
Verify:
vgs vgdisplay
Note: Without creating Physical Volume, you can directly create a Volume Group as they are almost identical (not recommended).
Creating Logical Volume
Create Logical Volume:
lvcreate -L 1GB -n lv001 vg001
-L
: Logical Volume Size-n
: Logical Volume Name
Verify:
lvs lvdisplay
Formatting Logical Volume
Format the Logical Volume:
mkfs.ext4 /dev/vg001/lv001
Verify with Partition List:
parted -l
Mounting the Logical Volume
Create a Mount Point:
cd / mkdir data
Edit
/etc/fstab
:/dev/vg001/lv001 /data ext4 defaults 0 0
Mount the Filesystem:
mount -a df -h
Verify with
lsblk
:lsblk
Extending a Logical Volume
Extend Logical Volume from 1GB to 2GB:
lvextend -L +1GB /dev/vg001/lv001
- This extra volume is taken from the volume group.
After extending the logical volume, the output of
df -h
will still not reflect the updated size. This is because the filesystem needs to be informed about the volume extension. Use theresize2fs
command to update the filesystem.
Update the Filesystem:
resize2fs /dev/vg001/lv001
Verify:
lvs vgs
You can see that the Logical Volume size is increased by 1GB and the Volume Group size is decreased by 1GB.
Reducing a Logical Volume
Take Downtime:
- Ensure no one is using the VG.
Unmount Logical Volume: Comment the line form
/etc/fstab
.umount /data
Organize Logical Volume Data:
Running
e2fsck
ensures the filesystem is consistent and free of errors before reducing a logical volume. This prevents data corruption during the resize processe2fsck -f /dev/vg001/lv001
-f
: Force checking even if the filesystem is marked clean.
Resize the Filesystem:
resize2fs /dev/vg001/lv001 1G
The 1G is the size we want of our logical volume after resizing.
Reduce the Logical Volume:
lvreduce -L -1G /dev/vg001/lv001
Re-mount the Logical Volume:
- Uncomment
/etc/fstab
entry.
- Uncomment
mount -a
Deleting Logical Volume
Delete or Comment
/etc/fstab
Entry.Unmount the Volume:
umount /data
Remove Logical Volume:
lvremove /dev/vg001/lv001
Deleting Volume Group
Remove the Volume Group:
vgremove vg001
Deleting Physical Volume
Remove the Physical Volume:
pvremove /dev/sdb1
Optimizing Space Usage with Physical Extents (PEs) and Logical Extents (LEs)
Create Physical Volume:
pvcreate /dev/sdb1
Create Volume Group Using PEs:
Calculate the required PE size. For example:
1 PE = 4MiB (default).
For 5GiB, calculate as 5×1024 = 5120 MiB.
vgcreate vg001 -s 5120 /dev/sdb1
-s
: Specifies the size of each physical extent.
Create Logical Volume Using LEs:
Logical Volume size calculation:
1 LE = 4MiB (default).
For a 1GiB Logical Volume, calculate as 1×1024 = 1024 LEs.
lvcreate -l 1024 -n lv001 vg001
-l
: Specifies the number of logical extents.
Format and Mount Logical Volume:
Format Logical Volume:
mkfs.ext4 /dev/vg001/lv001
Create Mount Point and Edit
/etc/fstab
:mkdir /data vim /etc/fstab
Add entry:
/dev/vg001/lv001 /data ext4 defaults 0 0
Mount the Filesystem:
mount -a df -h