LVM usage management and logical volume disk space expansion

 

Several concepts in LVM (Logical Partition Management):

PV (physical volume): The physical volume is at the bottom of the logical volume management system. It can be an entire physical hard disk or a partition on an actual physical hard disk.
VG (volume group): The volume group is built on a physical volume. A volume group must include at least one physical volume. After the volume group is created, the volume can be dynamically added to the volume group. There can be multiple volumes in a logical volume management system project. Volume group.
LV (logical volume): Logical volume is built on the basis of volume group. Unallocated space in the volume group can be used to create new logical volume. After logical volume is created, the space can be dynamically expanded and reduced.
PE (physical extent): The physical area is the smallest storage unit that can be used for allocation in a physical volume. The size of the physical area is specified when the volume group is created. Once it is determined that it cannot be changed, the physical area size of all physical volumes in the same volume group must be the same. After pv is added to vg, the size of pe is automatically changed to the pe size defined in vg.
LE (logical extent): The logical area is the smallest storage unit available for allocation in a 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.
Volume group description area: The volume group description area exists in each physical volume, and is used to describe all information such as the physical volume itself, the volume group to which the physical volume belongs, the logical volume in the volume group, and the allocation of physical areas in the logical volume. Created when using pvcreate to create a physical volume.
Write picture description here

1. Prepare physical partitions (Physical Partions)

First, we need to select the physical memory used for LVM. These are usually standard partitions, but they can also be created Linux Software RAID volumes. Here I use the fdisk command to divide the sdb and sdc disks into two areas sdb1 and sdc1, and specify the partition as 8e type (Linux LVM) through the t command of fdisk.

2. Create physical volume PV (Physical Volumes)

Physical Volumes (Physical Volumes) is abbreviated as PV, which is created on the physical partition of a disk or a device with the same function as the disk partition (such as RAID). It just marks a special area in the physical partition for recording management parameters related to LVM.
The command to create a physical volume is pvcreate:

[root@li2 ~]# pvcreate /dev/sdb1 
Physical volume "/dev/sdb1" successfully created 
[root@li2 ~]# pvcreate /dev/sdc1 
Physical volume "/dev/sdc1" successfully created

[root@li2 ~]# pvcreate /dev/sdb1 
Physical volume "/dev/sdb1" successfully created 
[root@li2 ~]# pvcreate /dev/sdc1 
Physical volume "/dev/sdc1" successfully created

The above commands respectively initialize /dev/sdc1 and /dev/sdd1 as physical volumes. Use the physical volume display command pvdisplay to view the physical volume as follows:

[root@li2 ~]# pvdisplay 
--- NEW Physical volume --- 
PV Name /dev/sdb1 
VG Name 
PV Size 36.00 GB 
Allocatable NO 
PE Size (KByte) 0 
Total PE 0 
Free PE 0 
Allocated PE 0 
PV UUID QDmnUd-tuvH-U4Hn-n5Ry-zGRT-O1yK-67Dxbb 
--- NEW Physical volume --- 
PV Name /dev/sdc1 
VG Name 
PV Size 36.00 GB 
Allocatable NO 
PE Size (KByte) 0 
Total PE 0 
Free PE 0 
Allocated PE 0 
PV UUID NDBf68-6qrD-9hE6-Rotv-RdxL-Azvv-7NlC0S

[root@li2 ~]# pvdisplay 
--- NEW Physical volume --- 
PV Name /dev/sdb1 
VG Name 
PV Size 36.00 GB 
Allocatable NO 
PE Size (KByte) 0 
Total PE 0 
Free PE 0 
Allocated PE 0 
PV UUID QDmnUd-tuvH-U4Hn-n5Ry-zGRT-O1yK-67Dxbb 
--- NEW Physical volume --- 
PV Name /dev/sdc1 
VG Name 
PV Size 36.00 GB 
Allocatable NO 
PE Size (KByte) 0 
Total PE 0 
Free PE 0 
Allocated PE 0 
PV UUID NDBf68-6qrD-9hE6-Rotv-RdxL-Azvv-7NlC0S

3. Create a volume group VG (Volume Groups)

Volume Group (Volume Group) is abbreviated as VG, which is a combination of one or more physical volumes. A volume group combines multiple physical volumes to form a manageable unit, which is similar to a physical hard disk in a non-LVM system.
The command to create a volume group is vgcreate. The following uses it to create a volume group named "lvmdisk", which contains two physical volumes /dev/sdb1 and /dev/sdc1.

[root@li2 ~]# vgcreate lvmdisk /dev/sdb1 /dev/sdc1 
Volume group "lvmdisk" successfully created 
  • 1 [root@li2 ~]# vgcreate lvmdisk /dev/sdb1 /dev/sdc1
  • 2 Volume group "lvmdisk" successfully created

Use the volume group view command vgdisplay to display the volume group status:

[root@li2 ~]# vgdisplay 
--- Volume group --- 
VG Name lvmdisk 
System ID 
Format lvm2 
Metadata Areas 2 
Metadata Sequence No 1 
VG Access read/write 
VG Status resizable 
MAX LV 0 
Cur LV 0 
Open LV 0 
Max PV 0 
Cur PV 2 
Act PV 2 
VG Size 71.98 GB 
PE Size 4.00 MB 
Total PE 18428 
Alloc PE / Size 0 / 0 
Free PE / Size 18428 / 71.98 GB 
VG UUID SARfuj-wAUI-od81-VWAc-A1nt-aaFN-JWaPVf
  • 1
  • 2
  •  

When multiple physical volumes are combined into a volume group, LVM will do a similar formatting work on all physical volumes, cutting each physical volume into a piece of space, and this piece of space is called PE ( Physical Extent), its default size is 4MB.
Due to the limitation of the kernel, a logical volume (Logic Volume) can only contain 65536 PEs (Physical Extent) at most, so the size of a PE determines the maximum capacity of the logical volume, and a PE of 4 MB determines the maximum size of a single logical volume. The capacity is 256 GB. If you want to use a logical volume larger than 256G, you need to specify a larger PE when creating a volume group. The PE size in Red Hat Enterprise Linux AS 4 ranges from 8 KB to 16 GB, and must always be a multiple of 2.

Note: The above restrictions on the number of PEs only exist in the lvm1 version. The restriction on the number of PEs has been removed in lvm2, that is, the maximum capacity of a single LV is not limited. In addition, systems after CentOS 6.X have directly used the format functions of lvm2, so there is no such restriction.

For example, if you want to use 64 MB of PE to create a volume group, so that the maximum capacity of the logical volume can be 4 TB, the command is as follows:

# vgcreate - 64MB lvmdisk /dev/sdb1 /dev/sdc1

[root@li2 ~]# vgdisplay 
--- Volume group --- 
VG Name lvmdisk 
System ID 
Format lvm2 
Metadata Areas 2 
Metadata Sequence No 1 
VG Access read/write 
VG Status resizable 
MAX LV 0 
Cur LV 0 
Open LV 0 
Max PV 0 
Cur PV 2 
Act PV 2 
VG Size 71.98 GB 
PE Size 4.00 MB 
Total PE 18428 
Alloc PE / Size 0 / 0 
Free PE / Size 18428 / 71.98 GB 
VG UUID SARfuj-wAUI-od81-VWAc-A1nt-aaFN-JWaPVf

4. Create logical volume LV (Logical Volumes)

Logical Volumes (Logical Volumes), referred to as LV, is a logical area divided in a volume group, similar to hard disk partitions in non-LVM systems.
The command to create a logical volume is lvcreate. Through the following command, we create a logical volume named pldy1 on the volume group lvmdisk, the size is 15GB, and its device entry is /dev/lvmdisk/pldy1.

[root@li2 dev]# lvcreate -L 15G -n pldy1 lvmdisk 
Logical volume "pldy1" created
  •  
  • 1 [root@li2 dev]# lvcreate -L 15G -n pldy1 lvmdisk
  • 2 Logical volume "pldy1" created

You can also use the -l parameter to set the logical partition size by specifying the number of PEs.
For example, if you want to create a logical volume that uses all the space, you need to check the total number of PEs in the volume group. The total number of PEs in the current volume group is 18428 through the above vgdisplay command. The command is as follows:

# lvcreate -l 18428 -n pldy1 lvmdisk 
  • 1

When the logical volume is created successfully, you can use the lvdisplay command to view the logical volume:

[root@li2 ~]# lvdisplay 
--- Logical volume --- 
LV Name /dev/lvmdisk/pldy1 
VG Name lvmdisk 
LV UUID FQcnm3-BMyq-NkJz-hykw-9xg1-Qy8d-8UeGCN 
LV Write Access read/write 
LV Status available 
# open 0 
LV Size 15.00 GB 
Current LE 3840 
Segments 1 
Allocation inherit 
Read ahead sectors 0 
Block device 253:0 

Like the volume group, the logical volume is divided into pieces of space during the creation process. These spaces are called LE (Logical Extents). In the same volume group, the size of LE and PE are the same, and they are one by one. correspond.

5. Create a file system

Create an ext3 file system on the logical volume:

[root@li2 ~]# mkfs -t ext3 /dev/lvmdisk/pldy1
  • 1

After the file system is created, it can be loaded and used:

[root@li2 ~]# mkdir /opt/Oracle 
[root@li2 ~]# mount /dev/lvmdisk/pldy1 /opt/Oracle 
  • 1
  • 2

In order to automatically load the file system when the system starts, you also need to add content in /etc/fstab:

/dev/lvmdisk/pldy1 /opt/Oracle ext3 defaults 1 2 
  • 1

6. Manage LVM

The biggest advantage of LVM is that it can dynamically adjust the partition size without restarting the machine. Let us experience it! Continuing the above example, now assuming that the logical volume /dev/lvmdisk/pldy1 is insufficient and its size needs to be increased, we will discuss it in two situations:

1. There is free space in the volume group

Use the vgdisplay command to check the current volume group space usage:

[root@li2 ~]# vgdisplay 
--- Volume group --- 
VG Name lvmdisk 
System ID 
Format lvm2 
Metadata Areas 2 
Metadata Sequence No 2 
VG Access read/write 
VG Status resizable 
MAX LV 0 
Cur LV 1 
Open LV 0 
Max PV 0 
Cur PV 2 
Act PV 2 
VG Size 71.98 GB 
PE Size 4.00 MB 
Total PE 18428 
Alloc PE / Size 3840 / 15.00 GB 
Free PE / Size 14588 / 56.98 GB 
VG UUID SARfuj-wAUI-od81-VWAc-A1nt-aaFN-JWaPVf 

Determine that the remaining space of the current volume group is 56.98GB, and the number of remaining PEs is 14,588. Here, all the remaining space is added to the logical volume /dev/lvmdisk/pldy1.

[root@li2 Oracle]# lvextend -l+14588 /dev/lvmdisk/pldy1 
Extending logical volume pldy1 to 56.98 GB 
Logical volume pldy1 successfully resized 

The above command uses the -l+14588 parameter, which means to add 14588 PEs to the specified logical volume. If not all the space is used, other forms of lvextend command can also be used.
For example, increase the space of the logical volume /dev/lvmdisk/pldy1 by 5GB to make the space reach 20GB, which can be written as: # lvextend -L+5G /dev/lvmdisk/pldy1or # lvextend -L20G /dev/lvmdisk/pldy1. After increasing the logical volume capacity, the size of the file system must be modified through the ext2online (ext2online or resize2fs command can be used for ext2, ext3 and ext4 file systems, and the xfs_growfs command must be used for xfs file systems, refer to the article ) command.

[root@li2 ~]# ext2online /opt/Oracle/

After the conversion, let's check the current state of the file system:

[root@li2 ~]# df -lh 
Filesystem 1k-blocks Used Available Use% Mounted on 
/dev/sda1 7.4G 1.8G 5.3G 25% / 
none 135M 0 135M 0% /dev/shm 
/dev/mapper/lvmdisk-pldy1 71G 81M 68G 1% /opt/Oracle
  • 2. Insufficient space in volume group

When there is not enough space in the volume group to expand the size of the logical volume, the capacity of the volume group needs to be increased, and the only way to increase the capacity of the volume group is to add new physical volumes to the volume group.
The first is to add a new hard disk (36GB SCSI hard disk), and complete work such as partitioning and creating physical volumes on it. The next step is to use the vgextend command to add the new physical volume (/dev/sdd1) to the volume group.
The command to extend the volume group is as follows:

[root@li2 ~]# vgextend lvmdisk /dev/sdd1 
Volume group "lvmdisk" successfully extended 

Use the vgdisplay command to view the volume group lvmdisk:

[root@li2 ~]# vgdisplay 
--- Volume group --- 
VG Name lvmdisk 
System ID 
Format lvm2 
Metadata Areas 3 
Metadata Sequence No 3 
VG Access read/write 
VG Status resizable 
MAX LV 0 
Cur LV 1 
Open LV 0 
Max PV 0 
Cur PV 3 
Act PV 3 
VG Size 107.97 GB 
PE Size 4.00 MB 
Total PE 27640 
Alloc PE / Size 3840 / 15.00 GB 
Free PE / Size 23800 / 92.97 GB 
VG UUID l8YPvz-uD7h-oj1A-0qS5-TFcT-mbC7-QbjzCu 

After completing the expansion of the volume group, the expansion of the logical volume can be completed according to the method in the first case, and finally the dynamic adjustment of the partition is realized.

Guess you like

Origin blog.csdn.net/zjc801/article/details/109109135