What is LVM
LVM (Logical Volume Manager) is a Linux mechanism for managing disk partitions. It inserts a logical layer between physical disks and file systems, so disk space can be allocated and adjusted more flexibly.
Typical LVM workflow:
- Initialize disks as physical volumes (PV)
- Combine PVs into a volume group (VG)
- Create logical volumes (LV) inside the VG
- Create file systems on LVs
- Mount and use
With LVM, a file system can span multiple disks and be resized dynamically.
Core LVM Concepts
- Physical media: underlying storage devices such as
/dev/sda, /dev/nvme0n1.
- Physical Volume (PV): a disk/partition initialized for LVM.
- Volume Group (VG): a storage pool made of one or more PVs.
- Logical Volume (LV): allocatable virtual partition created from a VG.
- Physical Extent (PE): fixed-size allocation unit in a VG.
- Logical Extent (LE): allocation unit used by an LV (mapped from PE).
Install
1
2
|
sudo apt update
sudo apt install lvm2
|
Manage PV
Create PV
1
2
3
4
|
pvcreate [option] devname ...
# Example: initialize /dev/sdb and /dev/sdc as PV
pvcreate /dev/sdb /dev/sdc
|
View PV
1
2
3
4
|
pvdisplay [option] devname
# Example
pvdisplay /dev/sdb
|
Remove PV
1
2
3
4
|
pvremove [option] pvname ...
# Example
pvremove /dev/sdb
|
Manage VG
Create VG
1
2
3
4
|
vgcreate [option] vgname pvname ...
# Example: create vg1 with /dev/sdb and /dev/sdc
vgcreate vg1 /dev/sdb /dev/sdc
|
View VG
1
2
3
4
|
vgdisplay [option] [vgname]
# Example
vgdisplay vg1
|
Extend VG
1
2
3
4
|
vgextend [option] vgname pvname ...
# Example
vgextend vg1 /dev/sdb
|
Reduce VG
1
2
3
4
|
vgreduce [option] vgname pvname ...
# Example
vgreduce vg1 /dev/sdb2
|
Remove VG
1
2
3
4
|
vgremove [option] vgname
# Example
vgremove vg1
|
Manage LV
Create LV
1
2
3
4
5
6
7
|
lvcreate [option] vgname
# Example 1: create a 10G LV in vg1
lvcreate -L 10G vg1
# Example 2: create a 200M LV named lv1 in vg1
lvcreate -L 200M -n lv1 vg1
|
View LV
1
2
3
4
|
lvdisplay [option] [lvname]
# Example
lvdisplay /dev/vg1/lv1
|
Resize LV
1
2
3
4
5
6
7
8
9
10
|
lvresize [option] lvname
# Add 200M
lvresize -L +200M /dev/vg1/lv1
# Reduce 200M
lvresize -L -200M /dev/vg1/lv1
# Use all free space in VG
lvresize -l +100%FREE /dev/vg1/lv1
|
Extend LV
1
2
3
4
5
6
7
|
lvextend [option] lvname
# Add 100M
lvextend -L +100M /dev/vg1/lv1
# Use all free space
lvextend -l +100%FREE /dev/vg1/lv1
|
Reduce LV
1
2
3
4
|
lvreduce [option] lvname
# Reduce 100M
lvreduce -L -100M /dev/vg1/lv1
|
Remove LV
1
2
3
4
|
lvremove [option] lvname
# Example
lvremove /dev/vg1/lv1
|
Create and Mount File System
Create file system
1
2
3
4
|
mkfs [option] lvname
# Example: create ext4 on LV
mkfs -t ext4 /dev/vg1/lv1
|
Mount manually
1
2
3
4
|
mount lvname mntpath
# Example
mount /dev/vg1/lv1 /mnt/data
|
Resize File System After LV Resize
After extending an LV, the file system size does not grow automatically in many cases. For ext4:
1
2
3
4
5
6
7
8
|
# Unmount first (recommended for safety)
umount /dev/vg1/lv1
# Check and repair file system
e2fsck -f /dev/vg1/lv1
# Resize file system to fill LV
resize2fs /dev/vg1/lv1
|
In some scenarios, online resize is also possible: