Login

Navigation

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

lvm.png

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

TaskPV PhaseVG PhaseLV Phasefilesystem (XFS)filesystem (EXT4)
Scanpvscanvgscanlvscanlsblk, blkidlsblk, blkid
Createpvcreatevgcreatelvcreatemkfs.xfsmkfs.ext4
Displaypvdisplayvgdisplaylvdisplaydf, mountdf, mount
Extend vgextendlvextend (lvresize)xfs_growfsresize2fs
Reduce vgreducelvreduce (lvresize)Not supportedresize2fs
Removepvremovevgremovelvremoveumount, reformattedumount, reformatted
Change the capacity (resize) lvresizexfs_growfsresize2fs
Change attributepvchangevgchangelvchange/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).

2022-12-12_144418.png

  1. Creating Physical Volumes (PV)
Use the pvcreate 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

2022-12-12_144524.png

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
  1. Creating Volume Groups (VG)
Volume groups are created using the vgcreate command.

2022-12-12_144621.png

sudo vgcreate volume.group /dev/sd[b-c]
volume.group is VG name.

Listing the Volume Groups (VG)

sudo vgscan

Or

sudo vgdisplay
  1. Creating Logical Volumes (LV)
Logical volumes are created using the lvcreate command.

2022-12-12_144735.png

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
  1. Create a filesystem on logical volumes
s.g. Format it with ext4,

2022-12-12_145011.png

sudo mkfs.ext4 /dev/volume.group/logical.volume

Create the mount point

2022-12-12_145050.png

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.

2022-12-12_145738.png

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

  1. Physical Volumes (PV)
Removing a physical volume
You can remove a physical volume with the pvremove command.
pvremove <pvname>
  1. Volume Groups (VG)

Extending a volume group

Extending a volume group (VG) means adding additional physical volumes (PV) to a volume group (VG). The vgextend command is used.
2022-12-12_145839.png

2022-12-12_145949.png

2022-12-12_150303.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>
  1. Logical Volumes (LV)

Resizing a logical volume

To extend a logical volume using lvextend command and reduce its size using lvreduce command.
Or you can use the single command lvresize 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.


Reference: