LVM in Linux
LVM stands for Logical Volume Management.
LVM is a tool for logical volume management which includes allocating disks, striping, mirroring, and resizing logical volumes.
LVM Advantage
Users can dynamically add or expand the capacity on demand to deploy storage for intended applications.
Components of LVM
Picture fr Red Hat Customer Portal
There are three main components to LVM:
- Physical Volumes (PV)
- Volume Groups (VG)
- Logical Volumes (LV)
Install LVM on Linux
To install LVM on Ubuntu, Debian, and Linux Mint:
sudo apt install lvm2
To install LVM on CentOS, Fedora, AlmaLinux, and Red Hat:
sudo dnf install lvm2
To install LVM on Arch Linux and Manjaro:
sudo pacman -S lvm2
Using LVM
Task | PV Phase | VG Phase | LV Phase | filesystem (XFS) | filesystem (EXT4) |
---|---|---|---|---|---|
Scan | pvscan | vgscan | lvscan | lsblk, blkid | lsblk, blkid |
Create | pvcreate | vgcreate | lvcreate | mkfs.xfs | mkfs.ext4 |
Display | pvdisplay | vgdisplay | lvdisplay | df, mount | df, mount |
Extend | vgextend | lvextend (lvresize) | xfs_growfs | resize2fs | |
Reduce | vgreduce | lvreduce (lvresize) | Not supported | resize2fs | |
Remove | pvremove | vgremove | lvremove | umount, reformatted | umount, reformatted |
Change the capacity (resize) | lvresize | xfs_growfs | resize2fs | ||
Change attribute | pvchange | vgchange | lvchange | /etc/fstab, remount | /etc/fstab, remount |
Reference: Master Bird - Lesson 14: Advanced File System Management (vbird.org)
Use 2 disks, each with a capacity of about 16GB.
All disks are combinate in to one Volume Groups (VG)
Create a Logical Volumes (LV) name called "logical.volume" and use the entire capacity of Volume Groups (VG).
- Creating Physical Volumes (PV)
Use thepvcreate
command to create a Physical Volumes (PV). Partitioned or unpartitioned disks both can be created.
Both disks partition's names are/dev/sdb
and/dev/sdc
sudo pvcreate /dev/sd[b-c]
Physical volume "/dev/sdb" successfully created.
Physical volume "/dev/sdc" successfully created.
Listing the available Physical Volumes (PV)
sudo pvscan
Or
sudo pvdisplay
- Creating Volume Groups (VG)
Volume groups are created using the vgcreate
command.
sudo vgcreate volume.group /dev/sd[b-c]
volume.group is VG name.
Listing the Volume Groups (VG)
sudo vgscan
Or
sudo vgdisplay
- Creating Logical Volumes (LV)
Logical volumes are created using the lvcreate
command.
sudo lvcreate -L <size> -n <lvname> <vgname>
lvcreate -l 100%FREE -n logical.volume volume.group
-l 100%FREE means use entire capacity of volume.group
-n The name of LV is logical.volume
- Create a filesystem on logical volumes
s.g. Format it with ext4,
sudo mkfs.ext4 /dev/volume.group/logical.volume
Create the mount point
sudo mkdir -p /mnt/lv01
Mount it at current directory /mnt/lv01
sudo mount /dev/volume.group/logical.volume /mnt/lv01
Edit fstab
to automatically mount partitions after power-on or reboot in the future.
vim /etc/fstab
Or
echo "/dev/volume.group/logical.volume /mnt/lv01 ext4 default 0 0" >> /etc/fstab
Test the Mount Point of logical volumes
In order to use our new volumes.
mount -a
Modify LVM
- Physical Volumes (PV)
Removing a physical volume
You can remove a physical volume with thepvremove
command.
pvremove <pvname>
- Volume Groups (VG)
Extending a volume group
Extending a volume group (VG) means adding additional physical volumes (PV) to a volume group (VG). Thevgextend
command is used.2022-12-12_145839.png
sudo vgextend <vgname> /dev/sdd
Reducing a volume group
Use the vgreduce
command to do this.
vgreduce <vgname> <pvname1> <pvname2>....
Removing a volume group
Remove a logical volume with the vgremove command.
sudo vgremove <vgname>
- Logical Volumes (LV)
Resizing a logical volume
To extend a logical volume usinglvextend
command and reduce its size usinglvreduce
command.
Or you can use the single commandlvresize
to accomplish both tasks.2022-12-12_150622.png
lvresize -L [+|-][Size] <vgname>/<lvname>
After the volume size has increased, the filesystem must be resized as well. For ext4, the command to use is resize2fs
.
sudo resize2fs /dev/<vgname>/<lvname>
Removing a logical volume
You remove a logical volume with the lvremove command. The command syntax is as follows:-
lvremove <vgname>/<lvname>
Conclusion:
Many manufacturers now use LVM the default file system, such as PVE, Ubuntu, Synology, QNAS, etc.