You can clickBottom right cornerTo evaluate the content of the article.Follow buttonTo follow the latest developments in my blog. If the article content is helpful to you, do not forget to clickRecommendation buttonTo support a Oh if you have any questions about the content of the article, you can contact me by comment or mail: 501395377@qq.com/lzp501395377@gmail.com If You Need To reprint, please indicate the source, thank you !!
This article will explain in detail the basic concepts of LVM logical volumes in the Linux disk management mechanism and how LVM works !!!
I. Traditional disk management
In fact, in Linux, our disk management mechanism is similar to that on windows. Most of them use MBR (Master Boot Recorder) to partition a hard disk first, then, format the partition in the file system. In Linux, if you want to use the partition, mount it, in windows, the underlying layer automatically mounts all partitions, and then we can use the partitions.
However, this traditional disk management often causes many problems. For example, when we use a partition, the size of its space is no longer enough, at this time, we have no way to expand partitions by stretching partitions. Of course, there are other third-party disk management software available to divide the disk partition space, but this will cause a lot of damage to our file system, and sometimes cause problems such as file system crash. For traditional disk management, if we encounter that when the partition size is insufficient, we can only add a new hard disk, create a partition on the new hard disk, and format the partition, then copy everything in the previous partition to the new partition. However, the newly added hard disk exists as an independent file system. The original file system has not been expanded, and upper-layer applications can only access one file system. This method may be acceptable for personal computers, but it is unacceptable for servers in the production environment. If you want to copy the content of a partition to another partition, you must first unmount the previous partition and then copy the entire partition, if an important service, such as WWW or FTP, is running normally for 7x24 hours on the server, uninstalling the partition is unimaginable, at the same time, if the partition stores a lot of content, it may take a long time to transfer the partition. Therefore, we will be limited by the traditional disk management at this time, because it cannot perform dynamic disk management. Therefore, to solve this problem, LVM technology was born !!!
Ii. LVM disk management
It is precisely because traditional disk management cannot dynamically manage our disk space. Therefore, LVM is born. What is LVM? How does it manage disks?
LVM (Logical volume Manager) is short for Logical volume management. It is a mechanism for managing disk partitions in Linux. At present, we can not only use the disk management mechanism like LVM on Linux systems, but also have disk management software similar to LVM for other UNIX-like operating systems and windows operating systems.
The operating principle of LVM is actually very simple. It encapsulates the underlying physical hard disk abstraction and presents it to upper-layer applications in the form of logical volumes. In the traditional disk management mechanism, our upper-layer applications directly access the file system to read the underlying physical hard disk. In LVM, it encapsulates the underlying hard disk. When we operate the underlying physical hard disk, it is no longer for partitioning, instead, you can manage the underlying disk of a logical volume. For example, if I add a physical hard disk, the upper-layer service will not feel it at this time, because the last service is presented in a logical volume.
The biggest feature of LVM is its ability to dynamically manage disks. Because the logical volume size can be dynamically adjusted without losing existing data. If we add a new hard disk, it will not change the existing upper-layer logical volume. As a dynamic disk management mechanism, logical volume technology greatly improves the flexibility of disk management !!!
Iii. LVM principles
To understand the principles of LVM, we must first master four basic logical volume concepts.
①PE(Physical Extend) Physical expansion
②PV(Physical Volume) Physical Volume
③VG(Volume Group) Volume Group
④LV(Logical Volume) Logical Volume
We know that after using LVM to dynamically manage disks, we present them to upper-layer services in the form of logical volumes. Therefore, all our operations aim to createLV(Logical Volume), the Logical Volume is used to replace our previous partitions. We can use it by formatting the Logical Volume and then mounting it. So what is the operating principle of LVM? The so-called no picture and no truth, Let's explain the principle of the logical volume through the diagram below !!
1. Format our Physical hard disk to PV (Physical Volume)
We can see that there are two hard disks, one being sda and the other being sdb. In the LVM disk management, we need to first format these two hard disks as ours.PV(Physical Volume), that is, our Physical Volume. In fact, during the formatting process, LVM divides the underlying hard disk into onePE(Physical Extend ),In our LVM disk management, the default PE size is 4 MB.In fact, PE is the most basic unit of logical volume management. For example, if I have a 100 m hard disk, when formatting it into a PV, the physical hard disk is actually divided into PES, because the default PE size is 4 M. This is our first step.
2. Create a VG (Volume Group)
After formatting the hard disk to PV, the second step is to create a volume group, that isVG(Volume Group), Volume groups here we can abstract them into a space pool, VG is used to install PE, we can add one or more PVS to VG, because the hard disk has been divided into multiple PES during the first step, after multiple PVS are added to the VG, the VG stores many PES from different PVS, we can see from the above picture that we have formatted two hard disks, each of which is formatted into three PES, and then added the PES of the two hard disks to our VG, then our VG contains 6 PES, which are the sum of the PES of the two hard disks. When creating a volume group, we usually take a name for it, that is, the VG name.
3. Create the Logical Volume)
【Note:] PV and VG cannot be directly used after they are created, because PV and VG are the underlying things of our logical volumes. In fact, we used them created based on VG.LV(Logical Volume), so the third step is to create the LV to be used based on VG.
After we have created our VG, the LV we have created is to extract the specified number of PES from the VG, we can see that at this time, VG already has 6 PES. At this time, we created our first logical volume, which is the size of 4 PES, that is, 16 M (because the default size of a PE is 4 M), and three of the four PES come from the first hard disk, the other PE comes from the second hard disk. When we create the second logical volume, the size of the logical volume is only two PES at most, because four of them have been allocated to our first logical volume.
Therefore, to create a logical volume is to extract the specified number of PES from the VG. the PES in the VG can come from different PVS, the size of the logical volume that we can create depends on the number of PES in VG, in addition, the size of the created logical volume must be an integer multiple of the PE (that is, the logical volume must be an integer multiple of 4 m ).
4. format the created LV file system and mount and use it.
After creating the LV, We Can format the file system. We finally use the LV we just created, it is equivalent to a traditional file management partition. We first need to format the file system, and then mount it through the mount command, in this case, we can use our logical volume just like using common partitions.
After creating the LV, we will see the LV information in the/dev directory, for example,/dev/vgname/lvname. Each time we create a VG, it will create a folder named after the VG under the/dev directory, after creating the LV on the basis of the VG, we will add a logical volume named after LV under this VG directory.
Next we will summarize the working principle of the entire LVM:
(1)The physical disk is formatted as PV, and the space is divided into PES.
(2)Different PVS are added to the same VG. All pes with different PVS enter the PE pool of the VG.
(3)LV is created based on PE and the size is an integer multiple of PE. The PE that makes up LV may come from different physical disks.
(4)LV can be directly formatted and mounted and used now
(5)LV expansion and reduction is actually to increase or decrease the number of PES that constitute the LV, and the process will not lose the original data.
We can see that if we want to expand LV here, we can directly add an sdc hard disk, format it into PE, and then add the PV to VG, at this time, we can dynamically expand the LV by adding the number of PES in the LV, as long as the size of our LV should not exceed the size of our VG free space !!
This article mainly explains the differences between the traditional disk management and the Dynamic Disk Management Mechanism of LVM, and explains in detail how LVM works. In the next article, we will explain in detail the basic configurations of LVM, including creating PV, VG, and VG, and dynamically expanding and reducing our LV !!!