Logical Volume Manager

Pankaj kushwaha
8 min readJun 20, 2020

LVM is the abbreviation of Logical Volume Manager . It is an abstraction layer built on top of physical storage devices, allowing you to generate logical storage volumes. It provides better flexibility than direct physical storage management.

LVM virtualizes storage and uses logical volumes.You will not be limited by the size of the physical disk.In addition, the storage settings related to the hardware are hidden by it.You can adjust the volume size or data migration without stopping applications or uninstalling the file system. This can reduce operating costs.

When planning the partition size of a disk, sometimes it is often impossible to determine the total space used by this partition. After partitioning the disk with fdisk, the size of each partition is fixed. If the partition is set too large, the disk space will be wasted; if the partition is set too small, it will cause insufficient space. At this time, LVM (Logical Volume Manager) can be used.

1. Introduction to LVM
LVM is short for Logical Volume Manager. LVM connects several disks or disk partitions into a whole volume group to form a storage pool. The administrator can arbitrarily create logical volumes on the volume group, and further create file systems on the logical volumes. Administrators can easily adjust the size of storage volume groups through LVM, and can name, manage, and allocate disk storage in groups.
Assuming that there are three disks /dev/sdb, /dev/sdc, and /dev/sdd used to divide logical volumes, the LVM model is shown in the figure:

2. Basic terminology of LVM
Through the LVM technology, the underlying differences of the disk partitions are shielded, and the file system is logically provided with the concept of a volume, and then the corresponding file systems are established on these volumes. Before understanding LVM, familiarize yourself with several commonly used terms in LVM.

( 1 ) Physical storage device ( The physical media ): refers to the storage device files of the system, such as: /dev/hda1, /dev/sda, etc.

( 2 ) Physical volume ( Physical volume ): PV for short , the physical volume can be the entire hard disk, hard disk partition, or a device that has the same function as the disk partition logically (such as RAID), which is the basic storage logical block of LVM, but and basic Comparison of physical storage media (such as partitions, disks, etc.), but contains management parameters related to LVM.

( 3 ) Volume Group ( Volume Group ) : referred to as VG , can be regarded as a separate logical disk, built on top of PV, a volume group must include at least one PV, after the volume group is established, you can dynamically add PV to the volume group in. The name of the volume group can be customized.

( 4 ) PE ( physical extent ) physical area : The physical area is the smallest storage unit available for allocation in the physical volume, and the size of the physical area is 4MB by default. Once the physical area size is determined, it cannot be changed. The physical area size of all physical volumes in the same volume group needs to be consistent.

( 5 ) Logical volume ( logical volume ) : LV for short , equivalent to physical partition. The logical volume is built on the volume group. The unallocated space in the volume group can be used to create a new logical volume. After the logical volume is created, the space can be dynamically expanded or reduced. Multiple logical volumes in the system can belong to the same volume group or different multiple volume groups.

( 6 ) LE ( logical exten t ) logical area: The logical area is the smallest storage unit available for allocation in the logical volume. The size of the logical area depends on the size of the physical area in the volume group where the logical volume is located. The size of LE is the same as the size of PE.

( 7 ) VGDA ( Volume Group Descriptor Area ) volume group descriptor area : the same as non-LVM systems that store metadata containing partition information in the partition table at the beginning of the partition, logical volume and volume group related metadata It is also stored in the volume group descriptor area at the beginning of the physical volume. VGDA includes the following: PV descriptor, VG descriptor, LV descriptor and some PE descriptors.
Third, install LVM
First determine whether the lvm tool is installed in the system:
[root@pankajconnect ~]# rpm -qa | grep lvm
lvm2–2.02.130–5.el7.x86_64
mesa-private-llvm-3.6.2–2.el7.x86_64
lvm2-libs-2.02.130–5.el7.x86_64
The result shows that the system has installed the LVM management tool; if the command does not output, it means that the LVM management tool is not installed. You need to download it from the Internet or install the LVM rpm toolkit from the CD.
Fourth, create and manage logical volumes

Common commands

