Introduction to LVM of Linux Disk Management

1. Overview of LVM

1. Why use LVM

  • LVM is a logical volume management system tool
  • Using the traditional partition method, the size is fixed after the partition
  • If the partition is too large, it will cause a waste of space; if the partition is too small, it will cause insufficient space
  • If it is too small, you need to re-divide the disk partition, or link the directory of this partition to another partition by soft link
  • Doing so temporarily solves the problem, but brings trouble to management

2. What is LVM

  • Logical Volume Management System Tool
  • LVM can create and manage logical volumes instead of using disks directly
  • It allows administrators to flexibly manage the expansion and shrinkage of logical volumes, and the operation is simple and does not damage existing data
  • Feel free to add new disks to LVM to extend existing logical volumes
  • LVM does not require a reboot to let the kernel know about the existence of a partition

2.LVM noun meaning

  • PV : Physical volume, which initializes a conventional disk device through the pvcreate command to form a physical volume 🔔 Flour
  • VG : Volume group, which combines multiple physical volumes into a logical whole, so that the size of the volume group is the sum of multiple hard disks 🔔Dough
  • LV : logical volume, which is divided into the required space from the volume group, the user only needs to format it and then mount it for use 🔔Cut it into buns
  • PE : basic unit (physical extend), the smallest unit of the allocated logical size, the default is 4MB basic block (if 100MB logical space is allocated, 25 PEs need to be created) 🔔Wheat

3. Advantages and disadvantages of LVM

  • advantage
1. The size of the file system can be dynamically expanded while the system is running
2. Filesystems can span multiple disks, So the file system size is not limited by the physical disk
3. New disks can be added to "LVM" in the storage pool
4. Redundant important data can be mirrored to multiple physical disks
5. Can easily export the entire volume group to another state machine
  • shortcoming
1. Because of the addition of additional operations, Access performance is affected
2. When one of the disks in the volume group is damaged, The entire volume group is affected
🔰explain: because of a"logical volume"may be composed of several different"physical volume"inner"PE"consist of
🔰solve: bottom use"RAID"Do, upper use"LVM", Both redundancy and dynamic expansion

2. The use of LVM

1.LVM basic commands

  • Install LVM first: yum install lvm2 -y

  • Make PV : pvcreate [device name]

  • View PV : pvs

  • Make VG : vgcreate [vg name] [multiple pv paths]

  • View VG : vgs

  • Make LV: lvcreate -L [specify size] -n [specify lv name] [specify which vg to divide from]

  • View LV : lvs

  • Expansion VG : vgextend [specify the vg to be expanded] [specify the new pv to be added]

  • Expansion LV: lvextend -L [+"specified size"] -n [specify the pv path to be increased]

  • Update the xfs file system after expansion: xfs_growfs [Which lv is expanded will update which lv]

  • remove PV : pvremove [pv to remove]

  • delete VG : vgremove [name of vg to delete]

  • Delete LV : lvremove [lv path to delete]

  • Create LV snapshot: lvcreate -L [specify snapshot size] -s -n [snapshot name] [the lv path that needs to be snapshotted]

  • Snap merge: lvconvert --mergesnapshot [snapshot name]

2. Make PV, VG, LV demo

  • Make PV
🌞We can do for a partition, You can also do it on the entire disk, I'm going to prepare"sdc3, sde"two devices
[root@shawn ~]#pvcreate /dev/sdc3 
[root@shawn ~]#pvcreate /dev/sde

🌞use"pvs"Check it out"pv"
[root@shawn ~]#pvs
PV         VG Fmt  Attr PSize   PFree  
/dev/sdc3     lvm2 ---  100.00m 100.00m
/dev/sde      lvm2 ---    5.00g   5.00g
  • make VG
🌞put the two"pv"synthesize one"vg"
[root@shawn ~]#vgcreate vg1 /dev/sdc3 /dev/sde
Volume group "vg1" successfully created  # success stands for success

🌞Check "vg"
[root@shawn ~]#vgs
VG  #PV #LV #SN Attr   VSize  VFree 
vg1   2   0   0 wz--n- <5.09g <5.09g
  • make LV
🌞make one"1G"of"lv", one"2G"of"lv"
[root@shawn ~]#lvcreate -L 1G -n lv1_from_vg1 vg1
[root@shawn ~]#lvcreate -L 2G -n lv2_from_vg1 vg1

