逻辑卷LVM知识(完整版操作)

时间:2020-09-03 16:53:08   收藏:0   阅读:45
一、逻辑卷(LVM)

注意:注意boot 分区不能为逻辑卷

逻辑卷底层可以是硬盘,也可以是分区

技术图片

整体思路

0、LVM创建步骤:物理硬盘 -->硬件raid -->pvcreate-->vgcreate --->lvcreate -->格式化文件系统-->临时挂载测试-->写进/etc/fstab-->mount -a 检查

[21:26:52 root@centos7 data]#lsblk 现有磁盘查看

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0 1000M  0 part /boot
└─sda2   8:2    0  9.8G  0 part /
sdb      8:16   0   25G  0 disk 
└─sdb1   8:17   0   20G  0 part 

[07:24:35 root@centos7 data]#df 现有挂载与空间,分区等情况查看

Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2       10229760 4962844   5266916  49% /
devtmpfs        16370032       0  16370032   0% /dev
tmpfs           16380928       0  16380928   0% /dev/shm
tmpfs           16380928   12244  16368684   1% /run
tmpfs           16380928       0  16380928   0% /sys/fs/cgroup
/dev/sda1        1020580  154716    865864  16% /boot
tmpfs            3276188       0   3276188   0% /run/user/0

1、磁盘新建分区,并改分区属性为lvm (8e)格式

1)新建分区

[07:24:40 root@centos7 data]#fdisk /dev/sdb

Welcome to fdisk (util-linux 2.23.2).

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 is deleted

Command (m for help): 
Command (m for help): `n`
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response `p`
Partition number (1-4, default 1): 
First sector (2048-52428799, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-52428799, default 52428799):` +1G`
Partition 1 of type Linux and of size 1 GiB is set

Command (m for help):` n`
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): 
Using default response `p`
Partition number (2-4, default 2): 
First sector (2099200-52428799, default 2099200): 
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-52428799, default 52428799): `+1G`
Partition 2 of type Linux and of size 1 GiB is set

Command (m for help): `n`
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): 
Using default response `p`
Partition number (3,4, default 3): 
First sector (4196352-52428799, default 4196352): 
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-52428799, default 52428799):` +1G`
Partition 3 of type Linux and of size 1 GiB is set

Command (m for help): `n`
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): 
Using default response e
Selected partition 4
First sector (6293504-52428799, default 6293504): 
Using default value 6293504
Last sector, +sectors or +size{K,M,G} (6293504-52428799, default 52428799): `+1G`
Partition 4 of type Extended and of size 1 GiB is set

Command (m for help):`t`
Partition number (1-4, default 4): `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 C:  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 eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext‘d (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list all codes): `8e`
Changed type of partition ‘Linux‘ to ‘Linux LVM‘

Command (m for help):` t`
Partition number (1-4, default 4): `2`
Hex code (type L to list all codes): `8e`
Changed type of partition ‘Linux‘ to ‘Linux LVM‘

Command (m for help): `t`
Partition number (1-4, default 4): `3`
Hex code (type L to list all codes): 8e
Changed type of partition ‘Linux‘ to ‘Linux LVM‘

Command (m for help): `t`
Partition number (1-4, default 4):` 4`
Hex code (type L to list all codes): `8e`

You cannot change a partition into an extended one or vice versa.
Delete it first.

Type of partition 4 is unchanged: Extended

Command (m for help): `w`
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

2)同步磁盘

[07:26:44 root@centos7 data]#partprobe

Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.

[07:26:51 root@centos7 data]#lsblk

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0 1000M  0 part /boot
└─sda2   8:2    0  9.8G  0 part /
sdb      8:16   0   25G  0 disk 
├─sdb1   8:17   0    1G  0 part 
├─sdb2   8:18   0    1G  0 part 
├─sdb3   8:19   0    1G  0 part 
└─sdb4   8:20   0    1K  0 part 
sr0     11:0    1   10G  0 rom  
sr1     11:1    1 1024M  0 rom 

2、创建pv

[07:26:53 root@centos7 data]#pvcreate /dev/sdb{1..4}

 Device /dev/sdb4 excluded by a filter.
WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: n
  Aborted wiping of xfs.
  1 existing signature left on the device.
  Physical volume "/dev/sdb2" successfully created.
  Physical volume "/dev/sdb3" successfully created.
[07:27:57 root@centos7 data]#`dd if=/dev/zero of=/dev/sdb1 bs=1M count=10`
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.0105745 s, 992 MB/s
[07:28:40 root@centos7 data]#`pvcreate /dev/sdb{1..4} `
  Device /dev/sdb4 excluded by a filter.
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdb2" successfully created.
  Physical volume "/dev/sdb3" successfully created.

3、pv查看

[07:28:43 root@centos7 data]#pvs

   PV         VG Fmt  Attr PSize PFree
  /dev/sdb1     lvm2 ---  1.00g 1.00g
  /dev/sdb2     lvm2 ---  1.00g 1.00g
  /dev/sdb3     lvm2 ---  1.00g 1.00g

[07:29:10 root@centos7 data]#pvdisplay

 "/dev/sdb2" is a new physical volume of "1.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2
  VG Name               
  PV Size               1.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               sp1RAv-KEJq-soOc-ryfd-2u2G-kdaR-P8EpbR
   --- NEW Physical volume ---
  PV Name               /dev/sdb3
  VG Name               
  PV Size               1.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               n2I2DR-IvJd-dBIV-z1vQ-QEgP-MB2p-KUBrRw
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               1.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               yZ16Cg-PGpj-3oAs-vsg1-qopN-4tKs-mVYOkh

4、创建vg

[07:29:49 root@centos7 data]#vgs
[07:29:54 root@centos7 data]#vgdisplay
[07:29:58 root@centos7 data]#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|lvm1 ]
    [ -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 ]
    [    --lockopt String ]
    [    --longhelp ]
    [    --profile String ]
    [    --version ]

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

[07:30:08 root@centos7 data]#vgcreate vg0 /dev/sdb{1..3}
Volume group "vg0" successfully created

5、 pv与vg查看

[07:31:09 root@centos7 data]#vgs

 VG  #PV #LV #SN Attr   VSize  VFree 
  vg0   3   0   0 wz--n- <2.99g <2.99g
[07:31:13 root@centos7 data]#`pvdisplay `
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               vg0
  PV Size               1.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              255
  Free PE               255
  Allocated PE          0
  PV UUID               yZ16Cg-PGpj-3oAs-vsg1-qopN-4tKs-mVYOkh

  --- Physical volume ---
  PV Name               /dev/sdb2
  VG Name               vg0
  PV Size               1.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              255
  Free PE               255
  Allocated PE          0
  PV UUID               sp1RAv-KEJq-soOc-ryfd-2u2G-kdaR-P8EpbR

  --- Physical volume ---
  PV Name               /dev/sdb3
  VG Name               vg0
  PV Size               1.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              255
  Free PE               255
  Allocated PE          0
  PV UUID               n2I2DR-IvJd-dBIV-z1vQ-QEgP-MB2p-KUBrRw

[07:31:18 root@centos7 data]#vgdisplay

  --- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        3
  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                3
  Act PV                3
  VG Size               <2.99 GiB
  PE Size               4.00 MiB
  Total PE              765
  Alloc PE / Size       0 / 0   
  Free  PE / Size       765 / <2.99 GiB
  VG UUID               UnE173-N5JP-Jswf-cmle-XFsH-dw81-dyfzHy

[07:31:39 root@centos7 data]#pvdisplay

 --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               vg0
  PV Size               1.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              255
  Free PE               255
  Allocated PE          0
  PV UUID               yZ16Cg-PGpj-3oAs-vsg1-qopN-4tKs-mVYOkh

  --- Physical volume ---
  PV Name               /dev/sdb2
  VG Name               vg0
  PV Size               1.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              255
  Free PE               255
  Allocated PE          0
  PV UUID               sp1RAv-KEJq-soOc-ryfd-2u2G-kdaR-P8EpbR

  --- Physical volume ---
  PV Name               /dev/sdb3
  VG Name               vg0
  PV Size               1.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              255
  Free PE               255
  Allocated PE          0
  PV UUID               n2I2DR-IvJd-dBIV-z1vQ-QEgP-MB2p-KUBrRw

[07:32:11 root@centos7 data]#vgs

  VG  #PV #LV #SN Attr   VSize  VFree 
  vg0   3   0   0 wz--n- <2.99g <2.99g

[07:32:12 root@centos7 data]#vgdisplay

--- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        3
  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                3
  Act PV                3
  VG Size               <2.99 GiB
  PE Size               4.00 MiB
  Total PE              765
  Alloc PE / Size       0 / 0   
  Free  PE / Size       765 / <2.99 GiB
  VG UUID               UnE173-N5JP-Jswf-cmle-XFsH-dw81-dyfzHy

6、创建lv

[07:33:11 root@centos7 data]#ls /dev/vg0
ls: cannot access /dev/vg0: No such file or directory
[07:33:14 root@centos7 data]#lvcreate vg0 /dev/sdb{1..3}
/dev/sdb1 /dev/sdb2 /dev/sdb3
[07:33:14 root@centos7 data]#lvs
[07:33:36 root@centos7 data]#lvdisplay
[07:33:39 root@centos7 data]#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] ]
    [ 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 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 cache LV, first creating a new origin LV, 
  then combining it with the existing cache pool named 
  by the --cachepool arg.
  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 ... ]

  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 ]
    [    --lockopt String ]
    [    --longhelp ]
    [    --profile String ]
    [    --version ]

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

7、lvm 大小指定

[07:33:43 root@centos7 data]#lvcreate -n mysql -L 2G vg0

Logical volume "mysql" created.

[07:35:53 root@centos7 data]#lvs

LV    VG  Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mysql vg0 -wi-a----- 2.00g

[07:35:56 root@centos7 data]#lvdisplay

  --- Logical volume ---
  LV Path                /dev/vg0/mysql
  LV Name                mysql
  VG Name                vg0
  LV UUID                vPLq9e-RWVJ-FJ2A-fra7-S3im-YjoV-2p5Fyc
  LV Write Access        read/write
  LV Creation host, time centos7, 2020-08-24 07:35:53 +0800
  LV Status              availableopen                 0

  LV Size                2.00 GiB
  Current LE             512
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto

  - currently set to     8192
    Block device           253:0

[07:36:01 root@centos7 data]#ll /dev/vg0/mysql
lrwxrwxrwx 1 root root 7 Aug 24 07:35 /dev/vg0/mysql -&gt; ../dm-0
[07:36:21 root@centos7 data]#ll /dev/mapper/vg0-mysql
lrwxrwxrwx 1 root root 7 Aug 24 07:35 /dev/mapper/vg0-mysql -&gt; ../dm-0
[07:36:39 root@centos7 data]#ll /dev/dm-0
brw-rw---- 1 root disk 253, 0 Aug 24 07:35 /dev/dm-0

[07:38:08 root@centos7 data]#pvdisplay

 --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               vg0
  PV Size               1.00 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              255
  Free PE               0
  Allocated PE          255
  PV UUID               yZ16Cg-PGpj-3oAs-vsg1-qopN-4tKs-mVYOkh

  --- Physical volume ---
  PV Name               /dev/sdb2
  VG Name               vg0
  PV Size               1.00 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              255
  Free PE               0
  Allocated PE          255
  PV UUID               sp1RAv-KEJq-soOc-ryfd-2u2G-kdaR-P8EpbR

  --- Physical volume ---
  PV Name               /dev/sdb3
  VG Name               vg0
  PV Size               1.00 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              255
  Free PE               253
  Allocated PE          2
  PV UUID               n2I2DR-IvJd-dBIV-z1vQ-QEgP-MB2p-KUBrRw

[07:38:12 root@centos7 data]#vgdisplay

-- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        3
  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                3
  Act PV                3
  VG Size               <2.99 GiB
  PE Size               4.00 MiB
  Total PE              765
  Alloc PE / Size       512 / 2.00 GiB
  Free  PE / Size       253 / 1012.00 MiB
  VG UUID               UnE173-N5JP-Jswf-cmle-XFsH-dw81-dyfzHy

[07:39:11 root@centos7 data]#blkid

/dev/sda2: UUID="296fe92a-a09a-4a43-b0bf-38302e32e228" TYPE="xfs" 
/dev/sdb1: UUID="yZ16Cg-PGpj-3oAs-vsg1-qopN-4tKs-mVYOkh" TYPE="LVM2_member" 
/dev/sda1: UUID="0745e236-6619-43df-929f-78cef14f3713" TYPE="xfs" 
/dev/sr0: UUID="2018-11-26-14-22-58-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/sdb2: UUID="sp1RAv-KEJq-soOc-ryfd-2u2G-kdaR-P8EpbR" TYPE="LVM2_member" 
/dev/sdb3: UUID="n2I2DR-IvJd-dBIV-z1vQ-QEgP-MB2p-KUBrRw" TYPE="LVM2_member" 

8、格式化逻辑卷

[07:39:17 root@centos7 data]#mkfs.xfs /dev/vg0/mysql

meta-data=/dev/vg0/mysql         isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=524288, 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

[07:40:11 root@centos7 data]#blkid

/dev/sda2: UUID="296fe92a-a09a-4a43-b0bf-38302e32e228" TYPE="xfs" 
/dev/sdb1: UUID="yZ16Cg-PGpj-3oAs-vsg1-qopN-4tKs-mVYOkh" TYPE="LVM2_member" 
/dev/sda1: UUID="0745e236-6619-43df-929f-78cef14f3713" TYPE="xfs" 
/dev/sr0: UUID="2018-11-26-14-22-58-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/sdb2: UUID="sp1RAv-KEJq-soOc-ryfd-2u2G-kdaR-P8EpbR" TYPE="LVM2_member" 
/dev/sdb3: UUID="n2I2DR-IvJd-dBIV-z1vQ-QEgP-MB2p-KUBrRw" TYPE="LVM2_member" 
/dev/mapper/vg0-mysql: UUID="5725bce9-cba7-4cb0-8c2e-61889d21ee86" TYPE="xfs" 

9、创建挂载点并挂载,且写进fstab

1)创建挂载点,并挂载

[07:40:14 root@centos7 data]#mkdir /mnt/mysql
[07:41:01 root@centos7 data]#mount /dev/mapper/vg0-mysql /mnt/mysql/
[07:41:23 root@centos7 data]#df

Filesystem            1K-blocks    Used Available Use% Mounted on
/dev/sda2              10229760 4962868   5266892  49% /
devtmpfs               16370032       0  16370032   0% /dev
tmpfs                  16380928       0  16380928   0% /dev/shm
tmpfs                  16380928   12272  16368656   1% /run
tmpfs                  16380928       0  16380928   0% /sys/fs/cgroup
/dev/sda1               1020580  154716    865864  16% /boot
tmpfs                   3276188       0   3276188   0% /run/user/0
/dev/mapper/vg0-mysql   2086912   32992   2053920   2% /mnt/mysql

[07:41:24 root@centos7 data]#df -h

Filesystem             Size  Used Avail Use% Mounted on
/dev/sda2              9.8G  4.8G  5.1G  49% /
devtmpfs                16G     0   16G   0% /dev
tmpfs                   16G     0   16G   0% /dev/shm
tmpfs                   16G   12M   16G   1% /run
tmpfs                   16G     0   16G   0% /sys/fs/cgroup
/dev/sda1              997M  152M  846M  16% /boot
tmpfs                  3.2G     0  3.2G   0% /run/user/0
/dev/mapper/vg0-mysql  2.0G   33M  2.0G   2% /mnt/mysql```

