LVM Expansion Road for Linux Disk Expansion

introduction

As we all know, most of our services run on Linux. Because of the characteristics of the Linux command line form, many developers only like to use Linux as a machine for running services, not for daily use. As a result, we can only remember the commonly used deployment commands, but we don't know how to start when encountering some complicated Linux operation and maintenance. No, I encountered the problem of insufficient disk on the server in the past two days. At first, I just wanted to Google it quickly, but in the end I still had to supplement the knowledge of LVM in Linux. In order to facilitate the rapid expansion of future generations, it is hereby recorded.

What is LVM

LVM (Logical Volume Manager) is translated as a logical volume manager. You can think of it as a unified intermediate layer in the form of "RAID" that uses multiple disks as one piece (you can understand this temporarily, but the principle is not the same. thing). That is to say, if we have three 4T hard disks, we can use LVM to form a 12T hard disk for use, and we can also expand the capacity online very safely later. ps: Now when Linux is installed, it actually adopts the form of LVM by default.

Composition of LVM

LVM consists of three main parts from top to bottom:

  • Physical Volumes physical volumes: created through physical storage devices, which are our disks;
  • Volume Group volume group: With PV (specifically referring to physical volume), we can create VG (specifically referring to volume group);
  • Logical Volume Logical Volume: With the VG, we can create an LV (logical volume) that can be used for our final storage file mount.
    insert image description here
    In order to facilitate everyone's understanding, I will give an example of deep-fried dough sticks. Our bottom-level physical volume is equivalent to our flour. After buying two bags of flour from JD, we need to mix the flour. We have one bag of flour and one pot, and two pots (these two pots are PV) . Then we can put these two pots of woken noodles into a big tank for use (the big tank is VG). The fried dough stick operator can take the reconciled noodles (LV) in a large tank, and then make fried dough sticks. The operator can take as much as he wants, right? The tank can be used however he wants. As for which flour this vat of noodles comes from, it doesn’t matter, just use it directly! ps: In fact, when PV is created, the flour will be divided into 4M by default. The smallest unit of this 4M is called PE (Physical Extend).

Start to expand

Then after understanding the basic concepts, we will start to expand the capacity. Let's first use it df -hto see the available space of my server:
insert image description here
it's too miserable! Only less than 30G is available, which can do something, but now the hard disk is at least 500G, why is there so little free space, it should be caused by being useless. So we lsblkuse check the disk situation:
insert image description here
Sure enough, I can see that my largest LV partition (cach3-lvcache3) has 484.2G of storage and is not used. Then let's take a look at the situation of the fried dough stick production line of this machine:
first look at the PV basin, use pvsthe command (simplified) pvdisplaycommand (detailed):
insert image description here
I have three pots of flour in this machine, which are: /dev/sda3 [54 G ], /dev/sda6 [279 G], /dev/sda7 [204 G], remember the PE we mentioned above, you can see that they are all 4M.
Let's take a look at my noodle tank again: use vgscommand (simplified) vgdiplaycommand (detailed):
insert image description here
here is more clear, we did not put the basin and the good noodles into one tank, but put them into two tanks: cache3centos. The 484 G noodle tank that I was free had never been used, so I just downgraded him. Note: all our reverse operations must be downgraded to one level higher than our own or the same level before operating. Here I want to put the same as LG cache3and centosput them together, that is, cache3merge them into centos, you need to cache3downgrade them first. The command to delete VG is: vgremove cache3
insert image description here
OK, now cache3delete this vat and put it in the washbasin.
insert image description here
We now know that flour is already in dev/sda6,dev/sda7In the basin, we are going to put the noodles in these two basins into the tank now. Use: vgextend centos /dev/sda6command. After adding, you can see that the VG (face vat) has been merged into one, the total size is 539G, and the available one is 484G.
insert image description here
I gave the 484G available space to the existing home LV 380G and root LV 100G respectively. At this time, look at our operating room LV, which has been expanded!
insert image description here
Huh~ But why is our file system still so small? This is because the logical volume is a low-level thing. To use this thing, the operating system must perform a format, that is, create a file system, and the size of the file system has been fixed when it is created. We stretched the underlying logical volume. This file system was not updated. At this time, the operating system can't recognize this new space, so we need to update it to let the operating system know that we have expanded! Different formats of partitions have different ways to update the file system. You need to check whether your file system is ext4 or xfs. If it is ext4 use: resize2fs /dev/centos/rootto update, if it is xfs use: xfs_growfs /dev/mapper/centos-rootcommand. How to check the format of your own partition? Use: cat /etc/fstab | grep centos-homecommand
insert image description here
As you can see, here I am xfs, so I use: xfs_growfs /dev/mapper/centos-rootcommand.
insert image description here
Yeah~ It's finally done!

Summarize

Overall, LVM is still very convenient, and can adjust our file system very safely. Hope it helps you!
See you~

Guess you like

Origin blog.csdn.net/u012558210/article/details/117805137