Detailed explanation of Linux LVM logical volume configuration process (create, increase, reduce, delete, uninstall)

Detailed explanation of Linux LVM logical volume configuration process (create, increase, reduce, delete, uninstall)

Detailed explanation of Linux LVM logical volume configuration process

Many Linux users will encounter this dilemma when installing the operating system: how to accurately evaluate and allocate the capacity of each hard disk partition. If the initial evaluation is inaccurate, once the system partition is not enough, they may have to back up and delete related data, or even be forced to re-plan the partition and reinstall the operating system to meet the needs of the application system.

LVM is a mechanism for managing disk partitions in the Linux environment. It is a logical layer built on top of hard disks and partitions and below the file system, which can improve the flexibility of disk partition management. The default partition format of RHEL5 is the LVM logical volume format. It should be noted that the /boot partition cannot be created based on LVM and must be separated.

1.LVM principle

To understand the principles of LVM, we must first master four basic logical volume concepts.

①PE (Physical Extend)

②PV (Physical Volume)

③VG (Volume Group)

④LV (Logical Volume)

We know that after using LVM to dynamically manage the disk, we present it to the upper-level services in the form of logical volumes. Therefore, the purpose of all our operations is actually to create an LV (Logical Volume). The logical volume is used to replace our previous partition. We can use it by formatting the logical volume and then mounting it. So how does LVM work? There is no truth without a picture. Let's use a picture to explain the principle of logical volumes! !

1. Format our physical hard disk into PV (Physical Volume)

We can see that there are two hard disks, one is sda and the other is sdb. In LVM disk management, I first need to format these two hard disks as our PV (Physical Volume), that is, our physical volume. In fact, in the process of formatting the physical volume, LVM divides the underlying hard disk into PE (Physical Extend) one by one. The default size of PE in our LVM disk management is 4M. In fact, PE is the most basic unit of our logical volume management. For example, if I have a 400M hard disk, when I format it into PV, it actually divides the physical hard disk into 100 PEs, because the default size of PE is 4M. This is our first step.

2. Create a VG (Volume Group)

After formatting the hard disk into PV, our second step is to create a volume group, that is, VG (Volume Group). Here we can abstract the volume group into a space pool. The role of VG is to install PE. We can add one or more PVs to VG. Because the hard disk has been divided into multiple PEs in the first step, after adding multiple PVs to VG, VG will store many PEs from different PVs. We can see from the picture above that we formatted two hard disks, each hard disk was formatted into 3 PEs, and then the PEs of the two hard disks were added to our VG, then our VG contains 6 PEs, and these 6 PEs are the sum of the PEs of the two hard disks. Usually when we create a volume group, we will give it a name, which is the name of the VG.

3. Create the LV (Logical Volume) we will use last based on VG

[Note] After PV and VG are created, we cannot use them directly, because PV and VG are the underlying things of our logical volume. In fact, what we finally use is the LV (Logical Volume) created based on VG, so the third step is to create the LV that we will eventually use based on VG.

After we have created our VG, creating LV at this time actually means taking out the specified number of PEs from the VG. Let's take the above picture as an example. We can see that we already have 6 PEs in our VG at this time. At this time, we create our first logical volume, which is the size of 4 PEs, that is, 16M (because the default size of a PE is 4M), and three of these 4 PEs come from the first hard disk, and the other PE comes from the second hard disk. When we create the second logical volume, its size will be at most the size of two PEs, because four of the PEs have been allocated to our first logical volume.

So creating a logical volume actually means taking out a specified number of PEs from VG. The PEs in VG can come from different PVs. The size of the logical volume we can create depends on the number of PEs in VG, and the size of the logical volume we create must be an integer multiple of PE (that is, the size of the logical volume must be an integer multiple of 4M).

4. Format the file system of the LV we created, and then mount it for use

After creating the LV, we can format the file system. What we finally use is the LV we just created, which is equivalent to the traditional file management partition. We must first format the file system and then mount it through the mount command. At this time, we can use our logical volume like a normal partition.

After we create LV, we will see our LV information in the /dev directory, for example, /dev/vgname/lvname. Every time we create a VG, a folder named after the VG will be created in the /dev directory. After creating LV based on the VG, we will have an additional logical volume named after the LV in this VG directory.

Let's 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, and the PEs of different PVs all enter the PE pool of the VG.

(3)LV is created based on PE, and its size is an integer multiple of PE. The PEs that make up LV may come from different physical disks.

(4) LV can be directly formatted and mounted for use

(5) The expansion and reduction of LV actually increases or decreases the number of PEs that make up the LV, and the original data will not be lost in the process

We can see that if we want to expand the LV here, we can directly add an SDC hard disk, format it into PE, and then add the PV to the VG. At this time, we can dynamically expand the LV by increasing the number of PEs in the LV, as long as the size of our LV does not exceed the size of our VG free space!

2. Create LVM logical volume

After getting familiar with the working principle of LVM, we first need to format our physical hard disk into PV, then add multiple PVs to the created VG, and finally create our LV through VG.

3. Stretch a logical volume

We know that compared with the various problems of traditional disk management methods, using LVM logical volumes to manage our disks allows us to manage them dynamically. In the traditional disk management method, if the partition size is insufficient, we can only add a physical hard disk and then partition it. Because the added hard disk exists as an independent file system, it has no effect on the original partition. If we need to expand the partition at this time, we can only uninstall the previous partition first, then transfer all the information to the new partition, and finally mount the new partition. If it is in a production environment, this is unimaginable. Because of this, we have the LVM disk management method, which can dynamically manage our disks.

Let's first look at the schematic diagram of dynamically stretching a logical volume:

We can see from the above figure that when we stretch the logical volume, we actually increase the number of PEs in the logical volume, and the number of PEs is determined by the number of remaining PEs in the VG.

[Note:] The logical volume stretching operation can be performed online without uninstalling our logical volume

The advantage of this is that when the size of our logical volume is not enough, we do not need to uninstall it, we can dynamically increase the size of our logical volume, and it will not have any impact on our system. For example, if we are running an important service or database on our server and require it to be online 24/7, then it is very necessary to dynamically increase the size of the logical volume.

Next, let's look at the steps to stretch the logical volume:

Because the stretching operation of our logical volume can be performed online, we first mount the logical volume here and dynamically stretch our logical volume when using it.

4. Experimental environment:

First, create two partitions sdb1 1G, sdb2 2G from the empty hard disk sdb. Prepare for the next LVM.

In order to facilitate maintenance and management in the future, remember to add a label to the partition, so that even if you are not there, others will not easily move this area after seeing the label. The LVM label is 8e. After setting, remember to press w to save

1. Create a logical volume

Convert the two newly created partitions /dev/sdb1 /dev/sdb2 into physical volumes, mainly by adding LVM attribute information and dividing PE storage units.

Create a volume group vgdata and add the two physical volumes just created to the volume group. It can be seen that the default PE size is 4MB, and PE is the smallest storage unit of the volume group. The size can be modified using the –s parameter.

Split 500M from the physical volume vgdata to the new logical volume lvdata1.

Use the mkfs.ext4 command to create an ext4 file system on the logical volume lvdata1.

Mount the created file system /data1 to /data1. (After creation, a soft link named "Volume Group-Logical Volume" will be generated in /dev/mapper/)

To facilitate automatic mounting when the server is restarted in the future, you need to add the created file system mounting information to /etc/fstab. The UUID can be queried through the blkid command.

To check whether /etc/fstab is set correctly, you can first unmount the logical volume data1, and then use mount –a to make the kernel reread /etc/fstab to see if it can be automatically mounted.

2. The logical volume lvdata1 is not enough, how to expand it.

Adding space to the logical volume will not affect the use of previous space, so there is no need to unmount the file system. Directly use the command lvextend –L +500M /dev/vgdata/lvdata1 or lvextend –l 2.5G /dev/vgdata/lvdata1 to add 500M space to lvdata1 (lvdata1 currently has 2G space). After the setting is completed, remember to use the resize2fs command to synchronize the file system.

3. How to expand the volume group when the volume group is not enough

Re-create a partition sdb3 on the second hard disk. The specific steps are omitted. And add the created partition to the existing volume group vgdata. Use the pvs command to check whether the command is successful.

4. When the hard disk space is insufficient, reducing the space of the logical volume can release it for use by other logical volumes.

To reduce the logical volume space, follow these steps:

1. Unmount the logical volume data1 first

2. Then use the e2fsck command to check the free space on the logical volume.

3. Use resize2fs to reduce the file system to 700M.

4. Use the lvreduce command to reduce the logical volume to 700M.

Note: The file system size and logical volume size must be consistent. If the logical volume is larger than the file system, space will be wasted because some areas are not formatted as a file system. If the logical volume is smaller than the file system, there will be problems with the data.

After completion, you can use the mount command to mount and reuse it.

5. If a disk or partition fails, how to quickly transfer data to other spaces in the same volume group.

1. Transfer space data through the pvmove command

2. Use the vgreduce command to remove the failed disk or partition from the volume group vgdata.

3. Use the pvremove command to delete the bad disk or partition from the system.

4. Manually remove the hard disk or repair the partition using some tools.

6. Delete the entire logical volume

1. First, unmount the logical volume lvdata1 using the umount command

2. Modify the mount information of the logical volume in /etc/fstab, otherwise the system may not start.

3. Delete the logical volume lvdata1 through lvremove

4. Delete volume group vgdata through vgremove

5. Convert the physical volume into a normal partition through pvremove.

After deleting, don't forget to change the partition id. Just change it to a normal Linux partition.

Summary: LVM logical volume is a great space utilization mechanism in Linux, because the partition cannot be enlarged or reduced without formatting. LVM can make your disk space flexible.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Tutorial on adjusting the size of lvm logical volume partition in Linux (for different file systems such as xfs and ext4)

<<:  A brief discussion on how to learn JS step by step

>>:  Solution to index failure in MySQL due to different field character sets

Recommend

Detailed explanation of JavaScript object conversion to primitive value

Table of contents Object.prototype.valueOf() Obje...

Explanation of the new feature of Hadoop 2.X, the recycle bin function

By turning on the Recycle Bin function, you can r...

How to install docker under centos and remotely publish docker in springboot

Table of contents 1. Installation of JDK1.8 under...

Two ways to visualize ClickHouse data using Apache Superset

Apache Superset is a powerful BI tool that provid...

In-depth understanding of javascript prototype and prototype chain

Table of contents 1. What is a prototype? 2. Prot...

Summary of fragmented knowledge of Docker management

Table of contents 1. Overview 2. Application Exam...

How to use VirtualBox to simulate a Linux cluster

1. Set up HOST on the host Macbook The previous d...

MySQL 5.7.19 winx64 free installation version configuration tutorial

mysql-5.7.19-winx64 installation-free version con...

WeChat applet component development: Visual movie seat selection function

Table of contents 1. Introduction 1. Component da...

How to install binary MySQL on Linux and crack MySQL password

1. Make sure the system has the required libaio s...

A brief analysis of how to upgrade PHP 5.4 to 5.6 in CentOS 7

1. Check the PHP version after entering the termi...