4.1 The logical volume creation process:
(1) Create a physical partition
Before using LVM, you need to divide the disk partition, that is, use the fdisk command to divide the disk partition. However, when creating a partition, you need to specify the partition type as linux LVM, and the corresponding ID is 8e (in fact, LVM can also recognize the default partition type of linux 83).
Add three 5G disks, the first disk partition method is: 2G main partition and 3G logical partition; the second disk partition method is: two 2G logical partitions; the third disk Not partitioned.

[root@pankajconnect ~]# fdisk -l /dev/sdb

[root@pankajconnect ~]# fdisk -l /dev/sdc

[root@pankajconnect ~]# partprobe
(2) Create a physical volume (pv)
[root@pankajconnect ~]# pvcreate /dev/sdb1 /dev/sdb5 /dev/sdc5 /dev/sdc6 /dev/sdd
[root@pankajconnect ~]# pvdisplay
[root@pankajconnect ~]# pvscan
[root@pankajconnect ~]# pvs

(3) Create a volume group
Add physical volumes /dev/sdb1, /dev/sdb5 and /dev/sdc5 to volume group vg1; add physical volumes /dev/sdc6, /dev/sdd to volume group vg2, the PE size of the volume group is 8MB
[root@pankajconnect ~]# vgcreate vg1 /dev/sdb1 /dev/sdb5 /dev/sdc5
[root@pankajconnect ~]# vgcreate -s 8M vg2 /dev/sdc6 /dev/sdd
[root@pankajconnect ~]# vgdisplay
[root@pankajconnect ~]# vgscan
[root@pankajconnect ~]# vgs

(4) Create a logical volume
The name of the first logical volume is lv1, the volume group belongs to vg1, and the size is 6G; the name of the second logical volume is lv2, the volume group belongs to vg2, and the size is 5G; the name of the third logical volume is lv3, The volume group belongs to vg2, and the logical volume consists of 100 PEs.
[root@pankajconnect ~]# lvcreate -L 6G -n lv1 vg1
[root@pankajconnect ~]# lvcreate -L 5G -n lv2 vg2
[root@pankajconnect ~]# lvcreate -l 100 -n lv3 vg2
[root@pankajconnect ~]# lvdisplay
[root@pankajconnect ~]# lvscan
[root@pankajconnect ~]# lvs

(5) Format the logical volume, create a file system, and mount
Format the newly created logical volumes lv1 and lv2 as ext4 file system and lv3 as xfs file system. The three logical volumes can be automatically mounted in /logical/lv1,/logical/lv2,/logical/lv3 table of Contents.
[root@pankajconnect ~]# mkfs.ext4 /dev/vg1/lv1
[root@pankajconnect ~]# mkfs.ext4 /dev/vg2/lv2
[root@pankajconnect ~]# mkfs.xfs /dev/vg2/lv3
[root@pankajconnect ~]# mkdir -p /logical/{lv1,lv2,lv3}
[root@pankajconnect ~]# blkid
[root@pankajconnect ~]# vim /etc/fstab
UUID=c47eb7a0–013d-4dc2–8b6c-4441186583e7 /logical/lv1 ext4 defaults 0 0
UUID=1f7f9b71-c99f-41fe-b647–25f3a18e875f /logical/lv2 ext4 defaults 0 0
UUID=baa50c7f-de65–4baa-8bd9-a89f611fd9f3 /logical/lv3 xfs defaults 0 0
[root@pankajconnect ~]# mount -a
[root@pankajconnect ~]# df -h

4.2 Modify the size of the logical volume:
To expand, you need to add a new physical volume to the volume group where the logical volume is located (xfs file system only supports expanding logical volumes, and ext4 file system supports expanding and shrinking logical volumes).

( 1 ) Shrink the logical volume of ext4 file system type: Suppose the reduced logical volume lv1 is 3G
Unmount the logical volume first
[root@pankajconnect ~]# umount /logical/lv1/
If it cannot be uninstalled (showing that the device is busy), a process is occupying the mount point directory, use the following command to terminate the occupying process: fuser -m -k mount point directory
[root@pankajconnect ~]# e2fsck -f /dev/vg1/lv1
[root@pankajconnect ~]# resize2fs /dev/vg1/lv1 3G
[root@pankajconnect ~]# lvreduce -L 3G /dev/vg1/lv1 or [root@pankajconnect ~]# lvresize -L 3G /dev/vg1/lv1
[root@pankajconnect ~]# mount -a
[root@pankajconnect ~]# df -h

