Linux系统的逻辑卷管理

  • 一、查看系统磁盘及系统版本
  • 二、LVM逻辑卷相关术语解释
  • 三、创建LVM步骤
  • 1.创建两个磁盘分区
  • 2.创建物理卷PV
  • 3.创建卷组VG
  • 4.创建逻辑卷LVM
  • 5.格式化逻辑卷及挂载
  • ①格式化逻辑卷
  • ②编辑fstab
  • ③永久挂载
  • 四、查看逻辑卷状态
  • 五、删除逻辑卷
  • 六、扩展逻辑卷大小
  • 1.准备新分区
  • 2.扩展卷组
  • 3.扩展逻辑卷
  • 4.ext4文件系统扩容
  • 七、逻辑卷其他操作
  • 1.查看vg操作
  • 2.恢复VG操作
  • 3.查看逻辑卷状态


一、查看系统磁盘及系统版本

[root@node1 ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk 
├─sda1        8:1    0    5G  0 part 
└─sda2        8:2    0    5G  0 part 
sdb           8:16   0   20G  0 disk 
└─sdb1        8:17   0    6G  0 part 
sdc           8:32   0   20G  0 disk 
sdd           8:48   0   20G  0 disk 
sr0          11:0    1 1024M  0 rom  
nvme0n1     259:0    0   20G  0 disk 
├─nvme0n1p1 259:1    0  500M  0 part /boot
├─nvme0n1p2 259:2    0    2G  0 part [SWAP]
└─nvme0n1p3 259:3    0 17.5G  0 part /
[root@node1 ~]# cat /etc/redhat-release 
Red Hat Enterprise Linux release 8.0 (Ootpa)
[root@node1 ~]#

二、LVM逻辑卷相关术语解释

PP:物理分区 ,用来存储数据的块设备,可以是分区,磁盘,raid或者是SAN设备。
PV:物理卷,LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
VG:卷组,卷组是存储池,有一个或多个物理卷组成,一个PV只能分给一个VG。
LV:逻辑卷,LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷智商可以建立文件系统。
PE:physical extent ,每一个物理卷被划分为PE的基本单元,是LV的最小存储单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小可设置,默认为4MB。
LE:logical extent,逻辑卷也被划分为被称为LE的可被寻址的基本单位。在同一卷组中,LE的大小和PE是相同的,并且一一对应。设置特定的LV选项将会更改此映射,如:镜像会导致将每个LE映射到两个PE。

三、创建LVM步骤

1.创建两个磁盘分区

[root@node1 ~]# fdisk /dev/sdc

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 0xcbfb19f5.

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): 1
First sector (2048-41943039, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): +5G

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

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

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

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

2.创建物理卷PV

[root@node1 ~]# pvcreate /dev/sdc1 /dev/sdc2
  Physical volume "/dev/sdc1" successfully created.
  Physical volume "/dev/sdc2" successfully created.
[root@node1 ~]#

3.创建卷组VG

[root@node1 ~]# vgcreate vg01 /dev/sdc1 /dev/sdc2
  Volume group "vg01" successfully created
[root@node1 ~]#

4.创建逻辑卷LVM

[root@node1 ~]# lvcreate -L 5G -n lv01 vg01
  Logical volume "lv01" created.
备注:
-L: 指定大小
	-l:指定pe个数
	-n:指定逻辑卷的名字

5.格式化逻辑卷及挂载

①格式化逻辑卷

[root@node1 ~]# mkfs.xfs  /dev/vg01/lv01 
meta-data=/dev/vg01/lv01         isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=1310720, 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

②编辑fstab

[root@node1 ~]# blkid /dev/vg01/lv01
/dev/vg01/lv01: UUID="deff8218-3389-4245-a6bf-1716010fd6d4" TYPE="xfs"
[root@node1 ~]#
# vim /etc/fstab
# 
# /etc/fstab
# Created by anaconda on Fri Mar 19 22:21:55 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=b7190d80-906f-4b9d-9ab4-5a503ecaea2c /                       xfs     defaults        0 0
UUID=525a30a7-d484-4ed5-9f38-f827f54e29ff /boot                   xfs     defaults        0 0
UUID=e6cf8733-5eec-4942-9429-c3e9087b6ff0 swap                    swap    defaults        0 0
UUID="deff8218-3389-4245-a6bf-1716010fd6d4" /mnt/lv01 xfs   defaults        0 0