🌞Check "lv"
[root@shawn ~]#lvs
LV   VG  Attr LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
lv1_from_vg1 vg1 -wi-a----- 1.00g                  
lv2_from_vg1 vg1 -wi-a----- 2.00g   
  • Make it a filesystem and mount it
🌞Create two files first
[root@shawn ~]#mkdir /test3
[root@shawn ~]#mkdir /test4

🌞put the two"lv"make file system
[root@shawn ~]#mkfs.xfs /dev/vg1/lv1_from_vg1 
[root@shawn ~]#mkfs.xfs /dev/vg1/lv2_from_vg1 

🌞mount it
[root@shawn ~]#mount /dev/vg1/lv1_from_vg1  /test3
[root@shawn ~]#mount /dev/vg1/lv2_from_vg1  /test4
  • df -h command to check

3. Dynamic expansion demonstration

  • demo scene
🌞Suppose our"lv1"that is"/test3"not enough
🌞we can start from"vg1"Dynamic acquisition space inside
🌞then suppose"vg1"Not enough space
🌞Then we can only add new"pv"
  • Add PV
🌞We prepared a new partition"/dev/sdd1"
[root@shawn ~]#pvcreate /dev/sdd1

🌞Check it out
[root@shawn ~]#pvs
PV         VG  Fmt  Attr PSize  PFree 
/dev/sdc3  vg1 lvm2 a--  96.00m 96.00m
/dev/sdd1      lvm2 ---   5.00g  5.00g   #Added this piece
/dev/sde   vg1 lvm2 a--  <5.00g <2.00g
  • Add PV to vg1
[root@shawn ~]#vgextend vg1 /dev/sdd1

🌞Check it out"vg1"Space
[root@shawn ~]#vgs
VG  #PV #LV #SN Attr   VSize   VFree 
vg1   3   2   0 wz--n- <10.09g <7.09g  #Compared to before, it can be found that "5G" has been added
  • Expand lv1 to 4G space
[root@shawn ~]#lvextend -L +4G -n /dev/vg1/lv1_from_vg1 

🌞renew"xfs"System Files
[root@shawn ~]#xfs_growfs /dev/vg1/lv1_from_vg1 

🌞Check it out"lv1"space
[root@shawn ~]#lvs
LV VG  Attr LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
lv1_from_vg1 vg1 -wi-ao---- 5.00g   #Found 4G bigger           
lv2_from_vg1 vg1 -wi-ao---- 2.00g 

4. Dynamic shrink and delete

  • shrink
🌞hint: Businesses generally do not shrink,but directly delete,and"xfs"The file system does not support shrinking

🌞Order: lvreduce -L [-]MGT /dev/VG_NAME/LV_NAME
  • delete LV
🌞You need to unmount the mount point before deleting
[root@shawn ~]#umount /test4
[root@shawn ~]#lvremove /dev/vg1/lv2_from_vg1 
🌞There is a prompt to enter"y"
  • delete VG
[root@shawn ~]#vgremove vg1
  • delete PV
🌞The only thing that can be deleted is that it does not belong to any "vg" of "pv"
[root@shawn ~]#pvremove /dev/sde

5. Snapshot demo

  • LVM provides a snapshot function, which is to make a backup of the file system

  • Copy-on-write is used, which is a hard link that only creates the inode of the actual data when it is created.

  • Once the original file corresponding to the snapshot is changed, the data to be changed on the original volume will be copied to the snapshot before the change

  • The snapshot actually creates a logical volume, but the volume is somewhat different from the normal logical volume

  • LVM snapshot underlying schematic

  • emphasis
1. "VG"space to store the snapshot itself,cannot be filled,Snapshot General Settings and Sources"lv"equal in size
2. Because the snapshot is the same as the source"LV"share a lot"PE",So where the snapshot is "VG" must be backed up "LV"same, Otherwise the snapshot will be invalid
  • Take snapshots and reply to single (small) file demos
🌞Let's clean up the previous environment,then make new"PV,VG,LV",Make the experiment look more obvious
[root@shawn ~]#pvs
PV         VG Fmt  Attr PSize   PFree  
/dev/sdc3     lvm2 ---  100.00m 100.00m
/dev/sdd1     lvm2 ---    5.00g   5.00g
/dev/sde      lvm2 ---    5.00g   5.00g

[root@shawn ~]#vgcreate vg1 /dev/sde /dev/sdd1
[root@shawn ~]#vgs
VG  #PV #LV #SN Attr   VSize VFree
vg1   2   0   0 wz--n- 9.99g 9.99g

