35.第九章 磁盘存储和文件系统管理(五)

4.逻辑卷管理器(LVM)

4.1 LVM介绍

LVM: Logical Volume Manager 可以允许对卷进行方便操作的抽象层,包括重新设定文件系统的大小,允许在多个物理设备间重新组织文件系统

LVM可以弹性的更改LVM的容量

35.第九章 磁盘存储和文件系统管理(五)
通过交换PE来进行资料的转换,将原来LV内的PE转移到其他的设备中以降低LV的容量,或将其他设备中的PE加到LV中以加大容量

实现过程

  • 将设备指定为物理卷
  • 用一个或者多个物理卷来创建一个卷组,物理卷是用固定大小的物理区域(Physical Extent,PE)来定义的
  • 在物理卷上创建的逻辑卷, 是由物理区域(PE)组成
  • 可以在逻辑卷上创建文件系统并挂载
  • 35.第九章 磁盘存储和文件系统管理(五)第一个逻辑卷对应设备名:/dev/dm-#

dm: device mapper,将一个或多个底层块设备组织成一个逻辑设备的模块

软链接:

  • /dev/mapper/VG_NAME-LV_NAME
  • /dev/VG_NAME/LV_NAME

范例:

/dev/mapper/vol0-root
/dev/vol0/root

#命名方式
/dev/vg0/lv0
/dev/mapper/vg0-lv0

4.2 实现逻辑卷

相关工具来自于 lvm2 包

[root@rocky8 ~]# dnf -y install lvm2

4.2.1 pv管理工具

显示pv信息

pvs:简要pv信息显示
pvdisplay

创建pv

pvcreate /dev/DEVICE

删除pv

pvremove /dev/DEVICE

4.2.2 vg管理工具

显示卷组

vgs
vgdisplay

创建卷组

vgcreate [-s #[kKmMgGtTpPeE]] VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]

范例:

vgcreate -s 16M vg0 /dev/sdb /dev/sdc #指定PE的大小,默认4M

管理卷组

vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]

删除卷组

  • 先做pvmove
  • 再做vgremove

4.2.3 lv管理工具

显示逻辑卷

lvs
Lvdisplay

创建逻辑卷

lvcreate -L #[mMgGtT] -n NAME VolumeGroup

范例:

lvcreate -l 60%VG -n mylv testvg
lvcreate -l 100%FREE -n yourlv testvg

删除逻辑卷

lvremove /dev/VG_NAME/LV_NAME

重设文件系统大小

fsadm [options] resize device [new_size[BKMGTEP]]
resize2fs [-f] [-F] [-M] [-P] [-p] device [new_size]
xfs_growfs /mountpoint

范例:

#创建物理卷
pvcreate /dev/sda3
#为卷组分配物理卷
vgcreate vg0 /dev/sda3
#从卷组创建逻辑卷
lvcreate -L 256M -n data vg0
#mkfs.xfs /dev/vg0/data
#挂载
mount /dev/vg0/data /mnt/data#

范例:

[root@rocky8 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0   50G  0 part /data
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0    2G  0 part [SWAP]
sdb      8:16   0   10G  0 disk #添加一块10G硬盘
sdc      8:32   0    7G  0 disk #添加一块7G硬盘 
sr0     11:0    1  9.2G  0 rom  

[root@rocky8 ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x744070d0.

Command (m for help): p
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x744070d0

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-20971519, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +4G

Created a new partition 1 of type 'Linux' and of size 4 GiB.

Command (m for help): p
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x744070d0

Device     Boot Start     End Sectors Size Id Type
/dev/sdb1        2048 8390655 8388608   4G 83 Linux

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs        
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT            
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor      
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary  
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS    
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE 
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep        
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT            
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): p
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x744070d0

Device     Boot Start     End Sectors Size Id Type
/dev/sdb1        2048 8390655 8388608   4G 8e Linux LVM

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[root@rocky8 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0   50G  0 part /data
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0    2G  0 part [SWAP]
sdb      8:16   0   10G  0 disk 
└─sdb1   8:17   0    4G  0 part 
sdc      8:32   0    7G  0 disk 
sr0     11:0    1  9.2G  0 rom  

[root@rocky8 ~]# fdisk -l
Disk /dev/sda: 200 GiB, 214748364800 bytes, 419430400 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x43d7507d

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048   2099199   2097152    1G 83 Linux
/dev/sda2         2099200 211814399 209715200  100G 83 Linux
/dev/sda3       211814400 316671999 104857600   50G 83 Linux
/dev/sda4       316672000 419430399 102758400   49G  5 Extended
/dev/sda5       316674048 320868351   4194304    2G 82 Linux swap / Solaris


Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x744070d0

Device     Boot Start     End Sectors Size Id Type
/dev/sdb1        2048 8390655 8388608   4G 8e Linux LVM


Disk /dev/sdc: 7 GiB, 7516192768 bytes, 14680064 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@rocky8 ~]# pvs
-bash: pvs: command not found
[root@rocky8 ~]# dnf -y install lvm2

[root@rocky8 ~]# rpm -ql lvm2
/etc/lvm
/etc/lvm/archive
/etc/lvm/backup
/etc/lvm/cache
/etc/lvm/cache/.cache
/etc/lvm/lvm.conf
/etc/lvm/lvmlocal.conf
/etc/lvm/profile
/etc/lvm/profile/cache-mq.profile
/etc/lvm/profile/cache-smq.profile
/etc/lvm/profile/command_profile_template.profile
/etc/lvm/profile/lvmdbusd.profile
/etc/lvm/profile/metadata_profile_template.profile
/etc/lvm/profile/thin-generic.profile
/etc/lvm/profile/thin-performance.profile
/etc/lvm/profile/vdo-small.profile
/run/lock/lvm
/run/lvm
/usr/lib/.build-id
/usr/lib/.build-id/40
/usr/lib/.build-id/40/56e4b2bc573155a3bbe0179b2f52e99ed20dad
/usr/lib/.build-id/5d
/usr/lib/.build-id/5d/992f05d74e0564c8f5b9f1a1625ae9840785f5
/usr/lib/.build-id/ad
/usr/lib/.build-id/ad/deeaab9b2f0914765cc2bb37f9f2b4b3eadbae
/usr/lib/systemd/system-generators/lvm2-activation-generator
/usr/lib/systemd/system/blk-availability.service
/usr/lib/systemd/system/lvm2-lvmpolld.service
/usr/lib/systemd/system/lvm2-lvmpolld.socket
/usr/lib/systemd/system/lvm2-monitor.service
/usr/lib/systemd/system/lvm2-pvscan@.service
/usr/lib/tmpfiles.d/lvm2.conf
/usr/lib/udev/rules.d/11-dm-lvm.rules
/usr/lib/udev/rules.d/69-dm-lvm-metad.rules
/usr/sbin/fsadm
/usr/sbin/lvchange
/usr/sbin/lvconvert
/usr/sbin/lvcreate
/usr/sbin/lvdisplay
/usr/sbin/lvextend
/usr/sbin/lvm
/usr/sbin/lvmconfig
/usr/sbin/lvmdiskscan
/usr/sbin/lvmdump
/usr/sbin/lvmpolld
/usr/sbin/lvmsadc
/usr/sbin/lvmsar
/usr/sbin/lvreduce
/usr/sbin/lvremove
/usr/sbin/lvrename
/usr/sbin/lvresize
/usr/sbin/lvs
/usr/sbin/lvscan
/usr/sbin/pvchange
/usr/sbin/pvck
/usr/sbin/pvcreate
/usr/sbin/pvdisplay
/usr/sbin/pvmove
/usr/sbin/pvremove
/usr/sbin/pvresize
/usr/sbin/pvs
/usr/sbin/pvscan
/usr/sbin/vgcfgbackup
/usr/sbin/vgcfgrestore
/usr/sbin/vgchange
/usr/sbin/vgck
/usr/sbin/vgconvert
/usr/sbin/vgcreate
/usr/sbin/vgdisplay
/usr/sbin/vgexport
/usr/sbin/vgextend
/usr/sbin/vgimport
/usr/sbin/vgimportclone
/usr/sbin/vgmerge
/usr/sbin/vgmknodes
/usr/sbin/vgreduce
/usr/sbin/vgremove
/usr/sbin/vgrename
/usr/sbin/vgs
/usr/sbin/vgscan
/usr/sbin/vgsplit
/usr/share/doc/lvm2
/usr/share/doc/lvm2/README
/usr/share/doc/lvm2/VERSION
/usr/share/doc/lvm2/WHATS_NEW
/usr/share/doc/lvm2/lvm_fault_handling.txt
/usr/share/licenses/lvm2
/usr/share/licenses/lvm2/COPYING
/usr/share/licenses/lvm2/COPYING.LIB
/usr/share/man/man5/lvm.conf.5.gz
/usr/share/man/man7/lvmcache.7.gz
/usr/share/man/man7/lvmraid.7.gz
/usr/share/man/man7/lvmreport.7.gz
/usr/share/man/man7/lvmsystemid.7.gz
/usr/share/man/man7/lvmthin.7.gz
/usr/share/man/man7/lvmvdo.7.gz
/usr/share/man/man8/fsadm.8.gz
/usr/share/man/man8/lvchange.8.gz
/usr/share/man/man8/lvconvert.8.gz
/usr/share/man/man8/lvcreate.8.gz
/usr/share/man/man8/lvdisplay.8.gz
/usr/share/man/man8/lvextend.8.gz
/usr/share/man/man8/lvm-config.8.gz
/usr/share/man/man8/lvm-dumpconfig.8.gz
/usr/share/man/man8/lvm-fullreport.8.gz
/usr/share/man/man8/lvm-lvpoll.8.gz
/usr/share/man/man8/lvm.8.gz
/usr/share/man/man8/lvm2-activation-generator.8.gz
/usr/share/man/man8/lvmconfig.8.gz
/usr/share/man/man8/lvmdiskscan.8.gz
/usr/share/man/man8/lvmdump.8.gz
/usr/share/man/man8/lvmpolld.8.gz
/usr/share/man/man8/lvmsadc.8.gz
/usr/share/man/man8/lvmsar.8.gz
/usr/share/man/man8/lvreduce.8.gz
/usr/share/man/man8/lvremove.8.gz
/usr/share/man/man8/lvrename.8.gz
/usr/share/man/man8/lvresize.8.gz
/usr/share/man/man8/lvs.8.gz
/usr/share/man/man8/lvscan.8.gz
/usr/share/man/man8/pvchange.8.gz
/usr/share/man/man8/pvck.8.gz
/usr/share/man/man8/pvcreate.8.gz
/usr/share/man/man8/pvdisplay.8.gz
/usr/share/man/man8/pvmove.8.gz
/usr/share/man/man8/pvremove.8.gz
/usr/share/man/man8/pvresize.8.gz
/usr/share/man/man8/pvs.8.gz
/usr/share/man/man8/pvscan.8.gz
/usr/share/man/man8/vgcfgbackup.8.gz
/usr/share/man/man8/vgcfgrestore.8.gz
/usr/share/man/man8/vgchange.8.gz
/usr/share/man/man8/vgck.8.gz
/usr/share/man/man8/vgconvert.8.gz
/usr/share/man/man8/vgcreate.8.gz
/usr/share/man/man8/vgdisplay.8.gz
/usr/share/man/man8/vgexport.8.gz
/usr/share/man/man8/vgextend.8.gz
/usr/share/man/man8/vgimport.8.gz
/usr/share/man/man8/vgimportclone.8.gz
/usr/share/man/man8/vgmerge.8.gz
/usr/share/man/man8/vgmknodes.8.gz
/usr/share/man/man8/vgreduce.8.gz
/usr/share/man/man8/vgremove.8.gz
/usr/share/man/man8/vgrename.8.gz
/usr/share/man/man8/vgs.8.gz
/usr/share/man/man8/vgscan.8.gz
/usr/share/man/man8/vgsplit.8.gz

[root@rocky8 ~]# pvs
[root@rocky8 ~]# pvdisplay 
[root@rocky8 ~]# vgs
[root@rocky8 ~]# vgdisplay 
[root@rocky8 ~]# lvs
[root@rocky8 ~]# lvdisplay 

[root@rocky8 ~]# pvcreate /dev/sd{b1,c} #添加物理卷
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdc" successfully created.
[root@rocky8 ~]# pvs
  PV         VG Fmt  Attr PSize PFree
  /dev/sdb1     lvm2 ---  4.00g 4.00g
  /dev/sdc      lvm2 ---  7.00g 7.00g
[root@rocky8 ~]# pvdisplay 
  "/dev/sdb1" is a new physical volume of "4.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               4.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               7zvfiq-wsl0-34pY-CzqC-PDfT-uI7L-8M0X7X
   
  "/dev/sdc" is a new physical volume of "7.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc
  VG Name               
  PV Size               7.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0

[root@rocky8 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0   50G  0 part /data
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0    2G  0 part [SWAP]
sdb      8:16   0   10G  0 disk 
└─sdb1   8:17   0    4G  0 part 
sdc      8:32   0    7G  0 disk 
sr0     11:0    1  9.2G  0 rom  

[root@rocky8 ~]# vgcreate --help
  vgcreate - Create a volume group

  vgcreate VG_new PV ...
	[ -A|--autobackup y|n ]
	[ -c|--clustered y|n ]
	[ -l|--maxlogicalvolumes Number ]
	[ -p|--maxphysicalvolumes Number ]
	[ -M|--metadatatype lvm2 ]
	[ -s|--physicalextentsize Size[m|UNIT] ]
	[ -f|--force ]
	[ -Z|--zero y|n ]
	[    --addtag Tag ]
	[    --alloc contiguous|cling|cling_by_tags|normal|anywhere|inherit ]
	[    --metadataprofile String ]
	[    --labelsector Number ]
	[    --metadatasize Size[m|UNIT] ]
	[    --pvmetadatacopies 0|1|2 ]
	[    --vgmetadatacopies all|unmanaged|Number ]
	[    --reportformat basic|json ]
	[    --dataalignment Size[k|UNIT] ]
	[    --dataalignmentoffset Size[k|UNIT] ]
	[    --shared ]
	[    --systemid String ]
	[    --locktype sanlock|dlm|none ]
	[ COMMON_OPTIONS ]

  Common options for lvm:
	[ -d|--debug ]
	[ -h|--help ]
	[ -q|--quiet ]
	[ -v|--verbose ]
	[ -y|--yes ]
	[ -t|--test ]
	[    --commandprofile String ]
	[    --config String ]
	[    --driverloaded y|n ]
	[    --nolocking ]
	[    --lockopt String ]
	[    --longhelp ]
	[    --profile String ]
	[    --version ]

  Use --longhelp to show all options and advanced commands.

[root@rocky8 ~]# vgs
[root@rocky8 ~]# vgcreate mysql-vg /dev/sd{b1,c} #创建卷组
  Volume group "mysql-vg" successfully created

[root@rocky8 ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree 
  mysql-vg   2   0   0 wz--n- 10.99g 10.99g
[root@rocky8 ~]# vgdisplay 
  --- Volume group ---
  VG Name               mysql-vg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               10.99 GiB
  PE Size               4.00 MiB #默认PE大小M
  Total PE              2814 #总共有2814个PE
  Alloc PE / Size       0 / 0  #目前使用PE为0  
  Free  PE / Size       2814 / 10.99 GiB #剩余PE个数和大小
  VG UUID               vTGFJ3-krhJ-KPLw-LooL-kisi-N0eJ-8mZCzQ

[root@rocky8 ~]# pvs
  PV         VG       Fmt  Attr PSize  PFree 
  /dev/sdb1  mysql-vg lvm2 a--  <4.00g <4.00g
  /dev/sdc   mysql-vg lvm2 a--  <7.00g <7.00g
#两个物理卷加到vg卷组

[root@rocky8 ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               mysql-vg
  PV Size               4.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              1023
  Free PE               1023
  Allocated PE          0
  PV UUID               PyYxIb-T7K7-X7XF-ZBQW-BWpC-xF3D-Ff2A4i
   
  --- Physical volume ---
  PV Name               /dev/sdc
  VG Name               mysql-vg
  PV Size               7.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              1791
  Free PE               1791
  Allocated PE          0
  PV UUID               nFvzKO-UN0g-sbBA-UCbm-sy9c-wcIV-M2fd4Y

[root@rocky8 ~]# lvcreate --help
  lvcreate - Create a logical volume

  Create a linear LV.
  lvcreate -L|--size Size[m|UNIT] VG
	[ -l|--extents Number[PERCENT] ]
	[    --type linear ]
	[ COMMON_OPTIONS ]
	[ PV ... ]

  Create a striped LV (infers --type striped).
  lvcreate -i|--stripes Number -L|--size Size[m|UNIT] VG
	[ -l|--extents Number[PERCENT] ]
	[ -I|--stripesize Size[k|UNIT] ]
	[ COMMON_OPTIONS ]
	[ PV ... ]

  Create a raid1 or mirror LV (infers --type raid1|mirror).
  lvcreate -m|--mirrors Number -L|--size Size[m|UNIT] VG
	[ -l|--extents Number[PERCENT] ]
	[ -R|--regionsize Size[m|UNIT] ]
	[    --mirrorlog core|disk ]
	[    --minrecoveryrate Size[k|UNIT] ]
	[    --maxrecoveryrate Size[k|UNIT] ]
	[ COMMON_OPTIONS ]
	[ PV ... ]

  Create a raid LV (a specific raid level must be used, e.g. raid1).
  lvcreate --type raid -L|--size Size[m|UNIT] VG
	[ -l|--extents Number[PERCENT] ]
	[ -m|--mirrors Number ]
	[ -i|--stripes Number ]
	[ -I|--stripesize Size[k|UNIT] ]
	[ -R|--regionsize Size[m|UNIT] ]
	[    --minrecoveryrate Size[k|UNIT] ]
	[    --maxrecoveryrate Size[k|UNIT] ]
	[    --raidintegrity y|n ]
	[    --raidintegritymode String ]
	[    --raidintegrityblocksize Number ]
	[ COMMON_OPTIONS ]
	[ PV ... ]

  Create a raid10 LV.
  lvcreate -m|--mirrors Number -i|--stripes Number -L|--size Size[m|UNIT] VG
	[ -l|--extents Number[PERCENT] ]
	[ -I|--stripesize Size[k|UNIT] ]
	[ -R|--regionsize Size[m|UNIT] ]
	[    --minrecoveryrate Size[k|UNIT] ]
	[    --maxrecoveryrate Size[k|UNIT] ]
	[ COMMON_OPTIONS ]
	[ PV ... ]

  Create a COW snapshot LV of an origin LV.
  lvcreate -s|--snapshot -L|--size Size[m|UNIT] LV
	[ -l|--extents Number[PERCENT] ]
	[ -i|--stripes Number ]
	[ -I|--stripesize Size[k|UNIT] ]
	[ -c|--chunksize Size[k|UNIT] ]
	[    --type snapshot ]
	[ COMMON_OPTIONS ]
	[ PV ... ]

  Create a thin pool.
  lvcreate --type thin-pool -L|--size Size[m|UNIT] VG
	[ -l|--extents Number[PERCENT] ]
	[ -c|--chunksize Size[k|UNIT] ]
	[ -i|--stripes Number ]
	[ -I|--stripesize Size[k|UNIT] ]
	[    --thinpool LV_new ]
	[    --poolmetadatasize Size[m|UNIT] ]
	[    --poolmetadataspare y|n ]
	[    --discards passdown|nopassdown|ignore ]
	[    --errorwhenfull y|n ]
	[ COMMON_OPTIONS ]
	[ PV ... ]

  Create a cache pool.
  lvcreate --type cache-pool -L|--size Size[m|UNIT] VG
	[ -l|--extents Number[PERCENT] ]
	[ -H|--cache ]
	[ -c|--chunksize Size[k|UNIT] ]
	[    --poolmetadatasize Size[m|UNIT] ]
	[    --poolmetadataspare y|n ]
	[    --cachemode writethrough|writeback|passthrough ]
	[    --cachepolicy String ]
	[    --cachesettings String ]
	[    --cachemetadataformat auto|1|2 ]
	[ COMMON_OPTIONS ]
	[ PV ... ]

  Create a thin LV in a thin pool (infers --type thin).
  lvcreate -V|--virtualsize Size[m|UNIT] --thinpool LV_thinpool VG
	[ -T|--thin ]
	[    --type thin ]
	[    --discards passdown|nopassdown|ignore ]
	[    --errorwhenfull y|n ]
	[ COMMON_OPTIONS ]

  Create a thin LV that is a snapshot of an existing thin LV 
  (infers --type thin).
  lvcreate -s|--snapshot LV_thin
	[    --type thin ]
	[    --discards passdown|nopassdown|ignore ]
	[    --errorwhenfull y|n ]
	[ COMMON_OPTIONS ]

  Create a thin LV that is a snapshot of an external origin LV.
  lvcreate --type thin --thinpool LV_thinpool LV
	[ -T|--thin ]
	[ -c|--chunksize Size[k|UNIT] ]
	[    --poolmetadatasize Size[m|UNIT] ]
	[    --poolmetadataspare y|n ]
	[    --discards passdown|nopassdown|ignore ]
	[    --errorwhenfull y|n ]
	[ COMMON_OPTIONS ]

  Create a LV that returns VDO when used.
  lvcreate --type vdo -L|--size Size[m|UNIT] VG
	[ -l|--extents Number[PERCENT] ]
	[ -V|--virtualsize Size[m|UNIT] ]
	[ -i|--stripes Number ]
	[ -I|--stripesize Size[k|UNIT] ]
	[    --vdo ]
	[    --vdopool LV_new ]
	[    --compression y|n ]
	[    --deduplication y|n ]
	[ COMMON_OPTIONS ]
	[ PV ... ]

  Create a thin LV, first creating a thin pool for it, 
  where the new thin pool is named by the --thinpool arg.
  lvcreate --type thin -V|--virtualsize Size[m|UNIT] -L|--size Size[m|UNIT] --thinpool LV_new
	[ -l|--extents Number[PERCENT] ]
	[ -T|--thin ]
	[ -c|--chunksize Size[k|UNIT] ]
	[ -i|--stripes Number ]
	[ -I|--stripesize Size[k|UNIT] ]
	[    --poolmetadatasize Size[m|UNIT] ]
	[    --poolmetadataspare y|n ]
	[    --discards passdown|nopassdown|ignore ]
	[    --errorwhenfull y|n ]
	[ COMMON_OPTIONS ]
	[ PV ... ]

  Create a new LV, then attach the specified cachepool 
  which converts the new LV to type cache.
  lvcreate --type cache -L|--size Size[m|UNIT] --cachepool LV_cachepool VG
	[ -l|--extents Number[PERCENT] ]
	[ -H|--cache ]
	[ -c|--chunksize Size[k|UNIT] ]
	[ -i|--stripes Number ]
	[ -I|--stripesize Size[k|UNIT] ]
	[    --poolmetadatasize Size[m|UNIT] ]
	[    --poolmetadataspare y|n ]
	[    --cachemode writethrough|writeback|passthrough ]
	[    --cachepolicy String ]
	[    --cachesettings String ]
	[    --cachemetadataformat auto|1|2 ]
	[ COMMON_OPTIONS ]
	[ PV ... ]

  Create a new LV, then attach the specified cachevol 
  which converts the new LV to type cache.
  lvcreate --type cache -L|--size Size[m|UNIT] --cachevol LV VG
	[ -l|--extents Number[PERCENT] ]
	[ -c|--chunksize Size[k|UNIT] ]
	[ -i|--stripes Number ]
	[ -I|--stripesize Size[k|UNIT] ]
	[    --cachemode writethrough|writeback|passthrough ]
	[    --cachepolicy String ]
	[    --cachesettings String ]
	[    --cachemetadataformat auto|1|2 ]
	[ COMMON_OPTIONS ]
	[ PV ... ]

  Create a new LV, then attach a cachevol created from 
  the specified cache device, which converts the 
  new LV to type cache.
  lvcreate --type cache -L|--size Size[m|UNIT] --cachedevice PV VG
	[ -l|--extents Number[PERCENT] ]
	[ -c|--chunksize Size[k|UNIT] ]
	[ -i|--stripes Number ]
	[ -I|--stripesize Size[k|UNIT] ]
	[    --cachemode writethrough|writeback|passthrough ]
	[    --cachepolicy String ]
	[    --cachesettings String ]
	[    --cachemetadataformat auto|1|2 ]
	[    --cachesize Size[m|UNIT] ]
	[ COMMON_OPTIONS ]
	[ PV ... ]

  Create a new LV, then attach the specified cachevol 
  which converts the new LV to type writecache.
  lvcreate --type writecache -L|--size Size[m|UNIT] --cachevol LV VG
	[ -l|--extents Number[PERCENT] ]
	[ -i|--stripes Number ]
	[ -I|--stripesize Size[k|UNIT] ]
	[    --cachesettings String ]
	[ COMMON_OPTIONS ]
	[ PV ... ]

  Create a new LV, then attach a cachevol created from 
  the specified cache device, which converts the 
  new LV to type writecache.
  lvcreate --type writecache -L|--size Size[m|UNIT] --cachedevice PV VG
	[ -l|--extents Number[PERCENT] ]
	[ -i|--stripes Number ]
	[ -I|--stripesize Size[k|UNIT] ]
	[    --cachesize Size[m|UNIT] ]
	[    --cachesettings String ]
	[ COMMON_OPTIONS ]
	[ PV ... ]

  Common options for command:
	[ -a|--activate y|n|ay ]
	[ -A|--autobackup y|n ]
	[ -C|--contiguous y|n ]
	[ -M|--persistent y|n ]
	[ -j|--major Number ]
	[ -k|--setactivationskip y|n ]
	[ -K|--ignoreactivationskip ]
	[ -n|--name String ]
	[ -p|--permission rw|r ]
	[ -r|--readahead auto|none|Number ]
	[ -W|--wipesignatures y|n ]
	[ -Z|--zero y|n ]
	[    --addtag Tag ]
	[    --alloc contiguous|cling|cling_by_tags|normal|anywhere|inherit ]
	[    --ignoremonitoring ]
	[    --metadataprofile String ]
	[    --minor Number ]
	[    --monitor y|n ]
	[    --nosync ]
	[    --noudevsync ]
	[    --reportformat basic|json ]

  Common options for lvm:
	[ -d|--debug ]
	[ -h|--help ]
	[ -q|--quiet ]
	[ -v|--verbose ]
	[ -y|--yes ]
	[ -t|--test ]
	[    --commandprofile String ]
	[    --config String ]
	[    --driverloaded y|n ]
	[    --nolocking ]
	[    --lockopt String ]
	[    --longhelp ]
	[    --profile String ]
	[    --version ]

  Use --longhelp to show all options and advanced commands.

#-L  指定大小(按容量算)
#-l  指定大小(按PE个数算)

[root@rocky8 ~]# lvcreate -L 2G -n data mysql-vg #创建逻辑卷
  Logical volume "data" created.
  
[root@rocky8 ~]# lvs
  LV   VG       Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data mysql-vg -wi-a----- 2.00g

[root@rocky8 ~]# lsblk
NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                  8:0    0  200G  0 disk 
├─sda1               8:1    0    1G  0 part /boot
├─sda2               8:2    0  100G  0 part /
├─sda3               8:3    0   50G  0 part /data
├─sda4               8:4    0    1K  0 part 
└─sda5               8:5    0    2G  0 part [SWAP]
sdb                  8:16   0   10G  0 disk 
└─sdb1               8:17   0    4G  0 part 
  └─mysql--vg-data 253:0    0    2G  0 lvm  #创建的逻辑卷
sdc                  8:32   0    7G  0 disk 
sr0                 11:0    1  9.2G  0 rom 

[root@rocky8 ~]# blkid
/dev/sda1: UUID="c72110fc-5dae-470c-9940-97ba978c7dc7" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-01"
/dev/sda2: UUID="57f3a077-bb67-42c3-9e6c-bc8a0fed3776" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-02"
/dev/sda3: UUID="02ae6744-8c28-4c86-b85b-e113c06a834f" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-03"
/dev/sda5: UUID="4f387202-0a81-4202-9e12-90d04af4d308" TYPE="swap" PARTUUID="43d7507d-05"
/dev/sdb1: UUID="PyYxIb-T7K7-X7XF-ZBQW-BWpC-xF3D-Ff2A4i" TYPE="LVM2_member" PARTUUID="744070d0-01" #/dev/sdb1 是逻辑卷成员
/dev/sr0: BLOCK_SIZE="2048" UUID="2021-06-20-16-16-21-00" LABEL="Rocky-8-4-x86_64-dvd" TYPE="iso9660" PTUUID="4a14a4ee" PTTYPE="dos"
/dev/sdc: UUID="nFvzKO-UN0g-sbBA-UCbm-sy9c-wcIV-M2fd4Y" TYPE="LVM2_member" #/dev/sdc是逻辑卷成员

[root@rocky8 ~]# lvs
  LV   VG       Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data mysql-vg -wi-a----- 2.00g                                                    
[root@rocky8 ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/mysql-vg/data
  LV Name                data
  VG Name                mysql-vg
  LV UUID                7OXyB8-YuPH-ZXeh-9lU2-KJ2d-U2c3-11GE2b
  LV Write Access        read/write
  LV Creation host, time rocky8, 2021-10-17 16:04:01 +0800
  LV Status              available
  # open                 0
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

[root@rocky8 ~]# ll /dev/mysql-vg/data
lrwxrwxrwx 1 root root 7 Oct 17 17:02 /dev/mysql-vg/data -> ../dm-0
#它是一个软链接

[root@rocky8 ~]# ll /dev/mapper/mysql--vg-data
lrwxrwxrwx 1 root root 7 Oct 17 17:02 /dev/mapper/mysql--vg-data -> ../dm-0
#还有另一个名字  /dev/mapper/mysql--vg-data 

[root@rocky8 ~]# lvs
  LV   VG       Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data mysql-vg -wi-a----- 2.00g                                                    
[root@rocky8 ~]# vgdisplay 
  --- Volume group ---
  VG Name               mysql-vg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               10.99 GiB
  PE Size               4.00 MiB
  Total PE              2814
  Alloc PE / Size       512 / 2.00 GiB #使用512PE/容量大小2G
  Free  PE / Size       2302 / 8.99 GiB
  VG UUID               vTGFJ3-krhJ-KPLw-LooL-kisi-N0eJ-8mZCzQ

[root@rocky8 ~]# lvcreate -L 1G -n log mysql-vg
  Logical volume "log" created.
[root@rocky8 ~]# lvs
  LV   VG       Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data mysql-vg -wi-a----- 2.00g                                                    
  log  mysql-vg -wi-a----- 1.00g                                                    
[root@rocky8 ~]# vgdisplay 
  --- Volume group ---
  VG Name               mysql-vg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               10.99 GiB
  PE Size               4.00 MiB
  Total PE              2814
  Alloc PE / Size       768 / 3.00 GiB
  Free  PE / Size       2046 / 7.99 GiB
  VG UUID               vTGFJ3-krhJ-KPLw-LooL-kisi-N0eJ-8mZCzQ

[root@rocky8 ~]# mkfs.ext4 /dev/mysql-vg/data #创建文件系统
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 524288 4k blocks and 131072 inodes
Filesystem UUID: eb8fc2ac-50fa-432c-b628-1b6c8faeba5e
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

[root@rocky8 ~]# blkid
/dev/sda1: UUID="c72110fc-5dae-470c-9940-97ba978c7dc7" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-01"
/dev/sda2: UUID="57f3a077-bb67-42c3-9e6c-bc8a0fed3776" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-02"
/dev/sda3: UUID="02ae6744-8c28-4c86-b85b-e113c06a834f" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-03"
/dev/sda5: UUID="4f387202-0a81-4202-9e12-90d04af4d308" TYPE="swap" PARTUUID="43d7507d-05"
/dev/sdb1: UUID="PyYxIb-T7K7-X7XF-ZBQW-BWpC-xF3D-Ff2A4i" TYPE="LVM2_member" PARTUUID="744070d0-01"
/dev/sr0: BLOCK_SIZE="2048" UUID="2021-06-20-16-16-21-00" LABEL="Rocky-8-4-x86_64-dvd" TYPE="iso9660" PTUUID="4a14a4ee" PTTYPE="dos"
/dev/sdc: UUID="nFvzKO-UN0g-sbBA-UCbm-sy9c-wcIV-M2fd4Y" TYPE="LVM2_member"
/dev/mapper/mysql--vg-data: UUID="eb8fc2ac-50fa-432c-b628-1b6c8faeba5e" BLOCK_SIZE="4096" TYPE="ext4"

[root@rocky8 ~]# mkfs -t xfs /dev/mysql-vg/log
meta-data=/dev/mysql-vg/log      isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

4.2.4 扩展和缩减逻辑卷

4.2.4.1 在线扩展逻辑卷

lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
#针对ext
resize2fs /dev/VG_NAME/LV_NAME
#针对xfs
xfs_growfs MOUNTPOINT

lvresize -r -l +100%FREE /dev/VG_NAME/LV_NAME

范例:

[root@rocky8 ~]# blkid
/dev/sda1: UUID="c72110fc-5dae-470c-9940-97ba978c7dc7" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-01"
/dev/sda2: UUID="57f3a077-bb67-42c3-9e6c-bc8a0fed3776" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-02"
/dev/sda3: UUID="02ae6744-8c28-4c86-b85b-e113c06a834f" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-03"
/dev/sda5: UUID="4f387202-0a81-4202-9e12-90d04af4d308" TYPE="swap" PARTUUID="43d7507d-05"
/dev/sdb1: UUID="PyYxIb-T7K7-X7XF-ZBQW-BWpC-xF3D-Ff2A4i" TYPE="LVM2_member" PARTUUID="744070d0-01"
/dev/sr0: BLOCK_SIZE="2048" UUID="2021-06-20-16-16-21-00" LABEL="Rocky-8-4-x86_64-dvd" TYPE="iso9660" PTUUID="4a14a4ee" PTTYPE="dos"
/dev/sdc: UUID="nFvzKO-UN0g-sbBA-UCbm-sy9c-wcIV-M2fd4Y" TYPE="LVM2_member"
/dev/mapper/mysql--vg-data: UUID="eb8fc2ac-50fa-432c-b628-1b6c8faeba5e" BLOCK_SIZE="4096" TYPE="ext4"
/dev/mapper/mysql--vg-log: UUID="a3528ff7-e3e7-4658-a9ce-fe90a4bc5566" BLOCK_SIZE="512" TYPE="xfs"

[root@rocky8 ~]# mkdir /data/mysql/data -pv
mkdir: created directory '/data/mysql'
mkdir: created directory '/data/mysql/data'
[root@rocky8 ~]# mkdir /data/mysql/logs -pv
mkdir: created directory '/data/mysql/logs'

[root@rocky8 ~]# mount /dev/mysql-vg/data /data/mysql/data/
[root@rocky8 ~]# mount /dev/mysql-vg/log /data/mysql/logs/

[root@rocky8 data]# cd /data/mysql/data/
[root@rocky8 data]# ls
lost+found
[root@rocky8 data]# cp /etc/fstab a.txt
[root@rocky8 data]# cp /etc/fstab b.txt
[root@rocky8 data]# ls
a.txt  b.txt  lost+found

[root@rocky8 data]# df -Th
Filesystem                 Type      Size  Used Avail Use% Mounted on
devtmpfs                   devtmpfs  374M     0  374M   0% /dev
tmpfs                      tmpfs     392M     0  392M   0% /dev/shm
tmpfs                      tmpfs     392M  5.6M  386M   2% /run
tmpfs                      tmpfs     392M     0  392M   0% /sys/fs/cgroup
/dev/sda2                  xfs       100G  2.6G   98G   3% /
/dev/sda3                  xfs        50G  390M   50G   1% /data
/dev/sda1                  xfs      1014M  188M  827M  19% /boot
tmpfs                      tmpfs      79M     0   79M   0% /run/user/0
/dev/mapper/mysql--vg-data ext4      2.0G  6.1M  1.8G   1% /data/mysql/data
/dev/mapper/mysql--vg-log  xfs      1014M   40M  975M   4% /data/mysql/logs

#要想扩逻辑卷,先要卷组中有空间

[root@rocky8 data]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree
  mysql-vg   2   2   0 wz--n- 10.99g 7.99g #卷组还有8G空间

[root@rocky8 data]# cat a.txt 

#
# /etc/fstab
# Created by anaconda on Wed Oct  6 11:16:34 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 /                       xfs     defaults        0 0
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot                   xfs     defaults        0 0
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data                   xfs     defaults        0 0
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none                    swap    defaults        0 0
#扩展不影响用户访问

[root@rocky8 data]# vgdisplay 
  --- Volume group ---
  VG Name               mysql-vg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               10.99 GiB
  PE Size               4.00 MiB
  Total PE              2814
  Alloc PE / Size       768 / 3.00 GiB
  Free  PE / Size       2046 / 7.99 GiB
  VG UUID               vTGFJ3-krhJ-KPLw-LooL-kisi-N0eJ-8mZCzQ

[root@rocky8 data]# lvextend -L +2G /dev/mysql-vg/data #扩展逻辑卷
  Size of logical volume mysql-vg/data changed from 2.00 GiB (512 extents) to 4.00 GiB (1024 extents).
  Logical volume mysql-vg/data successfully resized.
#扩展是在线扩展,不需要取消挂载

[root@rocky8 data]# lvs
  LV   VG       Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data mysql-vg -wi-ao---- 4.00g   #现在是4G                                                 
  log  mysql-vg -wi-ao---- 1.00g 
  
  [root@rocky8 data]# df -Th
Filesystem                 Type      Size  Used Avail Use% Mounted on
devtmpfs                   devtmpfs  374M     0  374M   0% /dev
tmpfs                      tmpfs     392M     0  392M   0% /dev/shm
tmpfs                      tmpfs     392M  5.6M  386M   2% /run
tmpfs                      tmpfs     392M     0  392M   0% /sys/fs/cgroup
/dev/sda2                  xfs       100G  2.6G   98G   3% /
/dev/sda3                  xfs        50G  390M   50G   1% /data
/dev/sda1                  xfs      1014M  188M  827M  19% /boot
tmpfs                      tmpfs      79M     0   79M   0% /run/user/0
/dev/mapper/mysql--vg-data ext4      2.0G  6.1M  1.8G   1% /data/mysql/data #这里看不到扩展后的容量
/dev/mapper/mysql--vg-log  xfs      1014M   40M  975M   4% /data/mysql/logs
#因为df看到的是文件系统的大小,lvs 看到的是空间大小

[root@rocky8 data]# resize2fs /dev/mysql-vg/data #resize2fs 只支持ext系列分区格式,它的作用是把扩容的空间与现有的空间的文件系统进行同步
resize2fs 1.45.6 (20-Mar-2020)
Filesystem at /dev/mysql-vg/data is mounted on /data/mysql/data; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mysql-vg/data is now 1048576 (4k) blocks long.

[root@rocky8 data]# df -Th
Filesystem                 Type      Size  Used Avail Use% Mounted on
devtmpfs                   devtmpfs  374M     0  374M   0% /dev
tmpfs                      tmpfs     392M     0  392M   0% /dev/shm
tmpfs                      tmpfs     392M  5.6M  386M   2% /run
tmpfs                      tmpfs     392M     0  392M   0% /sys/fs/cgroup
/dev/sda2                  xfs       100G  2.6G   98G   3% /
/dev/sda3                  xfs        50G  390M   50G   1% /data
/dev/sda1                  xfs      1014M  188M  827M  19% /boot
tmpfs                      tmpfs      79M     0   79M   0% /run/user/0
/dev/mapper/mysql--vg-data ext4      3.9G  8.0M  3.7G   1% /data/mysql/data #现在是扩展后的容量了
/dev/mapper/mysql--vg-log  xfs      1014M   40M  975M   4% /data/mysql/logs

[root@rocky8 data]# lvextend -l +100%free /dev/mysql-vg/log #+100%free  把卷组剩余空间全部使用
  Size of logical volume mysql-vg/log changed from 1.00 GiB (256 extents) to 6.99 GiB (1790 extents).
  Logical volume mysql-vg/log successfully resized.

[root@rocky8 data]# lvs
  LV   VG       Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data mysql-vg -wi-ao---- 4.00g                                                    
  log  mysql-vg -wi-ao---- 6.99g                                                    
[root@rocky8 data]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree
  mysql-vg   2   2   0 wz--n- 10.99g    0  #卷组空间没有了

[root@rocky8 data]# xfs_growfs /data/mysql/logs/ #xfs_growfs同步xfs文件系统
meta-data=/dev/mapper/mysql--vg-log isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 262144 to 1832960

[root@rocky8 data]# df -Th
Filesystem                 Type      Size  Used Avail Use% Mounted on
devtmpfs                   devtmpfs  374M     0  374M   0% /dev
tmpfs                      tmpfs     392M     0  392M   0% /dev/shm
tmpfs                      tmpfs     392M  5.6M  386M   2% /run
tmpfs                      tmpfs     392M     0  392M   0% /sys/fs/cgroup
/dev/sda2                  xfs       100G  2.6G   98G   3% /
/dev/sda3                  xfs        50G  390M   50G   1% /data
/dev/sda1                  xfs      1014M  188M  827M  19% /boot
tmpfs                      tmpfs      79M     0   79M   0% /run/user/0
/dev/mapper/mysql--vg-data ext4      3.9G  8.0M  3.7G   1% /data/mysql/data
/dev/mapper/mysql--vg-log  xfs       7.0G   84M  7.0G   2% /data/mysql/logs #文件系统已同步

[root@rocky8 data]# lsblk
NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                  8:0    0  200G  0 disk 
├─sda1               8:1    0    1G  0 part /boot
├─sda2               8:2    0  100G  0 part /
├─sda3               8:3    0   50G  0 part /data
├─sda4               8:4    0    1K  0 part 
└─sda5               8:5    0    2G  0 part [SWAP]
sdb                  8:16   0   10G  0 disk 
└─sdb1               8:17   0    4G  0 part 
  ├─mysql--vg-data 253:0    0    4G  0 lvm  /data/mysql/data
  └─mysql--vg-log  253:1    0    7G  0 lvm  /data/mysql/logs
sdc                  8:32   0    7G  0 disk 
├─mysql--vg-data   253:0    0    4G  0 lvm  /data/mysql/data
└─mysql--vg-log    253:1    0    7G  0 lvm  /data/mysql/logs
sr0                 11:0    1  9.2G  0 rom 

[root@rocky8 data]# scandisk
[root@rocky8 data]# lsblk
NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                  8:0    0  200G  0 disk 
├─sda1               8:1    0    1G  0 part /boot
├─sda2               8:2    0  100G  0 part /
├─sda3               8:3    0   50G  0 part /data
├─sda4               8:4    0    1K  0 part 
└─sda5               8:5    0    2G  0 part [SWAP]
sdb                  8:16   0   10G  0 disk 
└─sdb1               8:17   0    4G  0 part 
  ├─mysql--vg-data 253:0    0    4G  0 lvm  /data/mysql/data
  └─mysql--vg-log  253:1    0    7G  0 lvm  /data/mysql/logs
sdc                  8:32   0    7G  0 disk 
├─mysql--vg-data   253:0    0    4G  0 lvm  /data/mysql/data
└─mysql--vg-log    253:1    0    7G  0 lvm  /data/mysql/logs
sdd                  8:48   0    2G  0 disk #添加一块2G硬盘 
sr0                 11:0    1  9.2G  0 rom  

[root@rocky8 data]# pvs
  PV         VG       Fmt  Attr PSize  PFree
  /dev/sdb1  mysql-vg lvm2 a--  <4.00g    0 
  /dev/sdc   mysql-vg lvm2 a--  <7.00g    0 
[root@rocky8 data]#  pvcreate /dev/sdd
  Physical volume "/dev/sdd" successfully created.
[root@rocky8 data]# pvs
  PV         VG       Fmt  Attr PSize  PFree
  /dev/sdb1  mysql-vg lvm2 a--  <4.00g    0 
  /dev/sdc   mysql-vg lvm2 a--  <7.00g    0 
  /dev/sdd            lvm2 ---   2.00g 2.00g

[root@rocky8 data]# blkid
/dev/sda1: UUID="c72110fc-5dae-470c-9940-97ba978c7dc7" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-01"
/dev/sda2: UUID="57f3a077-bb67-42c3-9e6c-bc8a0fed3776" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-02"
/dev/sda3: UUID="02ae6744-8c28-4c86-b85b-e113c06a834f" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-03"
/dev/sda5: UUID="4f387202-0a81-4202-9e12-90d04af4d308" TYPE="swap" PARTUUID="43d7507d-05"
/dev/sdb1: UUID="PyYxIb-T7K7-X7XF-ZBQW-BWpC-xF3D-Ff2A4i" TYPE="LVM2_member" PARTUUID="744070d0-01"
/dev/sr0: BLOCK_SIZE="2048" UUID="2021-06-20-16-16-21-00" LABEL="Rocky-8-4-x86_64-dvd" TYPE="iso9660" PTUUID="4a14a4ee" PTTYPE="dos"
/dev/sdc: UUID="nFvzKO-UN0g-sbBA-UCbm-sy9c-wcIV-M2fd4Y" TYPE="LVM2_member"
/dev/mapper/mysql--vg-data: UUID="eb8fc2ac-50fa-432c-b628-1b6c8faeba5e" BLOCK_SIZE="4096" TYPE="ext4"
/dev/mapper/mysql--vg-log: UUID="a3528ff7-e3e7-4658-a9ce-fe90a4bc5566" BLOCK_SIZE="512" TYPE="xfs"
/dev/sdd: UUID="mncyA5-2Iap-DNAl-Jctz-767t-em77-hMwOYh" TYPE="LVM2_member"

[root@rocky8 data]#  vgextend mysql-vg /dev/sdd #扩展卷组
  Volume group "mysql-vg" successfully extended

[root@rocky8 data]# pvs
  PV         VG       Fmt  Attr PSize  PFree 
  /dev/sdb1  mysql-vg lvm2 a--  <4.00g     0 
  /dev/sdc   mysql-vg lvm2 a--  <7.00g     0 
  /dev/sdd   mysql-vg lvm2 a--  <2.00g <2.00g #现在属于卷组成员

[root@rocky8 data]# vgs
  VG       #PV #LV #SN Attr   VSize   VFree 
  mysql-vg   3   2   0 wz--n- <12.99g <2.00g #多了2G剩余空间

[root@rocky8 data]# df -Th
Filesystem                 Type      Size  Used Avail Use% Mounted on
devtmpfs                   devtmpfs  374M     0  374M   0% /dev
tmpfs                      tmpfs     392M     0  392M   0% /dev/shm
tmpfs                      tmpfs     392M  5.6M  386M   2% /run
tmpfs                      tmpfs     392M     0  392M   0% /sys/fs/cgroup
/dev/sda2                  xfs       100G  2.6G   98G   3% /
/dev/sda3                  xfs        50G  390M   50G   1% /data
/dev/sda1                  xfs      1014M  188M  827M  19% /boot
tmpfs                      tmpfs      79M     0   79M   0% /run/user/0
/dev/mapper/mysql--vg-data ext4      3.9G  8.0M  3.7G   1% /data/mysql/data
/dev/mapper/mysql--vg-log  xfs       7.0G   84M  7.0G   2% /data/mysql/logs

[root@rocky8 data]# lvextend -r -l +50%free /dev/mysql-vg/data #加-r  把文件系统也同步
  Size of logical volume mysql-vg/data changed from 4.00 GiB (1024 extents) to 5.00 GiB (1280 extents).
  Logical volume mysql-vg/data successfully resized.
resize2fs 1.45.6 (20-Mar-2020)
Filesystem at /dev/mapper/mysql--vg-data is mounted on /data/mysql/data; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/mysql--vg-data is now 1310720 (4k) blocks long.

[root@rocky8 data]# lvs
  LV   VG       Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data mysql-vg -wi-ao---- 5.00g                                                    
  log  mysql-vg -wi-ao---- 6.99g 
  
  [root@rocky8 data]# df -Th
Filesystem                 Type      Size  Used Avail Use% Mounted on
devtmpfs                   devtmpfs  374M     0  374M   0% /dev
tmpfs                      tmpfs     392M     0  392M   0% /dev/shm
tmpfs                      tmpfs     392M  5.6M  386M   2% /run
tmpfs                      tmpfs     392M     0  392M   0% /sys/fs/cgroup
/dev/sda2                  xfs       100G  2.6G   98G   3% /
/dev/sda3                  xfs        50G  390M   50G   1% /data
/dev/sda1                  xfs      1014M  188M  827M  19% /boot
tmpfs                      tmpfs      79M     0   79M   0% /run/user/0
/dev/mapper/mysql--vg-data ext4      4.9G  8.0M  4.7G   1% /data/mysql/data #文件系统也同步了
/dev/mapper/mysql--vg-log  xfs       7.0G   84M  7.0G   2% /data/mysql/logs

[root@rocky8 data]# ls
a.txt  b.txt  lost+found
[root@rocky8 data]# cat a.txt

#
# /etc/fstab
# Created by anaconda on Wed Oct  6 11:16:34 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=57f3a077-bb67-42c3-9e6c-bc8a0fed3776 /                       xfs     defaults        0 0
UUID=c72110fc-5dae-470c-9940-97ba978c7dc7 /boot                   xfs     defaults        0 0
UUID=02ae6744-8c28-4c86-b85b-e113c06a834f /data                   xfs     defaults        0 0
UUID=4f387202-0a81-4202-9e12-90d04af4d308 none                    swap    defaults        0 0

4.2.4.2 缩减逻辑卷

注意:缩减有数据损坏的风险,建议先备份再缩减,xfs文件系统不支持缩减

umount /dev/VG_NAME/LV_NAME
e2fsck -f /dev/VG_NAME/LV_NAME
resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT]
lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME
mount

范例:

[root@rocky8 data]# pwd
/data/mysql/data
[root@rocky8 data]# df -Th
Filesystem                 Type      Size  Used Avail Use% Mounted on
devtmpfs                   devtmpfs  374M     0  374M   0% /dev
tmpfs                      tmpfs     392M     0  392M   0% /dev/shm
tmpfs                      tmpfs     392M  5.6M  386M   2% /run
tmpfs                      tmpfs     392M     0  392M   0% /sys/fs/cgroup
/dev/sda2                  xfs       100G  2.6G   98G   3% /
/dev/sda3                  xfs        50G  390M   50G   1% /data
/dev/sda1                  xfs      1014M  188M  827M  19% /boot
tmpfs                      tmpfs      79M     0   79M   0% /run/user/0
/dev/mapper/mysql--vg-data ext4      4.9G  8.0M  4.7G   1% /data/mysql/data
/dev/mapper/mysql--vg-log  xfs       7.0G   84M  7.0G   2% /data/mysql/logs

#xfs  不支持缩减
#ext4 缩减会造成数据破坏,建议缩减前先把数据备份

#缩减5个步骤
#第一步,取消挂载
[root@rocky8 data]# umount /data/mysql/data
umount: /data/mysql/data: target is busy.
[root@rocky8 data]# cd
[root@rocky8 ~]# umount /data/mysql/data
[root@rocky8 ~]# resize2fs /dev/mysql-vg/data 1G #缩减至1G
resize2fs 1.45.6 (20-Mar-2020)
Please run 'e2fsck -f /dev/mysql-vg/data' first.
#提示让你先检查磁盘

#第二步,检查文件系统完整性
[root@rocky8 ~]# e2fsck -f /dev/mysql-vg/data
e2fsck 1.45.6 (20-Mar-2020)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mysql-vg/data: 13/327680 files (0.0% non-contiguous), 39008/1310720 blocks

#第三步,缩减文件系统
[root@rocky8 ~]# resize2fs /dev/mysql-vg/data 1G
resize2fs 1.45.6 (20-Mar-2020)
Resizing the filesystem on /dev/mysql-vg/data to 262144 (4k) blocks.
The filesystem on /dev/mysql-vg/data is now 262144 (4k) blocks long.
#现在执行就可以

#第四步,缩减空间大小为1G
[root@rocky8 ~]# lvreduce -L 1G /dev/mysql-vg/data
  WARNING: Reducing active logical volume to 1.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mysql-vg/data? [y/n]: y #提示缩减空间大小,会造成数据丢失,问你是否继续,y
  Size of logical volume mysql-vg/data changed from 5.00 GiB (1280 extents) to 1.00 GiB (256 extents).
  Logical volume mysql-vg/data successfully resized.

#第五步,恢复挂载
[root@rocky8 ~]# mount /dev/mysql-vg/data /data/mysql/data
[root@rocky8 ~]# df -Th
Filesystem                 Type      Size  Used Avail Use% Mounted on
devtmpfs                   devtmpfs  374M     0  374M   0% /dev
tmpfs                      tmpfs     392M     0  392M   0% /dev/shm
tmpfs                      tmpfs     392M  5.6M  386M   2% /run
tmpfs                      tmpfs     392M     0  392M   0% /sys/fs/cgroup
/dev/sda2                  xfs       100G  2.6G   98G   3% /
/dev/sda3                  xfs        50G  390M   50G   1% /data
/dev/sda1                  xfs      1014M  188M  827M  19% /boot
tmpfs                      tmpfs      79M     0   79M   0% /run/user/0
/dev/mapper/mysql--vg-log  xfs       7.0G   84M  7.0G   2% /data/mysql/logs
/dev/mapper/mysql--vg-data ext4      944M  5.1M  878M   1% /data/mysql/data #空间已被缩减

[root@rocky8 ~]# lvs
  LV   VG       Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data mysql-vg -wi-ao---- 1.00g  #逻辑卷大小1G                                                  
  log  mysql-vg -wi-ao---- 6.99g                                                    

[root@rocky8 ~]# ls /data/mysql/data
a.txt  b.txt  lost+found
#数据还在

范例:

[root@rocky8 ~]# dd if=/dev/zero of=/data/f1.img bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 1.47592 s, 728 MB/s

[root@rocky8 ~]# vim /etc/fstab
/dev/mysql-vg/data                        /data/mysql/data        ext4    defaults        0 0
/dev/mysql-vg/log                         /data/mysql/logs        xfs     defaults        0 0

[root@rocky8 ~]# reboot

[root@rocky8 ~]# df
Filesystem                 1K-blocks    Used Available Use% Mounted on
devtmpfs                      382688       0    382688   0% /dev
tmpfs                         400580       0    400580   0% /dev/shm
tmpfs                         400580    5768    394812   2% /run
tmpfs                         400580       0    400580   0% /sys/fs/cgroup
/dev/sda2                  104806400 2682492 102123908   3% /
/dev/sda3                   52403200 1447056  50956144   3% /data
/dev/sda1                    1038336  192152    846184  19% /boot
/dev/mapper/mysql--vg-log    7321600   85636   7235964   2% /data/mysql/logs
/dev/mapper/mysql--vg-data    966552    5132    898900   1% /data/mysql/data
tmpfs                          80116       0     80116   0% /run/user/0

[root@rocky8 ~]# dd if=/dev/zero of=/data/mysql/logs/f1.img bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 0.731037 s, 1.5 GB/s
#逻辑卷的速度更快

[root@rocky8 ~]# fdisk /dev/sda

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
All primary partitions are in use.
Adding logical partition 6
First sector (320870400-419430399, default 320870400):  
Last sector, +sectors or +size{K,M,G,T,P} (320870400-419430399, default 419430399): +2G

Created a new partition 6 of type 'Linux' and of size 2 GiB.

Command (m for help): p 
Disk /dev/sda: 200 GiB, 214748364800 bytes, 419430400 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x43d7507d

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048   2099199   2097152    1G 83 Linux
/dev/sda2         2099200 211814399 209715200  100G 83 Linux
/dev/sda3       211814400 316671999 104857600   50G 83 Linux
/dev/sda4       316672000 419430399 102758400   49G  5 Extended
/dev/sda5       316674048 320868351   4194304    2G 82 Linux swap / Solaris
/dev/sda6       320870400 325064703   4194304    2G 83 Linux

Command (m for help): w
The partition table has been altered.
Syncing disks.

[root@rocky8 ~]# lsblk
NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                  8:0    0  200G  0 disk 
├─sda1               8:1    0    1G  0 part /boot
├─sda2               8:2    0  100G  0 part /
├─sda3               8:3    0   50G  0 part /data
├─sda4               8:4    0    1K  0 part 
├─sda5               8:5    0    2G  0 part [SWAP]
└─sda6               8:6    0    2G  0 part 
sdb                  8:16   0   10G  0 disk 
└─sdb1               8:17   0    4G  0 part 
  ├─mysql--vg-data 253:0    0    1G  0 lvm  /data/mysql/data
  └─mysql--vg-log  253:1    0    7G  0 lvm  /data/mysql/logs
sdc                  8:32   0    7G  0 disk 
└─mysql--vg-log    253:1    0    7G  0 lvm  /data/mysql/logs
sdd                  8:48   0    2G  0 disk 
sr0                 11:0    1  9.2G  0 rom 

[root@rocky8 ~]# pvcreate /dev/sda6
  Physical volume "/dev/sda6" successfully created.
[root@rocky8 ~]# lsblk
NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                  8:0    0  200G  0 disk 
├─sda1               8:1    0    1G  0 part /boot
├─sda2               8:2    0  100G  0 part /
├─sda3               8:3    0   50G  0 part /data
├─sda4               8:4    0    1K  0 part 
├─sda5               8:5    0    2G  0 part [SWAP]
└─sda6               8:6    0    2G  0 part 
sdb                  8:16   0   10G  0 disk 
└─sdb1               8:17   0    4G  0 part 
  ├─mysql--vg-data 253:0    0    1G  0 lvm  /data/mysql/data
  └─mysql--vg-log  253:1    0    7G  0 lvm  /data/mysql/logs
sdc                  8:32   0    7G  0 disk 
└─mysql--vg-log    253:1    0    7G  0 lvm  /data/mysql/logs
sdd                  8:48   0    2G  0 disk 
sr0                 11:0    1  9.2G  0 rom 

[root@rocky8 ~]# blkid
/dev/sda2: UUID="57f3a077-bb67-42c3-9e6c-bc8a0fed3776" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-02"
/dev/sda1: UUID="c72110fc-5dae-470c-9940-97ba978c7dc7" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-01"
/dev/sda3: UUID="02ae6744-8c28-4c86-b85b-e113c06a834f" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="43d7507d-03"
/dev/sda5: UUID="4f387202-0a81-4202-9e12-90d04af4d308" TYPE="swap" PARTUUID="43d7507d-05"
/dev/sda6: UUID="NhyOgW-5ylA-Pu9L-rdHc-xFPy-rr4r-Y74k1m" TYPE="LVM2_member" PARTUUID="43d7507d-06" #它现在就是逻辑卷
/dev/sdb1: UUID="PyYxIb-T7K7-X7XF-ZBQW-BWpC-xF3D-Ff2A4i" TYPE="LVM2_member" PARTUUID="744070d0-01"
/dev/sr0: BLOCK_SIZE="2048" UUID="2021-06-20-16-16-21-00" LABEL="Rocky-8-4-x86_64-dvd" TYPE="iso9660" PTUUID="4a14a4ee" PTTYPE="dos"
/dev/sdd: UUID="mncyA5-2Iap-DNAl-Jctz-767t-em77-hMwOYh" TYPE="LVM2_member"
/dev/mapper/mysql--vg-data: UUID="eb8fc2ac-50fa-432c-b628-1b6c8faeba5e" BLOCK_SIZE="4096" TYPE="ext4"
/dev/sdc: UUID="nFvzKO-UN0g-sbBA-UCbm-sy9c-wcIV-M2fd4Y" TYPE="LVM2_member"
/dev/mapper/mysql--vg-log: UUID="a3528ff7-e3e7-4658-a9ce-fe90a4bc5566" BLOCK_SIZE="512" TYPE="xfs"

[root@rocky8 ~]# pvs
  PV         VG       Fmt  Attr PSize  PFree 
  /dev/sda6           lvm2 ---   2.00g  2.00g #它现在就是物理卷
  /dev/sdb1  mysql-vg lvm2 a--  <4.00g <2.00g
  /dev/sdc   mysql-vg lvm2 a--  <7.00g  1.00g
  /dev/sdd   mysql-vg lvm2 a--  <2.00g <2.00g
#分区不改分区类型也可以直接创建物理卷

[root@rocky8 ~]# vgextend mysql-vg /dev/sda6
  Volume group "mysql-vg" successfully extended
[root@rocky8 ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree
  mysql-vg   4   2   0 wz--n- 14.98g 6.99g
[root@rocky8 ~]# lvs
  LV   VG       Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data mysql-vg -wi-ao---- 1.00g                                                    
  log  mysql-vg -wi-ao---- 6.99g 
  
[root@rocky8 ~]# pvdisplay /dev/sdc
  --- Physical volume ---
  PV Name               /dev/sdc
  VG Name               mysql-vg
  PV Size               7.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              1791
  Free PE               257
  Allocated PE          1534
  PV UUID               nFvzKO-UN0g-sbBA-UCbm-sy9c-wcIV-M2fd4Y

#要拆硬盘,有没有数据不重要,看PE是不是占用了

#拆走,要把它占的PE搬到卷组中其他成员中

[root@rocky8 ~]# vgdisplay 
  --- Volume group ---
  VG Name               mysql-vg
  System ID             
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  9
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               13.98 GiB
  PE Size               4.00 MiB
  Total PE              3580
  Alloc PE / Size       1790 / 6.99 GiB
  Free  PE / Size       1790 / 6.99 GiB
  VG UUID               cH1KAE-JVzd-WxVi-K9aU-CTd7-m7xx-10rBgF
#vg的剩余PE  大于被移走的硬盘的PE ,就可以移走

[root@rocky8 ~]# lvextend -r -L 3G /dev/mysql-vg/data
  Size of logical volume mysql-vg/data changed from 1.00 GiB (256 extents) to 3.00 GiB (768 extents).
  Logical volume mysql-vg/data successfully resized.
resize2fs 1.45.4 (23-Sep-2019)
Filesystem at /dev/mapper/mysql--vg-data is mounted on /data/mysql/data; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/mysql--vg-data is now 786432 (4k) blocks long.

[root@rocky8 ~]# vgdisplay 
  --- Volume group ---
  VG Name               mysql-vg
  System ID             
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  10
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               13.98 GiB
  PE Size               4.00 MiB
  Total PE              3580
  Alloc PE / Size       2302 / 8.99 GiB
  Free  PE / Size       1278 / 4.99 GiB
  VG UUID               cH1KAE-JVzd-WxVi-K9aU-CTd7-m7xx-10rBgF

[root@rocky8 ~]# pvdisplay /dev/sdc 
  --- Physical volume ---
  PV Name               /dev/sdc
  VG Name               mysql-vg
  PV Size               6.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              1535
  Free PE               256
  Allocated PE          1279
  PV UUID               LooCPF-Y0wH-mKA4-i6fE-dTw6-asI4-VWo2Xi
#vg 剩余空间1278 减去 被移走的磁盘的可用空间256  ,不大于可移走的磁盘的PE使用空间,现在是不能被移走的

[root@rocky8 ~]# pvmove /dev/sdc
  Insufficient free space: 1279 extents needed, but only 1022 available
  Unable to allocate mirror extents for mysql-vg/pvmove0.
  Failed to convert pvmove LV to mirrored.
#搬不了,提示卷组PE不足,卷组只有1022个可用PE
[root@rocky8 ~]# echo 1278-256 |bc
1022
#卷组可用PE就是1278减去被移走硬盘的可用PE,1022


[root@rocky8 ~]# umount /data/mysql/data/
[root@rocky8 ~]# fsck -f /dev/mysql-vg/data
fsck from util-linux 2.32.1
e2fsck 1.45.4 (23-Sep-2019)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/mysql--vg-data: 13/196608 files (0.0% non-contiguous), 30270/786432 blocks
[root@rocky8 ~]# resize2fs /dev/mysql-vg/data 1G
resize2fs 1.45.4 (23-Sep-2019)
Resizing the filesystem on /dev/mysql-vg/data to 262144 (4k) blocks.
The filesystem on /dev/mysql-vg/data is now 262144 (4k) blocks long.
       
[root@rocky8 ~]# lvreduce -L 1G /dev/mysql-vg/data
  WARNING: Reducing active logical volume to 1.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mysql-vg/data? [y/n]: y
  Size of logical volume mysql-vg/data changed from 3.00 GiB (768 extents) to 1.00 GiB (256 extents).
  Logical volume mysql-vg/data successfully resized.
[root@rocky8 ~]# mount /dev/mysql-vg/data /data/mysql/data

[root@rocky8 ~]# vgdisplay 
  --- Volume group ---
  VG Name               mysql-vg
  System ID             
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  11
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               13.98 GiB
  PE Size               4.00 MiB
  Total PE              3580
  Alloc PE / Size       1790 / 6.99 GiB
  Free  PE / Size       1790 / 6.99 GiB
  VG UUID               cH1KAE-JVzd-WxVi-K9aU-CTd7-m7xx-10rBgF
#把逻辑卷缩减后现在,卷组有空间了

[root@rocky8 ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               mysql-vg
  PV Size               4.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              1023
  Free PE               511
  Allocated PE          512
  PV UUID               yjOXxh-JkTl-sGeM-06FJ-7ppr-I1gz-7i47tO
   
  --- Physical volume ---
  PV Name               /dev/sdc
  VG Name               mysql-vg
  PV Size               6.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              1535
  Free PE               257
  Allocated PE          1278
  PV UUID               LooCPF-Y0wH-mKA4-i6fE-dTw6-asI4-VWo2Xi
   
  --- Physical volume ---
  PV Name               /dev/sdd
  VG Name               mysql-vg
  PV Size               2.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              511
  Free PE               511
  Allocated PE          0
  PV UUID               rnMiiT-i9R5-Lo3k-TGu8-EbEs-cZfz-a79L5k
   
  --- Physical volume ---
  PV Name               /dev/sda6
  VG Name               mysql-vg
  PV Size               2.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              511
  Free PE               511
  Allocated PE          0
  PV UUID               pnBOZs-QLdG-rP36-2UmP-9bTN-NXvD-t2AXsl
   
[root@rocky8 ~]# pvmove /dev/sdc		#移走/dev/sdc
  /dev/sdc: Moved: 1.41%
  /dev/sdc: Moved: 39.98%
  /dev/sdc: Moved: 79.97%
  /dev/sdc: Moved: 100.00%

[root@rocky8 ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               mysql-vg
  PV Size               4.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              1023
  Free PE               255
  Allocated PE          768
  PV UUID               yjOXxh-JkTl-sGeM-06FJ-7ppr-I1gz-7i47tO
   
  --- Physical volume ---
  PV Name               /dev/sdc
  VG Name               mysql-vg
  PV Size               6.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              1535
  Free PE               1535
  Allocated PE          0		#它的空间没有人用了
  PV UUID               LooCPF-Y0wH-mKA4-i6fE-dTw6-asI4-VWo2Xi
   
  --- Physical volume ---
  PV Name               /dev/sdd
  VG Name               mysql-vg
  PV Size               2.00 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              511
  Free PE               0
  Allocated PE          511
  PV UUID               rnMiiT-i9R5-Lo3k-TGu8-EbEs-cZfz-a79L5k
   
  --- Physical volume ---
  PV Name               /dev/sda6
  VG Name               mysql-vg
  PV Size               2.00 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              511
  Free PE               0
  Allocated PE          511
  PV UUID               pnBOZs-QLdG-rP36-2UmP-9bTN-NXvD-t2AXsl
#分到了其他的PE中

[root@rocky8 ~]# vgreduce mysql-vg /dev/sdc
  Removed "/dev/sdc" from volume group "mysql-vg"	#把/dev/sdc从卷组中移走
[root@rocky8 ~]# pvs
  PV         VG       Fmt  Attr PSize  PFree   
  /dev/sda6  mysql-vg lvm2 a--  <2.00g       0 
  /dev/sdb1  mysql-vg lvm2 a--  <4.00g 1020.00m
  /dev/sdc            lvm2 ---   6.00g    6.00g		#它现在就不在卷组中了
  /dev/sdd   mysql-vg lvm2 a--  <2.00g       0 

[root@rocky8 ~]# pvremove /dev/sdc		
  Labels on physical volume "/dev/sdc" successfully wiped.	#把/dev/sdc从物理卷中移走
[root@rocky8 ~]# pvs
  PV         VG       Fmt  Attr PSize  PFree   
  /dev/sda6  mysql-vg lvm2 a--  <2.00g       0 
  /dev/sdb1  mysql-vg lvm2 a--  <4.00g 1020.00m
  /dev/sdd   mysql-vg lvm2 a--  <2.00g       0		#物理卷中已经没有/dev/sdc了

4.3 逻辑卷快照

4.3.1 逻辑卷快照原理

35.第九章 磁盘存储和文件系统管理(五)
快照是特殊的逻辑卷,它是在生成快照时存在的逻辑卷的准确拷贝,对于需要备份或者复制的现有数据临时拷贝以及其它操作来说,快照是最合适的选择,快照只有在它们和原来的逻辑卷不同时才会消耗空间,建立快照的卷大小小于等于原始逻辑卷,也可以使用lvextend扩展快照

逻辑卷管理器快照

快照就是将当时的系统信息记录下来,就好像照相一般,若将来有任何数据改动了,则原始数据会被移动到快照区,没有改动的区域则由快照区和文件系统共享

逻辑卷快照工作原理

  • 在生成快照时会分配给它一定的空间,但只有在原来的逻辑卷或者快照有所改变才会使用这些空间
  • 当原来的逻辑卷中有所改变时,会将旧的数据复制到快照中
  • 快照中只含有原来的逻辑卷中更改的数据或者自生成快照后的快照中更改的数据

由于快照区与原本的LV共用很多PE的区块,因此快照与被快照的LV必须在同一个VG中.系统恢复的时候的文件数量不能高于快照区的实际容量

快照特点:

  • 备份速度快,瞬间完成
  • 应用场景是测试环境,不能完成代替备份
  • 快照后,逻辑卷的修改速度会一定有影响

4.3.1 实现逻辑卷快照

范例:

mkfs.xfs /dev/vg0/data
mount /dev/vg0/data/ /mnt/data

#为现有逻辑卷创建快照,注意ext4使用-p r 实现只读
lvcreate -l 64 -s -n data-snapshot -p r /dev/vg0/data

#挂载快照,xfs注意要使用-o ro实现只读,访止快照被修改
mkdir -p /mnt/snap
mount -o ro,nouuid /dev/vg0/data-snapshot /mnt/snap

#恢复快照
umount /dev/vg0/data-snapshot
umount /dev/vg0/data
lvconvert --merge /dev/vg0/data-snapshot

#删除快照
umount /mnt/snap
lvremove /dev/vg0/data-snapshot

范例:

#快照创建,只是映射了逻辑卷的数据,并没有立即备份,只是在硬盘上占用了空间而已,它里边的空间是空的,没有任何数据,只有逻辑卷中的数据有修改或删除时,它自动把原数据推送到快照中,不会备份后续修改或新建的数据;恢复时,会用快照中的数据和逻辑卷中去对比,做快照时有的就恢复,逻辑卷中后续修改或新建的,都直接删除,恢复做快照时的原始数据

[root@rocky8 ~]# df 
Filesystem                 1K-blocks    Used Available Use% Mounted on
devtmpfs                      905164       0    905164   0% /dev
tmpfs                         921932       0    921932   0% /dev/shm
tmpfs                         921932    8932    913000   1% /run
tmpfs                         921932       0    921932   0% /sys/fs/cgroup
/dev/sda2                  104806400 2157916 102648484   3% /
/dev/sda3                   52403200 1447040  50956160   3% /data
/dev/sda1                     999320  137644    792864  15% /boot
/dev/mapper/mysql--vg-log    6273024 1126644   5146380  18% /data/mysql/logs
tmpfs                         184384       0    184384   0% /run/user/0
/dev/mapper/mysql--vg-data    966552    5132    901700   1% /data/mysql/data
[root@rocky8 ~]# blkid
/dev/sda2: UUID="814ade0f-e75d-4995-a4c2-f9437964ab4d" TYPE="xfs" PARTUUID="da652ccc-02"
/dev/sr0: UUID="2020-06-08-22-08-25-00" LABEL="CentOS-8-2-2004-x86_64-dvd" TYPE="iso9660" PTUUID="545ce9a4" PTTYPE="dos"
/dev/sda1: UUID="4cc0c03b-c480-4f31-9f96-9d96a6695d4b" TYPE="ext4" PARTUUID="da652ccc-01"
/dev/sda3: UUID="97aa05bc-e17c-405b-b9e8-bbaa15532579" TYPE="xfs" PARTUUID="da652ccc-03"
/dev/sda5: UUID="3090def3-7b81-46c1-91a8-08861a8f296f" TYPE="swap" PARTUUID="da652ccc-05"
/dev/sda6: UUID="pnBOZs-QLdG-rP36-2UmP-9bTN-NXvD-t2AXsl" TYPE="LVM2_member" PARTUUID="da652ccc-06"
/dev/sdb1: UUID="yjOXxh-JkTl-sGeM-06FJ-7ppr-I1gz-7i47tO" TYPE="LVM2_member" PARTUUID="ca94f5fb-01"
/dev/sdd: UUID="rnMiiT-i9R5-Lo3k-TGu8-EbEs-cZfz-a79L5k" TYPE="LVM2_member"
/dev/mapper/mysql--vg-data: UUID="84c129c2-9262-47bb-ac03-78f196f0a47b" TYPE="ext4"
/dev/mapper/mysql--vg-log: UUID="37750bb1-d741-4d0e-ade3-911c43aef081" TYPE="xfs"

[root@rocky8 ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree   
  mysql-vg   3   2   0 wz--n- <7.99g 1020.00m	#要创建快照要保证卷组中有空间

[root@rocky8 ~]# cd /data/mysql/data
[root@rocky8 data]# ls
lost+found
[root@rocky8 data]# cp /etc/fstab f1.txt
[root@rocky8 data]# cp /etc/fstab f2.txt
[root@rocky8 data]# cp /etc/fstab f3.txt
[root@rocky8 data]# ls
f1.txt  f2.txt  f3.txt  lost+found

[root@rocky8 data]# cp /etc/fstab /data/mysql/logs/a.txt
[root@rocky8 data]# cp /etc/fstab /data/mysql/logs/b.txt
[root@rocky8 data]# cp /etc/fstab /data/mysql/logs/c.txt

[root@rocky8 data]# tree /data/mysql
/data/mysql
├── data
│   ├── f1.txt
│   ├── f2.txt
│   ├── f3.txt
│   └── lost+found
└── logs
    ├── a.txt
    ├── b.txt
    ├── c.txt
    └── f1.img

3 directories, 7 files

[root@rocky8 data]# lvs
  LV   VG       Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data mysql-vg -wi-ao---- 1.00g                                                    
  log  mysql-vg -wi-ao---- 5.99g 
#创建快照可以比原来的磁盘小,但是大了没有意义

[root@rocky8 data]# lvdisplay /dev/mysql-vg/data
  --- Logical volume ---
  LV Path                /dev/mysql-vg/data
  LV Name                data
  VG Name                mysql-vg
  LV UUID                f780zp-MWyj-Xwjk-qFW3-sdFa-znMe-zqiSwM
  LV Write Access        read/write
  LV Creation host, time centos8.neteagles.cn, 2020-12-06 15:19:29 +0800
  LV Status              available
  # open                 1
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

[root@rocky8 data]# lvcreate -L 200M -s -n data-snapshot -p r /dev/mysql-vg/data		#创建逻辑卷快照加-s,-p r  是对创建的快照加可读权限
  Logical volume "data-snapshot" created.
[root@centos8 data]# lvdisplay /dev/mysql-vg/data
  --- Logical volume ---
  LV Path                /dev/mysql-vg/data
  LV Name                data
  VG Name                mysql-vg
  LV UUID                f780zp-MWyj-Xwjk-qFW3-sdFa-znMe-zqiSwM
  LV Write Access        read/write
  LV Creation host, time centos8.neteagles.cn, 2020-12-06 15:19:29 +0800
  LV snapshot status     source of
                         data-snapshot [active]		#现在这里多了快照信息
  LV Status              available
  # open                 1
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

[root@rocky8 data]# lvs
  LV            VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data          mysql-vg owi-aos---   1.00g                                                    
  data-snapshot mysql-vg sri-a-s--- 200.00m      data   0.01                                   
  log           mysql-vg -wi-ao----   5.99g   

[root@rocky8 data]# lvdisplay /dev/mysql-vg/data-snapshot 
  --- Logical volume ---
  LV Path                /dev/mysql-vg/data-snapshot
  LV Name                data-snapshot
  VG Name                mysql-vg
  LV UUID                D4sqNA-Vm4e-Rj7s-d3xh-E2LA-K734-28XQnh
  LV Write Access        read only		#只读权限
  LV Creation host, time centos8.neteagles.cn, 2020-12-06 18:05:06 +0800
  LV snapshot status     active destination for data		#他是data逻辑卷的快照
  LV Status              available
  # open                 0
  LV Size                1.00 GiB		#逻辑卷1G
  Current LE             256
  COW-table size         200.00 MiB	#快照299M,COW  copy on write 写时复制
  COW-table LE           50
  Allocated to snapshot  0.01%
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:4

[root@rocky8 data]# lsblk
NAME                             MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                                8:0    0  200G  0 disk 
├─sda1                             8:1    0    1G  0 part /boot
├─sda2                             8:2    0  100G  0 part /
├─sda3                             8:3    0   50G  0 part /data
├─sda4                             8:4    0    1K  0 part 
├─sda5                             8:5    0    4G  0 part [SWAP]
└─sda6                             8:6    0    2G  0 part 
  └─mysql--vg-log                253:1    0    6G  0 lvm  /data/mysql/logs
sdb                                8:16   0   10G  0 disk 
└─sdb1                             8:17   0    4G  0 part 
  ├─mysql--vg-log                253:1    0    6G  0 lvm  /data/mysql/logs
  ├─mysql--vg-data-real          253:2    0    1G  0 lvm  
  │ ├─mysql--vg-data             253:0    0    1G  0 lvm  /data/mysql/data
  │ └─mysql--vg-data--snapshot   253:4    0    1G  1 lvm  
  └─mysql--vg-data--snapshot-cow 253:3    0  200M  0 lvm  #快照
    └─mysql--vg-data--snapshot   253:4    0    1G  1 lvm  
sdc                                8:32   0    6G  0 disk 
sdd                                8:48   0    2G  0 disk 
└─mysql--vg-log                  253:1    0    6G  0 lvm  /data/mysql/logs
sr0                               11:0    1  7.7G  0 rom

[root@rocky8 data]# mkdir /mnt/data-snapshot
[root@rocky8 data]# mount /dev/mysql-vg/data-snapshot /mnt/data-snapshot/	#挂载快照
mount: /mnt/data-snapshot: WARNING: device write-protected, mounted read-only.

[root@rocky8 data]# df -Th
Filesystem                           Type      Size  Used Avail Use% Mounted on
devtmpfs                             devtmpfs  884M     0  884M   0% /dev
tmpfs                                tmpfs     901M     0  901M   0% /dev/shm
tmpfs                                tmpfs     901M  8.8M  892M   1% /run
tmpfs                                tmpfs     901M     0  901M   0% /sys/fs/cgroup
/dev/sda2                            xfs       100G  2.1G   98G   3% /
/dev/sda3                            xfs        50G  1.4G   49G   3% /data
/dev/sda1                            ext4      976M  135M  775M  15% /boot
/dev/mapper/mysql--vg-log            xfs       6.0G  1.1G  5.0G  18% /data/mysql/logs
tmpfs                                tmpfs     181M     0  181M   0% /run/user/0
/dev/mapper/mysql--vg-data           ext4      944M  5.1M  881M   1% /data/mysql/data
/dev/mapper/mysql--vg-data--snapshot ext4      944M  5.1M  881M   1% /mnt/data-snapshot	#快照已经被挂载

[root@rocky8 data]# blkid
/dev/sda2: UUID="814ade0f-e75d-4995-a4c2-f9437964ab4d" TYPE="xfs" PARTUUID="da652ccc-02"
/dev/sr0: UUID="2020-06-08-22-08-25-00" LABEL="CentOS-8-2-2004-x86_64-dvd" TYPE="iso9660" PTUUID="545ce9a4" PTTYPE="dos"
/dev/sda1: UUID="4cc0c03b-c480-4f31-9f96-9d96a6695d4b" TYPE="ext4" PARTUUID="da652ccc-01"
/dev/sda3: UUID="97aa05bc-e17c-405b-b9e8-bbaa15532579" TYPE="xfs" PARTUUID="da652ccc-03"
/dev/sda5: UUID="3090def3-7b81-46c1-91a8-08861a8f296f" TYPE="swap" PARTUUID="da652ccc-05"
/dev/sda6: UUID="pnBOZs-QLdG-rP36-2UmP-9bTN-NXvD-t2AXsl" TYPE="LVM2_member" PARTUUID="da652ccc-06"
/dev/sdb1: UUID="yjOXxh-JkTl-sGeM-06FJ-7ppr-I1gz-7i47tO" TYPE="LVM2_member" PARTUUID="ca94f5fb-01"
/dev/sdd: UUID="rnMiiT-i9R5-Lo3k-TGu8-EbEs-cZfz-a79L5k" TYPE="LVM2_member"
/dev/mapper/mysql--vg-data: UUID="84c129c2-9262-47bb-ac03-78f196f0a47b" TYPE="ext4"
/dev/mapper/mysql--vg-log: UUID="37750bb1-d741-4d0e-ade3-911c43aef081" TYPE="xfs"
/dev/mapper/mysql--vg-data--snapshot: UUID="84c129c2-9262-47bb-ac03-78f196f0a47b" TYPE="ext4"
#/dev/mapper/mysql--vg-data--snapshot和/dev/mapper/mysql--vg-data的UUID是一样的

[root@rocky8 data]# ls /data/mysql/data
f1.txt  f2.txt  f3.txt  lost+found
[root@rocky8 data]# ls /mnt/data-snapshot/
f1.txt  f2.txt  f3.txt  lost+found
#快照里看到的数据并不在快照里,而在逻辑卷中,它只是映射了逻辑,现在里边是没有数据的

[root@rocky8 data]# ll -i /data/mysql/data
total 28
12 -rw-r--r-- 1 root root   899 Dec  6 17:59 f1.txt
13 -rw-r--r-- 1 root root   899 Dec  6 17:59 f2.txt
14 -rw-r--r-- 1 root root   899 Dec  6 17:59 f3.txt
11 drwx------ 2 root root 16384 Dec  6 15:28 lost+found
[root@rocky8 data]# ll -i /mnt/data-snapshot/
total 28
12 -rw-r--r-- 1 root root   899 Dec  6 17:59 f1.txt
13 -rw-r--r-- 1 root root   899 Dec  6 17:59 f2.txt
14 -rw-r--r-- 1 root root   899 Dec  6 17:59 f3.txt
11 drwx------ 2 root root 16384 Dec  6 15:28 lost+found
#他俩的节点编号是完全一样的
#我们现在看到快照里的数据,是映射到原始逻辑卷的数据,快照中现在是没有数据的

[root@rocky8 data]# rm -f /mnt/data-snapshot/f1.txt 
rm: cannot remove '/mnt/data-snapshot/f1.txt': Read-only file system
#删不了快照里的数据,因为在创建快照时,设置了只读权限

[root@rocky8 data]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=905164k,nr_inodes=226291,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda2 on / type xfs (rw,relatime,attr2,inode64,noquota)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=40,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=24220)
mqueue on /dev/mqueue type mqueue (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
/dev/sda3 on /data type xfs (rw,relatime,attr2,inode64,noquota)
/dev/sda1 on /boot type ext4 (rw,relatime)
/dev/mapper/mysql--vg-log on /data/mysql/logs type xfs (rw,relatime,attr2,inode64,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=184384k,mode=700)
/dev/mapper/mysql--vg-data on /data/mysql/data type ext4 (rw,relatime)
/dev/mapper/mysql--vg-data--snapshot on /mnt/data-snapshot type ext4 (ro,relatime)	#这里是ro权限

[root@rocky8 data]# vim /data/mysql/data/f1.txt 
UUID=814ade0f-e75d-4995-a4c2-f9437964ab4d /                       xfs     defaults        0 0               
UUID=4cc0c03b-c480-4f31-9f96-9d96a6695d4b /boot                   ext4    defaults        1 2
UUID=97aa05bc-e17c-405b-b9e8-bbaa15532579 /data                   xfs     defaults        0 0
UUID=3090def3-7b81-46c1-91a8-08861a8f296f swap                    swap    defaults        0 0

/dev/mysql-vg/data                        /data/mysql/data        ext4    defaults        0 0
/dev/mysql-vg/log                         /data/mysql/logs         xfs     defaults        0 0
:wq
#修改逻辑卷中的数据

[root@rocky8 data]# cat /mnt/data-snapshot/f1.txt 

#
# /etc/fstab
# Created by anaconda on Tue Dec  1 00:36:14 2020
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=814ade0f-e75d-4995-a4c2-f9437964ab4d /                       xfs     defaults        0 0
UUID=4cc0c03b-c480-4f31-9f96-9d96a6695d4b /boot                   ext4    defaults        1 2
UUID=97aa05bc-e17c-405b-b9e8-bbaa15532579 /data                   xfs     defaults        0 0
UUID=3090def3-7b81-46c1-91a8-08861a8f296f swap                    swap    defaults        0 0

/dev/mysql-vg/data                        /data/mysql/data        ext4    defaults        0 0
/dev/mysql-vg/log                         /data/mysql/logs         xfs     defaults        0 0
#快照中还是原始数据

[root@rocky8 data]# rm -f /data/mysql/data/f2.txt 
[root@rocky8 data]# ls /data/mysql/data/ 
f1.txt  f3.txt  lost+found	#删除逻辑卷中的数据
[root@rocky8 data]# ls /mnt/data-snapshot/
f1.txt  f2.txt  f3.txt  lost+found
#快照中还有原始数据

[root@rocky8 data]# cp /etc/issue /data/mysql/data/f4.txt
[root@rocky8 data]# ls
f1.txt  f3.txt  f4.txt  lost+found		#在逻辑卷中复制一个新文件
[root@rocky8 data]# ls /mnt/data-snapshot/
f1.txt  f2.txt  f3.txt  lost+found		#快照中是没有这个文件的

f1.txt  f2.txt  f3.txt  lost+found
[root@rocky8 data]# lvcreate -s -L 100M -n data-snapshot2 -p r /dev/mysql-vg/data
  Logical volume "data-snapshot2" created.	#继续对 /dev/mysql-vg/data逻辑卷做快照

[root@rocky8 data]# lvs
  LV             VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data           mysql-vg owi-aos---   1.00g                                                    
  data-snapshot  mysql-vg sri-aos--- 200.00m      data   0.14                                   
  data-snapshot2 mysql-vg sri-a-s--- 100.00m      data   0.01   	#这是新创建的快照                                
  log            mysql-vg -wi-ao----   5.99g  

[root@rocky8 data]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/mysql-vg/data
  LV Name                data
  VG Name                mysql-vg
  LV UUID                f780zp-MWyj-Xwjk-qFW3-sdFa-znMe-zqiSwM
  LV Write Access        read/write
  LV Creation host, time centos8.neteagles.cn, 2020-12-06 15:19:29 +0800
  LV snapshot status     source of
                         data-snapshot [active]
                         data-snapshot2 [active]		#可以看到data上有两个快照
  LV Status              available
  # open                 1
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/mysql-vg/log
  LV Name                log
  VG Name                mysql-vg
  LV UUID                almI3j-r1Ad-0Jfd-nIEV-hsIJ-ooaQ-CuIL0i
  LV Write Access        read/write
  LV Creation host, time centos8.neteagles.cn, 2020-12-06 15:26:19 +0800
  LV Status              available
  # open                 1
  LV Size                5.99 GiB
  Current LE             1534
  Segments               4
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/mysql-vg/data-snapshot
  LV Name                data-snapshot
  VG Name                mysql-vg
  LV UUID                D4sqNA-Vm4e-Rj7s-d3xh-E2LA-K734-28XQn
  LV Write Access        read only
  LV Creation host, time centos8.neteagles.cn, 2020-12-06 18:05:06 +0800
  LV snapshot status     active destination for data
  LV Status              available
  # open                 1
  LV Size                1.00 GiB
  Current LE             256
  COW-table size         200.00 MiB
  COW-table LE           50
  Allocated to snapshot  0.14%
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:4
   
  --- Logical volume ---
  LV Path                /dev/mysql-vg/data-snapshot2
  LV Name                data-snapshot2
  VG Name                mysql-vg
  LV UUID                iU9c2R-73Ws-GuOg-VIF9-Qav0-Dy0x-S0bFQc
  LV Write Access        read only
  LV Creation host, time centos8.neteagles.cn, 2020-12-06 18:31:03 +0800
  LV snapshot status     active destination for data
  LV Status              available
  # open                 0
  LV Size                1.00 GiB
  Current LE             256
  COW-table size         100.00 MiB
  COW-table LE           25
  Allocated to snapshot  0.01%
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:6

[root@rocky8 data]# mkdir /mnt/data-snapshot2
[root@rocky8 data]# mount /dev/mysql-vg/data-snapshot2 /mnt/data-snapshot2/
mount: /mnt/data-snapshot2: WARNING: device write-protected, mounted read-only.
[root@rocky8 data]# ls /mnt/data-snapshot2
f1.txt  f3.txt  f4.txt  lost+found
[root@rocky8 data]# ls /mnt/data-snapshot
f1.txt  f2.txt  f3.txt  lost+found
[root@rocky8 data]# ls /data/mysql/data
f1.txt  f3.txt  f4.txt  lost+found
#可以看到新创建的快照和当前逻辑卷中的数据一样

[root@rocky8 data]# rm -f /data/mysql/data/f4.txt 
[root@rocky8 data]# ls /data/mysql/data/ 
f1.txt  f3.txt  lost+found	#删除逻辑卷中的数据
[root@rocky8 data]# ls /mnt/data-snapshot2
f1.txt  f3.txt  f4.txt  lost+found		#新建的快照中的还在
[root@rocky8 data]# ls /mnt/data-snapshot
f1.txt  f2.txt  f3.txt  lost+found		#第一个快照里是没有的

[root@rocky8 data]# umount /data/mysql/data
umount: /data/mysql/data: target is busy.
[root@rocky8 data]# cd
[root@rocky8 ~]# umount /data/mysql/data
[root@rocky8 ~]# umount /mnt/data-snapshot
#还原快照先卸载挂载
[root@rocky8 ~]# lvs
  LV             VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data           mysql-vg owi-a-s---   1.00g                                                    
  data-snapshot  mysql-vg sri-a-s--- 200.00m      data   0.16                                   
  data-snapshot2 mysql-vg sri-aos--- 100.00m      data   0.08                                   
  log            mysql-vg -wi-ao----   5.99g   

[root@rocky8 ~]# lvconvert --merge /dev/mysql-vg/data-snapshot		#还原快照
  Merging of volume mysql-vg/data-snapshot started.
  mysql-vg/data: Merged: 100.00%
 
[root@rocky8 ~]# lvs
  LV             VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data           mysql-vg owi-a-s---   1.00g                                                    
  data-snapshot2 mysql-vg sri-aos--- 100.00m      data   0.33                                   
  log            mysql-vg -wi-ao----   5.99g     
#一旦合并快照,快照就没有了

[root@rocky8 ~]# mount -a
[root@rocky8 ~]# ls /data/mysql/data
f1.txt  f2.txt  f3.txt  lost+found
#数据还原成原始状态

[root@rocky8 ~]# umount /data/mysql/data
[root@rocky8 ~]# umount /mnt/data-snapshot2
[root@rocky8 ~]# lvconvert --merge /dev/mysql-vg/data-snapshot2
  Merging of volume mysql-vg/data-snapshot2 started.
  mysql-vg/data: Merged: 100.00%
[root@rocky8 ~]# mount -a
[root@rocky8 ~]# ls /data/mysql/data
f1.txt  f3.txt  f4.txt  lost+found
#还原成第二次创建快照时的状态

[root@rocky8 ~]# lvs
  LV   VG       Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data mysql-vg -wi-ao---- 1.00g                                                    
  log  mysql-vg -wi-ao---- 5.99g      #快照是一次性的,还原之后就没有了

#对逻辑卷做过快照,每次改数据时,会把原始文件推送到快照中,只有影响性能,一般只在测试环境中使用快照

[root@rocky8 ~]# lvs
  LV   VG       Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data mysql-vg -wi-ao---- 1.00g                                                    
  log  mysql-vg -wi-ao---- 5.99g                                                    
                                                  
[root@rocky8 ~]# df -Th
Filesystem                 Type      Size  Used Avail Use% Mounted on
devtmpfs                   devtmpfs  884M     0  884M   0% /dev
tmpfs                      tmpfs     901M     0  901M   0% /dev/shm
tmpfs                      tmpfs     901M  8.8M  892M   1% /run
tmpfs                      tmpfs     901M     0  901M   0% /sys/fs/cgroup
/dev/sda2                  xfs       100G  2.1G   98G   3% /
/dev/sda3                  xfs        50G  1.4G   49G   3% /data
/dev/sda1                  ext4      976M  135M  775M  15% /boot
/dev/mapper/mysql--vg-log  xfs       6.0G  1.1G  5.0G  18% /data/mysql/logs
tmpfs                      tmpfs     181M     0  181M   0% /run/user/0
/dev/mapper/mysql--vg-data ext4      944M  5.1M  881M   1% /data/mysql/data
[root@rocky8 ~]# lvcreate -s -n log-snapshot -L 200M /dev/mysql-vg/log 
  Logical volume "log-snapshot" created.
[root@rocky8 ~]# vs
  LV           VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data         mysql-vg -wi-ao----   1.00g                                                    
  log          mysql-vg owi-aos---   5.99g                                                    
  log-snapshot mysql-vg swi-a-s--- 200.00m      log    0.00  

[root@rocky8 ~]# ls /data/mysql/logs/
a.txt  b.txt  c.txt  f1.img
[root@rocky8 ~]# mkdir /mnt/log-snapshot
[root@rocky8 ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/mysql-vg/data
  LV Name                data
  VG Name                mysql-vg
  LV UUID                f780zp-MWyj-Xwjk-qFW3-sdFa-znMe-zqiSwM
  LV Write Access        read/write
  LV Creation host, time centos8.neteagles.cn, 2020-12-06 15:19:29 +0800
  LV Status              available
  # open                 1
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/mysql-vg/log
  LV Name                log
  VG Name                mysql-vg
  LV UUID                almI3j-r1Ad-0Jfd-nIEV-hsIJ-ooaQ-CuIL0i
  LV Write Access        read/write
  LV Creation host, time centos8.neteagles.cn, 2020-12-06 15:26:19 +0800
  LV snapshot status     source of
                         log-snapshot [active]
  LV Status              available
  # open                 1
  LV Size                5.99 GiB
  Current LE             1534
  Segments               4
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/mysql-vg/log-snapshot
  LV Name                log-snapshot
  VG Name                mysql-vg
  LV UUID                Gn0KFE-djm8-kuGJ-MXQn-vWhw-XEBw-3irWMS
  LV Write Access        read/write		#这个快照没有加-p r选项,现在是可读可写的
  LV Creation host, time centos8.neteagles.cn, 2020-12-06 18:53:24 +0800
  LV snapshot status     active destination for log
  LV Status              available
  # open                 0
  LV Size                5.99 GiB
  Current LE             1534
  COW-table size         200.00 MiB
  COW-table LE           50
  Allocated to snapshot  0.01%
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:4


[root@rocky8 ~]# mount /dev/mysql-vg/log-snapshot /mnt/log-snapshot/
mount: /mnt/log-snapshot: wrong fs type, bad option, bad superblock on /dev/mapper/mysql--vg-log--snapshot, missing codepage or helper program, or other error.	#挂载失败

[root@rocky8 ~]# blkid
/dev/sda2: UUID="814ade0f-e75d-4995-a4c2-f9437964ab4d" TYPE="xfs" PARTUUID="da652ccc-02"
/dev/sr0: UUID="2020-06-08-22-08-25-00" LABEL="CentOS-8-2-2004-x86_64-dvd" TYPE="iso9660" PTUUID="545ce9a4" PTTYPE="dos"
/dev/sda1: UUID="4cc0c03b-c480-4f31-9f96-9d96a6695d4b" TYPE="ext4" PARTUUID="da652ccc-01"
/dev/sda3: UUID="97aa05bc-e17c-405b-b9e8-bbaa15532579" TYPE="xfs" PARTUUID="da652ccc-03"
/dev/sda5: UUID="3090def3-7b81-46c1-91a8-08861a8f296f" TYPE="swap" PARTUUID="da652ccc-05"
/dev/sda6: UUID="pnBOZs-QLdG-rP36-2UmP-9bTN-NXvD-t2AXsl" TYPE="LVM2_member" PARTUUID="da652ccc-06"
/dev/sdb1: UUID="yjOXxh-JkTl-sGeM-06FJ-7ppr-I1gz-7i47tO" TYPE="LVM2_member" PARTUUID="ca94f5fb-01"
/dev/sdd: UUID="rnMiiT-i9R5-Lo3k-TGu8-EbEs-cZfz-a79L5k" TYPE="LVM2_member"
/dev/mapper/mysql--vg-data: UUID="84c129c2-9262-47bb-ac03-78f196f0a47b" TYPE="ext4"
/dev/mapper/mysql--vg-log: UUID="37750bb1-d741-4d0e-ade3-911c43aef081" TYPE="xfs"
/dev/mapper/mysql--vg-log--snapshot: UUID="37750bb1-d741-4d0e-ade3-911c43aef081" TYPE="xfs"
#/dev/mapper/mysql--vg-log--snapshot和/dev/mapper/mysql--vg-log 它们两个的UUID是相同的,xfs不允许两个相同的UUID进行挂载

[root@rocky8 ~]# mount -o nouuid /dev/mysql-vg/log-snapshot /mnt/log-snapshot/
#加上nouuid  就可以挂载

[root@rocky8 ~]# ls /mnt/log-snapshot/
a.txt  b.txt  c.txt  f1.img

[root@rocky8 ~]# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=905164k,nr_inodes=226291,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/sda2 on / type xfs (rw,relatime,attr2,inode64,noquota)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=40,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=24220)
mqueue on /dev/mqueue type mqueue (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
/dev/sda3 on /data type xfs (rw,relatime,attr2,inode64,noquota)
/dev/sda1 on /boot type ext4 (rw,relatime)
/dev/mapper/mysql--vg-log on /data/mysql/logs type xfs (rw,relatime,attr2,inode64,noquota)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=184384k,mode=700)
/dev/mapper/mysql--vg-data on /data/mysql/data type ext4 (rw,relatime)
/dev/mapper/mysql--vg-log--snapshot on /mnt/log-snapshot type xfs (rw,relatime,nouuid,attr2,inode64,noquota)		#挂载的时候没有加ro属性,现在是 rw可读可写

[root@rocky8 ~]# ls /mnt/log-snapshot/
a.txt  b.txt  c.txt  f1.img
[root@rocky8 ~]# rm -f /mnt/log-snapshot/f1.img 	
[root@rocky8 ~]# ls /mnt/log-snapshot/
a.txt  b.txt  c.txt
#是可以删除快照中的数据的
[root@rocky8 ~]# ls /data/mysql/logs/
a.	txt  b.txt  c.txt  f1.img
#原来逻辑卷中的数据还在

[root@rocky8 ~]# lvconvert --merge /dev/mysql-vg/log-snapshot
  Merging of volume mysql-vg/log-snapshot started.
  mysql-vg/log: Merged: 100.00%
[root@rocky8 ~]# mount -a
[root@rocky8 ~]# ls /data/mysql/logs/
a.txt  b.txt  c.txt
#因为刚才把快照破坏过了,现在只要破坏过的快照的数据了,快照挂载的时候要加只读属性,不加只读属性,可以破坏快照中的数据


#删除
[root@rocky8 ~]# umount /data/mysql/data	#先卸载
[root@rocky8 ~]# umount /data/mysql/logs/
[root@rocky8 ~]# lvremove /dev/mysql-vg/{data,log}
Do you really want to remove active logical volume mysql-vg/data? [y/n]: y
  Logical volume "data" successfully removed
Do you really want to remove active logical volume mysql-vg/log? [y/n]: y
  Logical volume "log" successfully removed
#删除逻辑卷

[root@rocky8 ~]# lvs
[root@rocky8 ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree 
  mysql-vg   3   0   0 wz--n- <7.99g <7.99g
[root@rocky8 ~]# vgremove mysql-vg
  Volume group "mysql-vg" successfully removed
[root@rocky8 ~]# vgs
#删除卷组

[root@rocky8 ~]# pvs
  PV         VG Fmt  Attr PSize PFree
  /dev/sda6     lvm2 ---  2.00g 2.00g
  /dev/sdb1     lvm2 ---  4.00g 4.00g
  /dev/sdd      lvm2 ---  2.00g 2.00g
[root@rocky8 ~]# pvremove /dev/sd{a6,b1,d}
  Labels on physical volume "/dev/sda6" successfully wiped.
  Labels on physical volume "/dev/sdb1" successfully wiped.
  Labels on physical volume "/dev/sdd" successfully wiped.
[root@rocky8 ~]# pvs
#删除物理卷

[root@rocky8 ~]# vim /etc/fstab
#删除刚在配置文件中添加的逻辑卷配置

[root@rocky8 ~]# fdisk /dev/sda

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Partition number (1-6, default 6): 6

Partition 6 has been deleted.

Command (m for help): p
Disk /dev/sda: 200 GiB, 214748364800 bytes, 419430400 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xda652ccc

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048   2099199   2097152    1G 83 Linux
/dev/sda2         2099200 211814399 209715200  100G 83 Linux
/dev/sda3       211814400 316671999 104857600   50G 83 Linux
/dev/sda4       316672000 419430399 102758400   49G  5 Extended
/dev/sda5       316674048 325062655   8388608    4G 82 Linux swap / Solaris

Command (m for help): w
The partition table has been altered.
Syncing disks.

[root@rocky8 ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): p
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xca94f5fb

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

4.4 练习

1、创建一个至少有两个PV组成的大小为20G的名为testvg的VG;要求PE大小为16MB, 而后在卷组中创建大小为5G的逻辑卷testlv;挂载至/users目录

2、 新建用户archlinux,要求其家目录为/users/archlinux,而后su切换至archlinux用户,复制/etc/pam.d目录至自己的家目录

3、扩展testlv至7G,要求archlinux用户的文件不能丢失

4、收缩testlv至3G,要求archlinux用户的文件不能丢失

5、对testlv创建快照,并尝试基于快照备份数据,验证快照的功能

上一篇:缩小规模,OpenAI文本生成图像新模型GLIDE用35亿参数媲美DALL-E


下一篇:518044-35-4,Mal-PEG3-t-butyl ester溶于大部分有机溶剂