//192.168.200.150/share /media cifs defaults,credentials=/etc/samba/smbur.txt,multiuser,sec=ntlmssp 0 0
~                                                                                                                      
~                                                                                                                      
~                                                                                                                      
~                                                                                                                      
~                                                                                                                      
~                                                                                                                      
~                                                                                                                      
~                                                                                                                      
~                                                                                                                      
~                                                                                                                      
~                                                                                                                      
~

③永久挂载

[root@node1 ~]# mount -a
[root@node1 ~]# mount |grep lv01
/dev/mapper/vg01-lv01 on /mnt/lv01 type xfs (rw,relatime,attr2,inode64,noquota)
[root@node1 ~]#

四、查看逻辑卷状态

pvdisplay pvs
vgdisplay vgs
lvdisplaylvs
[root@node1 ~]# lvdisplay /dev/vg01/lv01 
  --- Logical volume ---
  LV Path                /dev/vg01/lv01
  LV Name                lv01
  VG Name                vg01
  LV UUID                vShvLp-OIP0-esvZ-IUXJ-EoiA-H85L-EVGf4L
  LV Write Access        read/write
  LV Creation host, time node1, 2021-09-20 18:10:16 +0800
  LV Status              available
  # open                 1
  LV Size                5.00 GiB
  Current LE             1280
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           252:0
   
[root@node1 ~]#

五、删除逻辑卷

lvremove
vgremove
pvremove

六、扩展逻辑卷大小

1.准备新分区

[root@node1 ~]# fdisk /dev/sdc

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
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3): 
First sector (23070720-41943039, default 23070720): 
Last sector, +sectors or +size{K,M,G,T,P} (23070720-41943039, default 41943039): +2G

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

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

[root@node1 ~]# fdisk -l /dev/sdc
Disk /dev/sdc: 20 GiB, 21474836480 bytes, 41943040 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: 0xcbfb19f5

Device     Boot    Start      End  Sectors Size Id Type
/dev/sdc1           2048 10487807 10485760   5G 83 Linux
/dev/sdc2       10487808 23070719 12582912   6G 83 Linux
/dev/sdc3       23070720 27265023  4194304   2G 83 Linux

2.扩展卷组

[root@node1 ~]# pvcreate /dev/sdc3
  Physical volume "/dev/sdc3" successfully created.
[root@node1 ~]# vgextend vg01 /dev/sdc3
  Volume group "vg01" successfully extended

3.扩展逻辑卷

[root@node1 ~]# lvextend -L +2G /dev/vg01/lv01 
  Size of logical volume vg01/lv01 changed from 5.00 GiB (1280 extents) to 7.00 GiB (1792 extents).
  Logical volume vg01/lv01 successfully resized.
[root@node1 ~]# xfs_growfs /mnt/lv01/
meta-data=/dev/mapper/vg01-lv01  isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=1310720, 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 1310720 to 1835008
[root@node1 ~]# lvdisplay /dev/vg01/lv01 
  --- Logical volume ---
  LV Path                /dev/vg01/lv01
  LV Name                lv01
  VG Name                vg01
  LV UUID                vShvLp-OIP0-esvZ-IUXJ-EoiA-H85L-EVGf4L
  LV Write Access        read/write
  LV Creation host, time node1, 2021-09-20 18:10:16 +0800
  LV Status              available
  # open                 1
  LV Size                7.00 GiB
  Current LE             1792
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           252:0

4.ext4文件系统扩容

vgextend vg0 /dev/sdc3
lvextend -L 2G /dev/vg01/lv01
resize2fs /dev/vg01/lv01  扩展ext文件系统

七、逻辑卷其他操作

1.查看vg操作