2)写进/etc/fstab,记得使用mount -a 检查一下

[07:41:28 root@centos7 data]#vim /etc/fstab

/etc/fstab

Created by anaconda on Fri Jul 24 07:21:37 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#
UUID=296fe92a-a09a-4a43-b0bf-38302e32e228 /                       xfs     defaults        0 0
UUID=0745e236-6619-43df-929f-78cef14f3713 /boot                   xfs     defaults        0 0
UUID=60393891-1289-45d3-83cf-1545e6ae2d3d  /home                  xfs     defaults        0 0 
UUID=5725bce9-cba7-4cb0-8c2e-61889d21ee86  /mnt/mysql            xfs     defaults        0  0

3)速度测试

[07:42:31 root@centos7 data]#dd if=/dev/zero of=/mnt/mysql/f1 bs=1M count=1024 conv=fdatasync

1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 4.91085 s, 219 MB/s 

[07:44:48 root@centos7 data]#dd if=/dev/zero of=/mnt/mysql/f12 bs=1M count=4024 conv=fdatasync

dd: error writing ‘/mnt/mysql/f12’: No space left on device
982+0 records in
981+0 records out
1028653056 bytes (1.0 GB) copied, 4.75583 s, 216 MB/s

[18:20:00 root@centos7 ~]#mount /dev/mapper/vg0-mysql /mnt/mysql/
[19:16:16 root@centos7 ~]#df

Filesystem            1K-blocks    Used Available Use% Mounted on
/dev/sda2              10229760 4963452   5266308  49% /
devtmpfs               16370032       0  16370032   0% /dev
tmpfs                  16380928       0  16380928   0% /dev/shm
tmpfs                  16380928   12272  16368656   1% /run
tmpfs                  16380928       0  16380928   0% /sys/fs/cgroup
/dev/sda1               1020580  154716    865864  16% /boot
tmpfs                   3276188       0   3276188   0% /run/user/0
/dev/mapper/vg0-mysql   2086912   32992   2053920   2% /mnt/mysql

[19:16:17 root@centos7 ~]#vgdisplay

--- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <2.99 GiB
  PE Size               4.00 MiB
  Total PE              765
  Alloc PE / Size       512 / 2.00 GiB
  Free  PE / Size       253 / 1012.00 MiB
  VG UUID               UnE173-N5JP-Jswf-cmle-XFsH-dw81-dyfzHy

二、逻辑卷扩展

1、逻辑卷扩展步骤:先加硬盘或者分区--->再扩展pv--->再扩展vg--->再扩展lv-----再扩展逻辑卷-->再扩展逻辑卷文件系统

虚拟机添加硬盘,并在线扫描
echo ‘- - -‘ >/sys/class/scsi_host/host0/scan 
echo ‘- - -‘ >/sys/class/scsi_host/host2/scan
lsblk 查看磁盘信息
创建pv
pvcreate /dev/sdc
pvs  pv查看
vgdisplay  vg查看
扩展vg
vgextend vg0 /dev/sdc
vgdisplay  vg查看
pvs pv查看
lvs  lv查看
创建新的lv
lvcreate -n binlog -L 10G vg0
lvdisplay  lv查看
格式化新创建的lv
mkfs.ext4 /dev/vg0/binlog 
创建挂载点
mkdir /mnt/binlog
挂载
mount /dev/mapper/vg0-binlog /mnt/binlog/
df -hT  查看使用情况

vgdisplay  查看vg是否还剩

lvextend -l +1000 /dev/vg0/binlog 再扩展逻辑卷大小
xfs_growfs /mnt/mysql/    `xfs文件系统扩展,注意这个命令是挂载点`

resize2fs  /dev/vg0/binlog    `ext4文件系统扩展,注意这个命令是设备名`

或者
 lvextend -r -l +500 /dev/vg0/mysql `(针对于xfs与ext4 都适用)`

[19:17:20 root@centos7 ~]#lvextend -l +100%free /dev/vg0/mysql

 Size of logical volume vg0/mysql changed from 2.00 GiB (512 extents) to <2.99 GiB (765 extents).
  Logical volume vg0/mysql successfully resized.

[19:18:29 root@centos7 ~]#vgdisplay

 --- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <2.99 GiB
  PE Size               4.00 MiB
  Total PE              765
  Alloc PE / Size       765 / <2.99 GiB
  Free  PE / Size       0 / 0   
  VG UUID               UnE173-N5JP-Jswf-cmle-XFsH-dw81-dyfzHy

[19:18:45 root@centos7 ~]#lvdisplay

 --- Logical volume ---
  LV Path                /dev/vg0/mysql
  LV Name                mysql
  VG Name                vg0
  LV UUID                vPLq9e-RWVJ-FJ2A-fra7-S3im-YjoV-2p5Fyc
  LV Write Access        read/write
  LV Creation host, time centos7, 2020-08-24 07:35:53 +0800
  LV Status              availableopen                 1

  LV Size                <2.99 GiB
  Current LE             765
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto

  - currently set to     8192
    Block device           253:0

[19:19:37 root@centos7 ~]#df -hT

 Filesystem            Type      Size  Used Avail Use% Mounted on
/dev/sda2             xfs       9.8G  4.8G  5.1G  49% /
devtmpfs              devtmpfs   16G     0   16G   0% /dev
tmpfs                 tmpfs      16G     0   16G   0% /dev/shm
tmpfs                 tmpfs      16G   12M   16G   1% /run
tmpfs                 tmpfs      16G     0   16G   0% /sys/fs/cgroup
/dev/sda1             xfs       997M  152M  846M  16% /boot
tmpfs                 tmpfs     3.2G     0  3.2G   0% /run/user/0
/dev/mapper/vg0-mysql xfs       2.0G   33M  2.0G   2% /mnt/mysql

[19:21:23 root@centos7 ~]#xfs_growfs /mnt/mysql/ 注意:不同的文件系统,扩展命令不一样

meta-data=/dev/mapper/vg0-mysql  isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=524288, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               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 524288 to 783360

[19:22:39 root@centos7 ~]#df -Th

Filesystem            Type      Size  Used Avail Use% Mounted on
/dev/sda2             xfs       9.8G  4.8G  5.1G  49% /
devtmpfs              devtmpfs   16G     0   16G   0% /dev
tmpfs                 tmpfs      16G     0   16G   0% /dev/shm
tmpfs                 tmpfs      16G   12M   16G   1% /run
tmpfs                 tmpfs      16G     0   16G   0% /sys/fs/cgroup
/dev/sda1             xfs       997M  152M  846M  16% /boot
tmpfs                 tmpfs     3.2G     0  3.2G   0% /run/user/0
/dev/mapper/vg0-mysql xfs       3.0G   33M  3.0G   2% /mnt/mysql

[19:22:44 root@centos7 ~]#vgdisplay

 --- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <2.99 GiB
  PE Size               4.00 MiB
  Total PE              765
  Alloc PE / Size       765 / <2.99 GiB
  Free  PE / Size       0 / 0    `全部用完`
  VG UUID               UnE173-N5JP-Jswf-cmle-XFsH-dw81-dyfzHy

2、点击虚拟机添加一块新硬盘22G,并在线重新识别。(新加硬盘在线识别)

[19:26:51 root@centos7 ~]#echo ‘- - -‘ &gt;/sys/class/scsi_host/host0/scan
[19:29:04 root@centos7 ~]#echo ‘- - -‘ &gt;/sys/class/scsi_host/host2/scan
[19:29:08 root@centos7 ~]#lsblk

NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda             8:0    0  200G  0 disk 
├─sda1          8:1    0 1000M  0 part /boot
└─sda2          8:2    0  9.8G  0 part /
sdb             8:16   0   25G  0 disk 
├─sdb1          8:17   0    1G  0 part 
│ └─vg0-mysql 253:0    0    3G  0 lvm  /mnt/mysql
├─sdb2          8:18   0    1G  0 part 
│ └─vg0-mysql 253:0    0    3G  0 lvm  /mnt/mysql
├─sdb3          8:19   0    1G  0 part 
│ └─vg0-mysql 253:0    0    3G  0 lvm  /mnt/mysql
└─sdb4          8:20   0    1K  0 part 
sdc             8:32   0   22G  0 disk 
sr0            11:0    1 1024M  0 rom  
sr1            11:1    1 1024M  0 rom 

