Detailed explanation of LVM of Linux file system

LLVM is the abbreviation of Logical Volume Manager Logical Volume Management. The main function is to facilitate dynamic expansion and reduction of volumes, which greatly improves the flexibility of disk management. The working principle is as follows:
1. Physical disks are formatted as PV (Physical Volume) physical volume, the space is divided into PE (Physical Extend) physical expansion
2. Add PV to VG (Volume Group) volume group, and display it in the form of PE in VG
3. LV is created based on PE, the size It is an integer multiple of PE. The PEs that make up the LV may come from different physical disks, but they are all in the same VG. PE
4.LV can now be directly formatted and mounted
. The expansion and reduction of 5.LV is actually an increase or decrease. Reduce the number of PEs that make up the LV without losing the original data

Detailed explanation of LVM of Linux file system

Implementation

centos7 does not have lvm tool by default, you need to install lvm2

yum install -y lvm2

Prepare 2 partition blocks in 8e format

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb9         8400896    10498047     1048576   8e  Linux LVM
/dev/sdb10       10500096    14694399     2097152   8e  Linux LVM

1. Create pv

[root@localhost mnt]# pvcreate /dev/sdb9
[root@localhost mnt]# pvcreate /dev/sdb10
[root@localhost mnt]# pvs
  PV         VG Fmt  Attr PSize PFree
  /dev/sdb10    lvm2 ---  2.00g 2.00g
  /dev/sdb9     lvm2 ---  1.00g 1.00g

#详细查看    
[root@localhost mnt]# pvdisplay

2. Create vg and expand

[root@localhost mnt]# vgcreate myvg /dev/sdb9
[root@localhost mnt]# vgextend myvg /dev/sdb10
[root@localhost mnt]# vgs
  VG   #PV #LV #SN Attr   VSize VFree
  myvg   2   0   0 wz--n- 2.99g 2.99g

#详细查看
[root@localhost mnt]# vgdisplay myvg

3. lv creation

[root@localhost mnt]# lvcreate -L 1G -n mylv myvg

#查看
[root@localhost mnt]# lvs
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv myvg -wi-a----- 1.00g

4. Create a file system and mount it

[root@localhost mnt]# mkfs.ext4 -L mylv /dev/myvg/mylv
[root@localhost mnt]# mount /dev/myvg/mylv /mnt/t3
[root@localhost mnt]# df -h|grep mapp
/dev/mapper/myvg-mylv  976M  2.6M  907M    1% /mnt/t3

5. To expand lv, first check how much space is left in the group vg where the current lv is located

[root@localhost mnt]# lvs
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv myvg -wi-ao---- 1.00g
[root@localhost mnt]# vgs myvg
  VG   #PV #LV #SN Attr   VSize VFree
  myvg   2   1   0 wz--n- 2.99g 1.99g

[root@localhost mnt]# lvextend -L 2G /dev/myvg/mylv

#扩展文件系统
[root@localhost mnt]# resize2fs /dev/myvg/mylv
#检查
[root@localhost mnt]# df -h|grep mapp
/dev/mapper/myvg-mylv  2.0G  3.0M  1.9G    1% /mnt/t3

6. Reducing lv is to free up pe

[root@localhost mnt]# umount /dev/mapper/myvg-mylv
[root@localhost mnt]# e2fsck -f /dev/mapper/myvg-mylv
[root@localhost mnt]# resize2fs /dev/myvg/mylv 512M
#这步之前必须确定1G是否能装下源文件
[root@localhost mnt]# lvreduce -L 512M /dev/myvg/mylv

7. Check whether VFree is larger than the partition to be removed below

[root@localhost mnt]# vgs
  VG   #PV #LV #SN Attr   VSize VFree
  myvg   2   1   0 wz--n- 2.99g 2.49g
[root@localhost mnt]# pvs
  PV         VG   Fmt  Attr PSize    PFree
  /dev/sdb10 myvg lvm2 a--    <2.00g   <1.50g
  /dev/sdb9  myvg lvm2 a--  1020.00m 1020.00m

8. Escape the pe data first, shrink the vg, and remove the pv

[root@localhost mnt]# pvmove /dev/sdb10
  /dev/sdb10: Moved: 10.94%
  /dev/sdb10: Moved: 100.00%
[root@localhost mnt]# vgreduce myvg /dev/sdb10
  Removed "/dev/sdb10" from volume group "myvg"
[root@localhost mnt]# pvremove /dev/sdb10
  Labels on physical volume "/dev/sdb10" successfully wiped.

9. If vg is not needed, remove the lv operation [lvremove myvg/mylv]

[root@localhost mnt]# vgremove myvg
Do you really want to remove volume group "myvg" containing 1 logical volumes? [y/n]: y
Do you really want to remove active logical volume myvg/mylv? [y/n]: y
  Logical volume "mylv" successfully removed
  Volume group "myvg" successfully removed
[root@localhost mnt]# pvremove /dev/sdb9
  Labels on physical volume "/dev/sdb9" successfully wiped.

Introduction to Snapshots

Principle introduction: Snapshot is a backup of the source volume, which is based on the implementation of the same lv volume.
Snapshot volume features:
1. Another access entry to
the source volume 2. After the snapshot, when the existing files of the source volume change, the source files will be copied to the snapshot volume
3. The newly added files will not be copied to the snapshot volume

Detailed explanation of LVM of Linux file system

Snapshot: snapshot
lvcreate -L #[mMgGtT] -pr -s -n snapshot_lv_name original_lv_name
-L size
-r onlyread
-s snapshot
snapshot_lv_name snapshot volume name
original_lv_name source volume name

1. Create snapshot snapshot volume source volume is based on mylv logical volume

[root@localhost ~]# lvcreate -s -L 500m -n mylv-snap -p r /dev/myvg/mylv
  Using default stripesize 64.00 KiB.
  Logical volume "mylv-snap" created.

2. Mount the snapshot

[root@localhost ~]# mount /dev/myvg/mylv-snap  /mnt/t2/
mount: /dev/mapper/myvg-mylv--snap 写保护,将以只读方式挂载

[root@localhost ~]# cp /etc/passwd /mnt/t3/   #源卷中文件,之后修改这个文件跟快对比

3. Edit the source volume file

[root@localhost t3]# echo 999999  >> passwd
[root@localhost t3]# tail -1 passwd
999999

3.1. View snapshot volumes

[root@localhost t2]# tail -1 passwd
marvin:x:1001:1001::/home/marvin:/bin/bash

4. Add files to the source volume

[root@localhost t3]# cp /root/anaconda-ks.cfg  /mnt/t2/
cp: 无法创建普通文件"/mnt/t2/anaconda-ks.cfg": 只读文件系统
[root@localhost t3]# cp /root/anaconda-ks.cfg  /mnt/t3

4.1, check the snapshot volume and not

[root@localhost t2]# ls
issue  lost+found  passwd

5. Delete the snapshot volume

[root@localhost mnt]# umount /mnt/t2
[root@localhost mnt]# lvremove /dev/myvg/mylv-snap
Do you really want to remove active logical volume myvg/mylv-snap? [y/n]: y
  Logical volume "mylv-snap" successfully removed

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324741153&siteId=291194637