🌞make one"lv1",and format and mount,Check
[root@shawn ~]#lvcreate -L 1G -n lv1_from_vg1 vg1
[root@shawn ~]#mkfs.xfs /dev/vg1/lv1_from_vg1
[root@shawn ~]#mount /dev/vg1/lv1_from_vg1 /test3
[root@shawn ~]#df -h
/dev/mapper/vg1-lv1_from_vg1 1014M   33M  982M    4% /test3

🌞write some files in it,and view
[root@shawn ~]#echo 333 > /test3/1.txt
[root@shawn ~]#echo 222 > /test3/2.txt
[root@shawn ~]#echo 111 > /test3/3.txt
[root@shawn ~]#cat /test3/1.txt /test3/2.txt /test3/3.txt 

🌞Start making a snapshot of this time(Snapshots need to be in the same"vg"in),and view
[root@shawn ~]#lvcreate -L 1G -s -n lv1_from_vg1_bak /dev/vg1/lv1_from_vg1 
[root@shawn ~]#lvs
LV   VG  Attr   LSize Pool Origin  Data%  Meta%  Move Log Cpy%Sync Convert
lv1_from_vg1     vg1 owi-aos--- 1.00g                            
lv1_from_vg1_bak vg1 swi-a-s--- 1.00g   lv1_from_vg1 0.00  #Discovery snapshot created successfully

🌞Modify files to simulate data tampering,and view
[root@shawn ~]#echo "This is change file" >> /test3/3.txt 
[root@shawn ~]#cat /test3/3.txt 
This is change file

🌞Data recovery
⛅mount snapshot: When the snapshot is mounted, it is the same as the original"LVM"is the same"UUID"
⛅and"XFS"is not allowed to be the same"UUID"The file system is mounted, so you need to add options"-0 nouuid"Do not use uuid do identification
[root@shawn ~]#mount -o nouuid /dev/vg1/lv1_from_vg1_bak /test4
[root@shawn ~]#ls /test4
1.txt  2.txt  3.txt
[root@shawn ~]#cat /test4/3.txt 
111           #Confirmed to be the original sub

🌞copy it back
[root@shawn ~]#cp -rf /test4/3.txt /test3/3.txt 
cp: whether to cover"/test3/3.txt"? y
[root@shawn ~]#cat /test3/3.txt 
111           #back to the original sub
  • Too many recovered files can be merged directly
🌞Check the original data first
[root@shawn ~]#cat /test3/1.txt /test3/2.txt /test3/3.txt 

🌞Create snapshot"bak2",and view
[root@shawn ~]#lvcreate -L 1G -s -n lv1_from_vg1_bak2 /dev/vg1/lv1_from_vg1
[root@shawn ~]#lvs
LV   VG  Attr  LSize Pool Origin   Data%  Meta%  Move Log Cpy%Sync Convert
lv1_from_vg1      vg1 owi-aos--- 1.00g                         
lv1_from_vg1_bak  vg1 swi-aos--- 1.00g      lv1_from_vg1 0.20 
lv1_from_vg1_bak2 vg1 swi-a-s--- 1.00g      lv1_from_vg1 0.00

🌞Modify the original file,and view
[root@shawn ~]#echo aaaaa >> /test3/1.txt 
[root@shawn ~]#echo aaaaa >> /test3/2.txt 
[root@shawn ~]#echo aaaaa >> /test3/3.txt
[root@shawn ~]#cat /test3/1.txt /test3/2.txt /test3/3.txt 

🌞Unmount the original data device and snapshot first,then merge,Snapshots are automatically deleted(one-time merger)
[root@shawn ~]#umount /test3
[root@shawn ~]#umount /test4
[root@shawn ~]#lvconvert --mergesnapshot /dev/vg1/lv1_from_vg1_bak2 
[root@shawn ~]#mount /dev/vg1/lv1_from_vg1 /test3
[root@shawn ~]#ls /test3
1.txt  2.txt  3.txt
[root@shawn ~]#cat /test3/1.txt /test3/2.txt /test3/3.txt 
111           #found back to the original sub

🌞have a look at"bak2"document
[root@shawn ~]#lvs
LV  VG  Att LSize Pool Origin  Data%  Meta%  Move Log Cpy%Sync Convert
lv1_from_vg1     vg1 owi-aos--- 1.00g    
lv1_from_vg1_bak vg1 swi-a-s--- 1.00g      lv1_from_vg1 0.20   #Surprise 2 is gone

Tags: Linux

Posted by Canadian on Mon, 02 May 2022 15:02:22 +0300