3、创建pv,新加硬盘sdc

[19:29:11 root@centos7 ~]#pvcreate /dev/sdc

  Physical volume "/dev/sdc" successfully created.

[19:30:53 root@centos7 ~]#pvs

PV         VG  Fmt  Attr PSize    PFree 
  /dev/sdb1  vg0 lvm2 a--  1020.00m     0 
  /dev/sdb2  vg0 lvm2 a--  1020.00m     0 
  /dev/sdb3  vg0 lvm2 a--  1020.00m     0 
  /dev/sdc       lvm2 ---    22.00g 22.00g

[19:30:57 root@centos7 ~]#vgdisplay

 --- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <2.99 GiB
  PE Size               4.00 MiB
  Total PE              765
  Alloc PE / Size       765 / <2.99 GiB
  Free  PE / Size       0 / 0   
  VG UUID               UnE173-N5JP-Jswf-cmle-XFsH-dw81-dyfzHy

4、vg扩展 将sdc所在的vg

[19:31:04 root@centos7 ~]#vgextend vg0 /dev/sdc

 Volume group "vg0" successfully extended

[19:31:22 root@centos7 ~]#vgdisplay

 --- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               24.98 GiB
  PE Size               4.00 MiB
  Total PE              6396
  Alloc PE / Size       765 / <2.99 GiB
  Free  PE / Size       5631 / <22.00 GiB
  VG UUID               UnE173-N5JP-Jswf-cmle-XFsH-dw81-dyfzHy

[19:31:26 root@centos7 ~]#pvs

  PV         VG  Fmt  Attr PSize    PFree  
  /dev/sdb1  vg0 lvm2 a--  1020.00m      0 
  /dev/sdb2  vg0 lvm2 a--  1020.00m      0 
  /dev/sdb3  vg0 lvm2 a--  1020.00m      0 
  /dev/sdc   vg0 lvm2 a--   <22.00g <22.00g

[19:34:09 root@centos7 ~]#lvs

  LV    VG  Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mysql vg0 -wi-ao---- <2.99g     

5、lv扩展

[19:34:19 root@centos7 ~]#lvcreate -n binlog -L 10G vg0

  Logical volume "binlog" created.

[19:34:40 root@centos7 ~]#lvdisplay

  --- Logical volume ---
  LV Path                /dev/vg0/mysql
  LV Name                mysql
  VG Name                vg0
  LV UUID                vPLq9e-RWVJ-FJ2A-fra7-S3im-YjoV-2p5Fyc
  LV Write Access        read/write
  LV Creation host, time centos7, 2020-08-24 07:35:53 +0800
  LV Status              available

  # open                 1

  LV Size                <2.99 GiB
  Current LE             765
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto

  - currently set to     8192
    Block device           253:0

  --- Logical volume ---
  LV Path                /dev/vg0/binlog
  LV Name                binlog
  VG Name                vg0
  LV UUID                tI4gWz-Mbx5-EmQ1-lFAt-X0m4-ZMv7-72Pe0V
  LV Write Access        read/write
  LV Creation host, time centos7, 2020-08-24 19:34:40 +0800
  LV Status              available

  # open                 0

  LV Size                10.00 GiB
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto

  - currently set to     256
    Block device           253:1

6、创建lvm的文件系统

[19:34:46 root@centos7 ~]#mkfs.ext4 /dev/vg0/binlog

mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

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

7、创建挂载点与挂载测试

[19:35:18 root@centos7 ~]#mkdir /mnt/binlog
[19:35:27 root@centos7 ~]#mount /dev/mapper/vg0-binlog /mnt/binlog/
[19:35:49 root@centos7 ~]#df

 Filesystem             Type      Size  Used Avail Use% Mounted on
/dev/sda2              xfs       9.8G  4.8G  5.1G  49% /
devtmpfs               devtmpfs   16G     0   16G   0% /dev
tmpfs                  tmpfs      16G     0   16G   0% /dev/shm
tmpfs                  tmpfs      16G   12M   16G   1% /run
tmpfs                  tmpfs      16G     0   16G   0% /sys/fs/cgroup
/dev/sda1              xfs       997M  152M  846M  16% /boot
tmpfs                  tmpfs     3.2G     0  3.2G   0% /run/user/0
/dev/mapper/vg0-mysql  xfs       3.0G   33M  3.0G   2% /mnt/mysql
/dev/mapper/vg0-binlog ext4      9.8G   37M  9.2G   1% /mnt/binlog

[19:37:26 root@centos7 ~]#vgdisplay

 --- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  5
  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               24.98 GiB
  PE Size               4.00 MiB
  Total PE              6396
  Alloc PE / Size       3325 / <12.99 GiB
  Free  PE / Size       3071 / <12.00 GiB
  VG UUID               UnE173-N5JP-Jswf-cmle-XFsH-dw81-dyfzHy

8、扩容逻辑卷

[19:37:43 root@centos7 ~]#lvextend -l +1000 /dev/vg0/binlog

Size of logical volume vg0/binlog changed from 10.00 GiB (2560 extents) to <13.91 GiB (3560 extents).
  Logical volume vg0/binlog successfully resized.
[19:38:36 root@centos7 ~]#lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg0/mysql
  LV Name                mysql
  VG Name                vg0
  LV UUID                vPLq9e-RWVJ-FJ2A-fra7-S3im-YjoV-2p5Fyc
  LV Write Access        read/write
  LV Creation host, time centos7, 2020-08-24 07:35:53 +0800
  LV Status              available

  # open                 1

  LV Size                <2.99 GiB
  Current LE             765
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto

  - currently set to     8192
    Block device           253:0

  --- Logical volume ---
  LV Path                /dev/vg0/binlog
  LV Name                binlog
  VG Name                vg0
  LV UUID                tI4gWz-Mbx5-EmQ1-lFAt-X0m4-ZMv7-72Pe0V
  LV Write Access        read/write
  LV Creation host, time centos7, 2020-08-24 19:34:40 +0800
  LV Status              available

  # open                 1

  LV Size                <13.91 GiB
  Current LE             3560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto

  - currently set to     256
    Block device           253:1

