What is LVM?
3 min readFeb 24, 2024
What is LVM? and How to use LVM?
In this article, we are going through the LVM, its features, and its configuration.
Logical Volume Manager (LVM)
- LVM is a tool to partition your file systems and manage them. You can have one partition on several disks.
- LVM is a solution for better managing servers with several Physical Hard Disks.
- In LVM, we have three objects: 1. Physical Volume (PV) 2. Group Volume (GV) 3. Logical Volume (LV)
- We see LV as a disk and mount it. Also, we can create GV from PVs and make different groups for different data.
- LVM features are: 1. Shared Partition 2. Extend LV (whenever needed).
- Linux modules have .ko extension and compile with C language.
- Linux modules path: /lib/modules/kernel-version
- Install LVM:
$ apt install lvm2
Physical Volume (PV) Administration
Initializing Physical Volume:
# Create PV on one disk
$ pvcreate /dev/sdb
# Even you can create PV on a created partition
$ pvcreate /dev/sdc1 /dev/sdd1
Display Physical Volumes:
$ pvs
or
$ pvdisplay
Scanning current system for physical volumes:
$ pvscan
Removing a physical volume:
$ pvremove /dev/sdc1
Volume Group (VG) Administration
Creating Volume Groups:
$ vgcreate vg1 /dev/sdc1 /dev/sdd1
Add/Remove a new PV to an existing VG:
# Add
$ vgextend vg1 /dev/sde
# Remove
$ vgreduce vg1 /dev/sdd1
Displaying Volume Groups:
$ vgs
or
$ vgdisplay
Scan existing Volume Groups:
$ vgscan
Removing a Volume Group:
$ vgremove vg1
Logical Volume (LV) Administration
Creating a Logical Volume:
$ lvcreate --name lv1 -L 10G vg1
Extend/Reduce a Logical Volume:
# Extend
$ lvextend -L +1G /dev/vg1/lv1
# To take effect, if it is mounted, unmount(umount) it and then:
$ fsck -f /dev/vg1/lv1
$ resize2fs /dev/vg1/lv1
# Now you can mount it again!
# Reduce
$ lvreduce -L -1G /dev/vg1/lv1
# To take effect, if it is mounted, unmount(umount) it and then:
$ fsck -f /dev/vg1/lv1
$ resize2fs /dev/vg1/lv1
# Now you can mount it again!
⚠️ Before unmounting, fsck, and resizing, backup your data, there is the possibility of losing your data!
Display/Remove a Logical Volume:
# Display
$ lvs
or
$ lvdisplay
# Scan
$ lvscan# Remove
$ lvremove /dev/vg1/lv1
LVM Snapshots
- Snapshot Volume is a volume that when you use it, freezes & backups LVM until that moment.
- You can give your desired size of backup to your snapshot.
Create Snapshot
# Creating snapshot "lvsnap1" with size 100M from lv1:
$ lvcreate --snapshot --size 100M --name lvsnap1 /dev/vg1/lv1
# The newly created snapshots can be mounted & used:
$ mkdire /backup
$ mount /dev/vg1/lvsnap1 /backup# Restore snapshot on the LVM again:
$ umount /dev/vg1/lvsnap1
$ mount /dev/vg1/lvsnap1 /mnt# Delete; first you should umount it then:
$ lvremove /dev/vg1/lvsnap1