( 2 ) Expand the logical volume of the ext4 file system type : Assume that the expanded logical volume lv2 is 6G
First you have to check whether the remaining volume group capacity is enough for the expansion of the logical volume
[root@pankajconnect ~]# vgs
Can not uninstall
[root@pankajconnect ~]# lvextend -L 6G /dev/vg2/lv2
[root@pankajconnect ~]# resize2fs /dev/vg2/lv2

( 3 ) Shrink and expand the logical volume of ext4 file system
The command lvresize can be used for both expansion and contraction. At the same time, the -r option of lvresize can be used to execute resize2fs when executing lvresize.
Assuming that lv1 is reduced to 2G
[root@pankajconnect ~]# lvresize -L 2G -r /dev/vg1/lv1
Do you want to unmount “/logical/lv1”? [Y|n] y

( 4 ) Expand the logical volume of the xfs file system
Assume that the size of the expanded lv3 is 1600M
First you have to check whether the remaining volume group capacity is enough for the expansion of the logical volume
[root@pankajconnect ~]# vgs

After checking, it is found that the capacity of volume group vg2 of lv3 is not enough, so you must first expand the capacity of the volume group
[root@pankajconnect ~]# fdisk /dev/sdc
[root@pankajconnect ~]# pvcreate /dev/sdc7
[root@pankajconnect ~]# vgextend vg2 /dev/sdc7
[root@pankajconnect ~]# vgs
[root@pankajconnect ~]# lvextend -L 1600M /dev/vg2/lv3
[root@pankajconnect ~]# xfs_growfs /dev/vg2/lv3

( 5 ) Suppose a disk or partition is damaged
Suppose you want to delete /dev/sdd from the volume group, first check whether there is extra space in the volume group where the disk is located

[root@pankajconnect ~]# vgs
If there is no extra space in the volume group where the damaged disk is located, prepare a partition or disk of the same size as the disk. Here, it is assumed that there is a partition /dev/sda5 with a partition size of 5G

[root@pankajconnect ~]# pvcreate /dev/sda5
[root@pankajconnect ~]# vgextend vg2 /dev/sda5
[root@pankajconnect ~]# pvmove /dev/sdd /dev/sda5 Move all PEs in /dev/sdd to /dev/sda5
[root@pankajconnect ~]# vgreduce vg2 /dev/sdd

4.3 Delete physical volumes, volume groups and logical volumes
Delete the logical volume first, then delete the volume group, and finally delete the physical volume. At the same time of deletion, the relevant mounting information in /etc/fstab needs to be deleted or commented, otherwise the system will not start normally

[root@pankajconnect ~]# umount /logical/{lv1,lv2,lv3}
[root@pankajconnect ~]# lvremove /dev/vg1/lv1 /dev/vg2/lv2 /dev/vg2/lv3
[root@pankajconnect ~]# vgremove vg1 vg2
[root@pankajconnect ~]# pvremove /dev/sda5

5. 1. Basic steps for using LVM
(1) Create a physical volume (PV) on a physical partition (PP)
pvcreate /dev/sda1 //Turn the traditional partition into a physical volume in LVM

(2) Create a volume group (VG)
vgcreate vg /dev/sda1 //Add the physical volume created in (1) to the volume group vg to complete the volume group creation

(3) Allocate space from VG and create logical volume (LV)
lvcreate -L 20G -n data vg //Create a logical volume partition with volume 20G and name data from volume group vg

(4) Format the newly created LV to a certain file system type

mke2fs -j /dev/vg/data //Format LV to ext2 file system type
(5) Mount LV

mount -t reiserfs /dev/vg/data /mnt/
Since then, the newly created LV is available.

Thanks for reading.

--

--

Pankaj kushwaha

Database/System Administrator | DevOPS | Cloud Specialist | DevOPS