[19:38:50 root@centos7 ~]#df -Th

Filesystem             Type      Size  Used Avail Use% Mounted on
/dev/sda2              xfs       9.8G  4.8G  5.1G  49% /
devtmpfs               devtmpfs   16G     0   16G   0% /dev
tmpfs                  tmpfs      16G     0   16G   0% /dev/shm
tmpfs                  tmpfs      16G   12M   16G   1% /run
tmpfs                  tmpfs      16G     0   16G   0% /sys/fs/cgroup
/dev/sda1              xfs       997M  152M  846M  16% /boot
tmpfs                  tmpfs     3.2G     0  3.2G   0% /run/user/0
/dev/mapper/vg0-mysql  xfs       3.0G   33M  3.0G   2% /mnt/mysql
/dev/mapper/vg0-binlog ext4      9.8G   37M  9.2G   1% /mnt/binlog

9、扩容逻辑卷文件系统

[19:38:52 root@centos7 ~]#resize2fs /dev/vg0/binlog

resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/binlog is mounted on /mnt/binlog; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/vg0/binlog is now 3645440 blocks long.

[19:39:38 root@centos7 ~]#df -Th

Filesystem             Type      Size  Used Avail Use% Mounted on
/dev/sda2              xfs       9.8G  4.8G  5.1G  49% /
devtmpfs               devtmpfs   16G     0   16G   0% /dev
tmpfs                  tmpfs      16G     0   16G   0% /dev/shm
tmpfs                  tmpfs      16G   12M   16G   1% /run
tmpfs                  tmpfs      16G     0   16G   0% /sys/fs/cgroup
/dev/sda1              xfs       997M  152M  846M  16% /boot
tmpfs                  tmpfs     3.2G     0  3.2G   0% /run/user/0
/dev/mapper/vg0-mysql  xfs       3.0G   33M  3.0G   2% /mnt/mysql
/dev/mapper/vg0-binlog ext4       14G   41M   13G   1% /mnt/binlog

[19:39:44 root@centos7 ~]#vgdisplay

  --- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  6
  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               24.98 GiB
  PE Size               4.00 MiB
  Total PE              6396
  Alloc PE / Size       4325 / 16.89 GiB
  Free  PE / Size       2071 / <8.09 GiB
  VG UUID               UnE173-N5JP-Jswf-cmle-XFsH-dw81-dyfzHy

10、只要vg 还有空间,也可以用下面的命令进行扩容逻辑卷

[19:40:39 root@centos7 ~]#lvextend -r -l +500 /dev/vg0/mysql

  Size of logical volume vg0/mysql changed from <2.99 GiB (765 extents) to 4.94 GiB (1265 extents).
  Logical volume vg0/mysql successfully resized.
meta-data=/dev/mapper/vg0-mysql  isize=512    agcount=6, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=783360, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               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 783360 to 1295360

[19:41:08 root@centos7 ~]#df -Th

Filesystem             Type      Size  Used Avail Use% Mounted on
/dev/sda2              xfs       9.8G  4.8G  5.1G  49% /
devtmpfs               devtmpfs   16G     0   16G   0% /dev
tmpfs                  tmpfs      16G     0   16G   0% /dev/shm
tmpfs                  tmpfs      16G   12M   16G   1% /run
tmpfs                  tmpfs      16G     0   16G   0% /sys/fs/cgroup
/dev/sda1              xfs       997M  152M  846M  16% /boot
tmpfs                  tmpfs     3.2G     0  3.2G   0% /run/user/0
/dev/mapper/vg0-mysql  xfs       5.0G   33M  4.9G   1% /mnt/mysql
/dev/mapper/vg0-binlog ext4       14G   41M   13G   1% /mnt/binlog 

11、逻辑卷缩减,工作中不常用,有风险,数据易丢 ; 特别是缩减和缩减到,一定要慎重。(注意:只能缩减ext4 文件系统 ;xfs不能缩减)

逻辑卷缩减步骤:卸载逻辑卷挂载点-->逻辑卷文件系统检查--->缩减文件系统--->缩减逻辑卷大小-->重新挂载--->df 查看缩减后的大小

0)缩减逻辑卷命令如下:

注意:只能缩减ext4 文件系统 ;xfs不能缩减
 umount /mnt/binlog/
 e2fsck -f /dev/vg0/binlog
 resize2fs /dev/vg0/binlog 5G
 lvs
 lvreduce -L 5G /dev/vg0/binlog 
 mount /dev/vg0/binlog /mnt/binlog/
  df -Th

19:41:43 root@centos7 ~]#resize2fs /dev/vg0/binlog 5G 此时报错,没有取消挂载

resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/binlog is mounted on /mnt/binlog; on-line resizing required
resize2fs: On-line shrinking not supported

1)卸载逻辑卷挂载点

[19:57:48 root@centos7 ~]#umount /mnt/binlog/

[19:57:58 root@centos7 ~]#resize2fs /dev/vg0/binlog 5G

resize2fs 1.42.9 (28-Dec-2013)
Please run ‘e2fsck -f /dev/vg0/binlog‘ first.

2)执行文件系统检查

[19:57:59 root@centos7 ~]#e2fsck -f /dev/vg0/binlog

e2fsck 1.42.9 (28-Dec-2013)
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/vg0/binlog: 11/1040384 files (0.0% non-contiguous), 109349/4157440 blocks
[19:58:09 root@centos7 ~]#resize2fs /dev/vg0/binlog 5G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/binlog to 1310720 (4k) blocks.
The filesystem on /dev/vg0/binlog is now 1310720 blocks long.

3)缩减逻辑卷文件系统