[root@node1 ~]# vgcfgrestore -l vg01 
   
  File:		/etc/lvm/archive/vg01_00000-678331601.vg
  Couldn't find device with uuid kc3fhN-gqjI-cc5Y-5hIu-Yk6k-ynHp-QUx9Pu.
  Couldn't find device with uuid ILteGS-iTup-NVd1-txdT-DcHJ-Sj0h-44wJ35.
  VG name:    	vg01
  Description:	Created *before* executing 'vgcreate vg01 /dev/sdc1 /dev/sdc2'
  Backup Time:	Mon Sep 20 18:05:46 2021

   
  File:		/etc/lvm/archive/vg01_00001-1663951558.vg
  VG name:    	vg01
  Description:	Created *before* executing 'lvcreate -L 5G -n lv01 vg01'
  Backup Time:	Mon Sep 20 18:10:16 2021

   
  File:		/etc/lvm/archive/vg01_00002-1665376704.vg
  VG name:    	vg01
  Description:	Created *before* executing 'vgextend vg01 /dev/sdc3'
  Backup Time:	Mon Sep 20 18:26:54 2021

   
  File:		/etc/lvm/archive/vg01_00003-569676061.vg
  Couldn't find device with uuid 0mx5Hv-2Pcb-ZuIS-sTz6-Tq2E-H5x4-ZHEo2a.
  VG name:    	vg01
  Description:	Created *before* executing 'lvextend -L +2G /dev/vg01/lv01'
  Backup Time:	Mon Sep 20 18:30:31 2021

   
  File:		/etc/lvm/backup/vg01
  VG name:    	vg01
  Description:	Created *after* executing 'lvextend -L +2G /dev/vg01/lv01'
  Backup Time:	Mon Sep 20 18:30:31 2021

[root@node1 ~]#

2.恢复VG操作

[root@node1 ~]# vgcfgrestore -l vg01
   
  File:		/etc/lvm/archive/vg01_00000-678331601.vg
  Couldn't find device with uuid kc3fhN-gqjI-cc5Y-5hIu-Yk6k-ynHp-QUx9Pu.
  Couldn't find device with uuid ILteGS-iTup-NVd1-txdT-DcHJ-Sj0h-44wJ35.
  VG name:    	vg01
  Description:	Created *before* executing 'vgcreate vg01 /dev/sdc1 /dev/sdc2'
  Backup Time:	Mon Sep 20 18:05:46 2021

   
  File:		/etc/lvm/archive/vg01_00001-1663951558.vg
  VG name:    	vg01
  Description:	Created *before* executing 'lvcreate -L 5G -n lv01 vg01'
  Backup Time:	Mon Sep 20 18:10:16 2021

   
  File:		/etc/lvm/archive/vg01_00002-1665376704.vg
  VG name:    	vg01
  Description:	Created *before* executing 'vgextend vg01 /dev/sdc3'
  Backup Time:	Mon Sep 20 18:26:54 2021

   
  File:		/etc/lvm/archive/vg01_00003-569676061.vg
  Couldn't find device with uuid 0mx5Hv-2Pcb-ZuIS-sTz6-Tq2E-H5x4-ZHEo2a.
  VG name:    	vg01
  Description:	Created *before* executing 'lvextend -L +2G /dev/vg01/lv01'
  Backup Time:	Mon Sep 20 18:30:31 2021

   
  File:		/etc/lvm/backup/vg01
  VG name:    	vg01
  Description:	Created *after* executing 'lvextend -L +2G /dev/vg01/lv01'
  Backup Time:	Mon Sep 20 18:30:31 2021

[root@node1 ~]# vgcfgrestore -f /etc/lvm/archive/vg01_00003-569676061.vg

3.查看逻辑卷状态

[root@node1 ~]# lvs
  LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv01 vg01 -wi-ao---- 7.00g                                                    
[root@node1 ~]# df -hT /dev/vg01/lv01 
Filesystem            Type  Size  Used Avail Use% Mounted on
/dev/mapper/vg01-lv01 xfs   7.0G   83M  7.0G   2% /mnt/lv01
[root@node1 ~]#