Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed description of the Linux LVM logical volume configuration process (create, add, decrease, delete, unmount)

2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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 not accurate, once the system partition is not enough, they may have to back up and delete the relevant data, or even be forced to replan the partition and reinstall the operating system to meet the needs of the application system.

LVM is a mechanism for managing disk partitions in Linux environment. It is a logical layer built on hard disks and partitions and under the file system, which can improve the flexibility of disk partition management. The default partition format installed by RHEL5 is the format of LVM logical volumes. It is important to note that / boot partitions cannot be created based on LVM and must be independent.

I. LVM principle

In order to understand the principle of LVM, we must first master four basic concepts of logical volumes.

① PE (Physical Extend) physical extension

② PV (Physical Volume) physical Volume

③ VG (Volume Group) volume group

④ LV (Logical Volume) logical Volume

We know that after using LVM to dynamically manage the disk, we present it to the upper services as logical volumes. So all our operations are actually to create a LV (Logical Volume), where the logical volume is used to replace our previous partition, and we can use it by formatting the logical volume and then mounting it. So how does LVM work? The so-called no picture, no truth, we below through the diagram to explain the principle of logical volumes!

1. Format our physical hard drive to PV (Physical Volume)

We can see that there are two hard drives here, one is sda and the other is sdb. In LVM disk management, I first need to format these two hard drives into our PV (Physical Volume), that is, our physical volumes. In fact, in the process of formatting physical volumes, LVM divides the underlying hard drives into PE (Physical Extend), and 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 drive, when I format it into PV, I actually divide the physical hard drive into 100m PE, because the default size of PE is 4m. This is our first step.

two。 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), where we can abstract it into a space pool. The function of VG is to install PE. We can add one or more PV to VG, because the hard disk has been divided into multiple PE in the first step, so after adding multiple PV to VG VG contains a lot of PE from different PV. We can see from the picture above that we formatted two hard drives, each of which was formatted into three PE, and then added the PE of both hard drives to our VG. Then our VG contains six PE, which is the sum of the PE of the two hard drives. Usually when we create a volume group, we give it a name, that is, the name of the VG.

3. Create the final LV (Logical Volume) we are going to use based on VG

[note] after PV and VG are created, we cannot use them directly, because PV and VG are the things at the bottom of our logical volume, and we actually end up using LV (Logical Volume) created on the basis of VG, so the third step is to create the LV we want to use based on VG.

When we have created our VG, when we create the LV, we actually take our specified number of PE from the VG, or take the figure above. We see that we already have six PE in our VG at this time. At this time, we create our first logical volume, which is the size of four PE, that is, 16m (because the default size of a PE is 4m). Three of the four PE are from the first hard drive, while the other PE is from the second hard drive. When we create the second logical volume, it will be at most the size of two PE, because four of the PE have already been assigned to our first logical volume.

So to create a logical volume is to take out our specified number of PE,VG from VG, the PE in our specified number of PE,VG can come from different PV, the size of the logical volume we can create depends on the number of PE in the VG, and the size of the logical volume we create must be an integral multiple of PE (that is, the size of a logical volume must be an integral multiple of 4m).

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

After creating the LV, we will be able to format the file system. What we finally use is the LV we just created, which is equivalent to the traditional file management partition. We have to format it first, and then mount it through the mount command. At this time, we will be able to use our logical volumes as if we were using normal partitions.

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

Let's summarize how the whole LVM works:

(1) physical disks are formatted as PV, and space is divided into PE.

(2) different PV is added to the same VG, and all the PE of different PV are added to the PE pool of VG.

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

(4) LV can be directly formatted and mounted.

(5) the expansion and reduction of LV actually increases or decreases the number of PE that make up the LV, and the process does not lose the original data.

We see that if we want to expand the LV here, add a sdc hard drive directly, then 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 PE in the LV, as long as the size of our LV does not exceed the size of our VG free space.

Second, create LVM logical volume

Familiar with how LVM works, the first step is to format our physical hard disk into PV, then add multiple PV to the created VG, and finally create our LV through VG.

Third, stretch a logical volume

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

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

As we can see from the figure above, when we stretch a logical volume, it is actually adding the number of PE to the logical volume, and the number of PE is determined by the number of PE left in the VG.

[note:] the stretching operation of logical volumes can be carried out online, and there is no need to unmount our logical volumes

The advantage 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 will not have any impact on our system. For example, if we have an important service or database running on our server and require us to stay online 24 hours a day, it is necessary to dynamically increase the size of logical volumes.

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

Because the stretching operation of our logical volume can be carried out online, here we mount the logical volume first and stretch our logical volume dynamically in use.

four。 Experimental environment:

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

In order to facilitate maintenance and management at a later stage, remember to mark the partition so that even if you are not there, others will not easily touch the area when they see the sign. The logo of LVM is 8e. Remember to press w to save after setting up.

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 you just created to the volume group. It can be seen that the default PE size of 4 MB is the smallest storage unit of the volume group. You can change the size with 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. (once created, a soft connection named "Volume Group-logical Volume" will be generated in / dev/mapper/.)

To facilitate automatic mount after server restart, you need to add the created file system mount information to / etc/fstab. The UUID can be queried through the blkid command.

To see if / etc/fstab is set correctly, unmount the logical volume data1, and then use mount-a to make the kernel read / etc/fstab again to see if it can be mounted automatically.

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

Adding space to logical volumes does not affect the use of previous space, so there is no need to unmount the file system, directly through the command lvextend-L + 500m / dev/vgdata/lvdata1 or lvextend-l 2.5G / dev/vgdata/lvdata1 to add 500m space to lvdata1 (lvdata1 is currently 2G space), 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 from the second hard drive, omitting the specific steps. And add the created partition to the existing volume group vgdata. Use the pvs command to see if it is successful.

4. When there is not enough hard disk space, if the space of the logical volume is reduced and released to other logical volumes.

Reduce logical volume space by following the steps below

1. Unmount the logical volume data1 first

2. Then use the e2fsck command to detect 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 the same. If the logical volume is larger than the file system, space is wasted because some areas are not formatted into the file system. If the logical volume is smaller than the file system, there will be a problem with the data.

When you're done, you can mount it with the mount command and reuse it.

5. How to quickly transfer data to other spaces in the same volume group if a disk or partition fails.

1. Transfer spatial data through pvmove command

2. Remove the soon-to-be-broken disk or partition from the volume group vgdata by vgreduce command.

3. Delete the soon-to-be-broken disk or partition from the system by pvremove command.

4. Remove the hard drive manually or repair the partition with some tools.

6. Delete the entire logical volume

1. Unmount the logical volume lvdata1 by umount command.

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

3. Delete logical volume lvdata1 through lvremove

4. Delete the volume group vgdata through vgremove

5. Convert physical volumes into normal partitions through pvremove.

After deletion, don't forget to modify the id ID of the partition. Just change it to a normal Linux partition.

Summary: LVM logical volumes are a great space usage mechanism in Linux, because partitions cannot be enlarged or reduced without formatting. You can use LVM to make your disk space flexible.

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report