[19:58:09 root@centos7 ~]#resize2fs /dev/vg0/binlog 5G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/binlog to 1310720 (4k) blocks.
The filesystem on /dev/vg0/binlog is now 1310720 blocks long.

[19:58:14 root@centos7 ~]#lvs

LV     VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  binlog vg0 -wi-a----- <15.86g                                                    
  mysql  vg0 -wi-ao----   4.94g 

4)缩减逻辑卷

[19:58:37 root@centos7 ~]#lvreduce -L 5G /dev/vg0/binlog

Filesystem             Type      Size  Used Avail Use% Mounted on
/dev/sda2              xfs       9.8G  4.8G  5.1G  49% /
devtmpfs               devtmpfs   16G     0   16G   0% /dev
tmpfs                  tmpfs      16G     0   16G   0% /dev/shm
tmpfs                  tmpfs      16G   12M   16G   1% /run
tmpfs                  tmpfs      16G     0   16G   0% /sys/fs/cgroup
/dev/sda1              xfs       997M  152M  846M  16% /boot
tmpfs                  tmpfs     3.2G     0  3.2G   0% /run/user/0
/dev/mapper/vg0-mysql  xfs       5.0G   33M  4.9G   1% /mnt/mysql
/dev/mapper/vg0-binlog ext4      4.8G   33M  4.6G   1% /mnt/binlog

三、跨主机迁移逻辑卷

技术图片

分区无法取消时,可以使用这个进行检查
mount

fuser -v /mnt/sdb1

df

lsblk

partx -d --nr 1 /dev/sdb

lsblk 恢复

[20:51:53 root@centos7 ~]#vgrename vg0 vg1
Volume group "vg0" successfully renamed to "vg1"

[20:53:13 root@centos7 ~]#vgdisplay

--- Volume group ---
  VG Name               vg1
  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               0
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               24.98 GiB
  PE Size               4.00 MiB
  Total PE              6396
  Alloc PE / Size       2545 / 9.94 GiB
  Free  PE / Size       3851 / 15.04 GiB
  VG UUID               UnE173-N5JP-Jswf-cmle-XFsH-dw81-dyfzHy

1、禁用逻辑卷

[20:53:17 root@centos7 ~]#vgchange -an vg1
0 logical volume(s) in volume group "vg1" now active
[20:53:39 root@centos7 ~]#lvdisplay

 --- Logical volume ---
  LV Path                /dev/vg1/mysql
  LV Name                mysql
  VG Name                vg1
  LV UUID                vPLq9e-RWVJ-FJ2A-fra7-S3im-YjoV-2p5Fyc
  LV Write Access        read/write
  LV Creation host, time centos7, 2020-08-24 07:35:53 +0800
  LV Status              NOT available
  LV Size                4.94 GiB
  Current LE             1265
  Segments               4
  Allocation             inherit
  Read ahead sectors     auto

  --- Logical volume ---
  LV Path                /dev/vg1/binlog
  LV Name                binlog
  VG Name                vg1
  LV UUID                tI4gWz-Mbx5-EmQ1-lFAt-X0m4-ZMv7-72Pe0V
  LV Write Access        read/write
  LV Creation host, time centos7, 2020-08-24 19:34:40 +0800
  LV Status              NOT available
  LV Size                5.00 GiB
  Current LE             1280
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto

2、逻辑卷导出状态

[20:53:49 root@centos7 ~]#vgexport vg1
Volume group "vg1" successfully exported
[20:54:37 root@centos7 ~]#lvdisplay
Volume group vg1 is exported

[20:54:42 root@centos7 ~]#vgdisplay

 --- Volume group ---
  VG Name               vg1
  System ID             
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  11
  VG Access             read/write
  VG Status             exported/resizable
  MAX LV                0
  Cur LV                2
  Open LV               0
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               24.98 GiB
  PE Size               4.00 MiB
  Total PE              6396
  Alloc PE / Size       2545 / 9.94 GiB
  Free  PE / Size       3851 / 15.04 GiB
  VG UUID               UnE173-N5JP-Jswf-cmle-XFsH-dw81-dyfzHy

3、此时可以拆硬盘,拿到别的机器了;执行硬盘添加在线扫描

4、逻辑卷导入状态

[20:58:50 root@centos7 ~]#vgimport vg1
Volume group "vg1" successfully imported
[20:59:01 root@centos7 ~]#vgdisplay

--- Volume group ---
  VG Name               vg1
  System ID             
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  12
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               0
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               24.98 GiB
  PE Size               4.00 MiB
  Total PE              6396
  Alloc PE / Size       2545 / 9.94 GiB
  Free  PE / Size       3851 / 15.04 GiB
  VG UUID               UnE173-N5JP-Jswf-cmle-XFsH-dw81-dyfzHy

5、激活逻辑卷

[20:59:07 root@centos7 ~]#vgchange -ay vg1
2 logical volume(s) in volume group "vg1" now active
[20:59:19 root@centos7 ~]#lvdisplay

 --- Logical volume ---
  LV Path                /dev/vg1/mysql
  LV Name                mysql
  VG Name                vg1
  LV UUID                vPLq9e-RWVJ-FJ2A-fra7-S3im-YjoV-2p5Fyc
  LV Write Access        read/write
  LV Creation host, time centos7, 2020-08-24 07:35:53 +0800
  LV Status              available

  # open                 0

  LV Size                4.94 GiB
  Current LE             1265
  Segments               4
  Allocation             inherit
  Read ahead sectors     auto

  - currently set to     8192
    Block device           253:0

  --- Logical volume ---
  LV Path                /dev/vg1/binlog
  LV Name                binlog
  VG Name                vg1
  LV UUID                tI4gWz-Mbx5-EmQ1-lFAt-X0m4-ZMv7-72Pe0V
  LV Write Access        read/write
  LV Creation host, time centos7, 2020-08-24 19:34:40 +0800
  LV Status              available

  # open                 0

  LV Size                5.00 GiB
  Current LE             1280
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto

  - currently set to     256
    Block device           253:1

6、创建挂载点,并挂载测试

[20:59:25 root@centos7 ~]#mkdir /mnt/guazai
[21:01:08 root@centos7 ~]#mount /dev/vg1/mysql /mnt/guazai/
[21:01:24 root@centos7 ~]#df

Filesystem            1K-blocks    Used Available Use% Mounted on
/dev/sda2              10229760 4963656   5266104  49% /
devtmpfs               16370032       0  16370032   0% /dev
tmpfs                  16380928       0  16380928   0% /dev/shm
tmpfs                  16380928   12248  16368680   1% /run
tmpfs                  16380928       0  16380928   0% /sys/fs/cgroup
/dev/sda1               1020580  154716    865864  16% /boot
tmpfs                   3276188       0   3276188   0% /run/user/0
/dev/mapper/vg1-mysql   5171200   33280   5137920   1% /mnt/guaza

四、硬盘拔出

假如一个逻辑卷中某一块盘,需要迁移,且盘全部分出去的情况下,只能搬到同卷组的vg 其他盘里;现有逻辑卷空间不受影响。搬到同一个卷组的其他成员里。

1、拆除pv对应的硬盘

pvmove    /dev/sdd

vgreduce   vg0   /dev/sdd

pvremove    /dev/sdd
`可以拔掉盘了`

2、删除逻辑卷

umount /mnt/mysql

lvremove  /dev/vg1/mysql  一定要确保数据不用了,才删除

vgremove vg1

pvremove /dev/sde

`可以拔掉盘了`

五、逻辑卷快照

0、使用LVM快照步骤:

1) 为现有逻辑卷创建快照
  lvcreate -l 64 -s -n data-snapshot -p r /dev/vg0/data

2) 挂载快照
  mkdir -p /mnt/snap
  mount -o ro /dev/vg0/data-snapshot /mnt/snap

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

4) 删除快照
  umount /mnt/databackup
  lvremove /dev/vg0/databackup

1、创建只读快照

lvcreate -n mysql_snaphost -s -L 1G -p r /dev/vg1/mysql -s 快照 -p r 只读 xfs不可以;ext4 可以

[21:59:59 root@centos7 ~]#

mount /dev/vg1/mysql_snaphost /mnt/

mount: /dev/mapper/vg1-mysql_snaphost is write-protected, mounting read-only
mount: wrong fs type, bad option, bad superblock on /dev/mapper/vg1-mysql_snaphost,
       missing codepage or helper program, or other error
       In some cases useful info is found in syslog - try
   dmesg | tail or so.

2、只读快照,无法挂载 xfs不可以;ext4 可以 。

[22:00:45 root@centos7 ~]#mount -o nouuid /dev/vg1/mysql_snaphost /mnt/

mount: /dev/mapper/vg1-mysql_snaphost is write-protected, mounting read-only
mount: cannot mount /dev/mapper/vg1-mysql_snaphost read-only

3、再次创建快照

[22:01:07 root@centos7 ~]#lvcreate -n mysql_snaphost22222 -s -L 1G /dev/vg1/mysql
Logical volume "mysql_snaphost22222" created.

[22:03:10 root@centos7 ~]#lvs


  LV                  VG  Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  binlog              vg1 -wi-a----- 5.00g                                                    
  mysql               vg1 owi-aos--- 4.94g                                                    
  mysql_snaphost      vg1 sri-a-s--- 1.00g      mysql  0.01                                   
  mysql_snaphost22222 vg1 swi-a-s--- 1.00g      mysql  0.00   

4、删除刚才创建的只读快照

[22:03:18 root@centos7 ~]#lvremove /dev/vg1/mysql_snaphost

Do you really want to remove active logical volume vg1/mysql_snaphost? [y/n]: y
  Logical volume "mysql_snaphost" successfully removed

[22:03:38 root@centos7 ~]#lvs

LV                  VG  Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  binlog              vg1 -wi-a----- 5.00g                                                    
  mysql               vg1 owi-aos--- 4.94g                                                    
  mysql_snaphost22222 vg1 swi-a-s--- 1.00g      mysql  0.01

5、创建挂载点

[22:05:26 root@centos7 ~]#mkdir -p /mnt/snaphost
[22:06:19 root@centos7 ~]#mkdir /mnt/mysql
[22:06:38 root@centos7 ~]#mount /dev/vg1/mysql /mnt/mysql
[22:06:40 root@centos7 ~]#mount /dev/vg1/mysql_snaphost22222 /mnt

注意xfs系统的uuid 不能一样,否则不能挂载; ext4 可以
mount: wrong fs type, bad option, bad superblock on /dev/mapper/vg1-mysql_snaphost22222,
       missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
   dmesg | tail or so.

6、不检查uuid 挂载快照

[22:06:52 root@centos7 ~]#mount -o nouuid /dev/vg1/mysql_snaphost22222 /mnt
[22:07:01 root@centos7 ~]#df

Filesystem                          1K-blocks    Used Available Use% Mounted on
/dev/sda2                            10229760 4963672   5266088  49% /
devtmpfs                             16370032       0  16370032   0% /dev
tmpfs                                16380928       0  16380928   0% /dev/shm
tmpfs                                16380928   12272  16368656   1% /run
tmpfs                                16380928       0  16380928   0% /sys/fs/cgroup
/dev/sda1                             1020580  154716    865864  16% /boot
tmpfs                                 3276188       0   3276188   0% /run/user/0
/dev/mapper/vg1-mysql_snaphost22222   5171200   33280   5137920   1% /mnt

7、快照返回原来的状态

1)取消快照挂载和逻辑卷挂载

umount /mnt/snap

umount /mnt/mysql

2)还原原来做快照状态 ,新建文件将丢失,逻辑卷快将照丢失,只有一次生命。合并逻辑卷快照与快照 。 (xfs系列)

lvconvert --merge /dev/vg1/mysql_snaphost22222

3)重新挂载与查看

mount /dev/vg1/binlog /mnt/binlog

df -Th

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!