可以参考这篇文章来理解lvm的一些概念
https://cloud.tencent.com/developer/article/1937939
把若干个物理磁盘组成物理卷(PV),再把物理卷组成卷组(VG),卷组支持动态扩容,然后再把卷组分成逻辑卷(LV),只要卷组有空间,逻辑卷就可以扩容,逻辑卷的最小存储单位是PE默认大小4MB
建立LVM步骤
1.把物理硬盘变成一个分区,也可以是整块硬盘
2.把物理分区建立成物理卷
3.把多个物理卷组合成卷组,卷组可调整大小
4.把卷组划分成逻辑卷
4.1、分区并且更改文件系统
1.分区
[root@localhost ~]# 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): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-52428799, default 2048): +2G
Last sector, +sectors or +size{K,M,G} (4194304-52428799, default 52428799):
Using default value 52428799
Partition 1 of type Linux and of size 23 GiB is set
Command (m for help): p
Disk /dev/sdb: 26.8 GB, 26843545600 bytes, 52428800 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
Disk label type: dos
Disk identifier: 0xefc86b95
Device Boot Start End Blocks Id System
/dev/sdb1 4194304 52428799 24117248 83 Linux
Command (m for help): d
Selected partition 1
Partition 1 is deleted
Command (m for help): p
Disk /dev/sdb: 26.8 GB, 26843545600 bytes, 52428800 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
Disk label type: dos
Disk identifier: 0xefc86b95
Device Boot Start End Blocks Id System
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-52428799, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-52428799, default 52428799): +2G
Partition 1 of type Linux and of size 2 GiB is set
Command (m for help): p
Disk /dev/sdb: 26.8 GB, 26843545600 bytes, 52428800 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
Disk label type: dos
Disk identifier: 0xefc86b95
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (4196352-52428799, default 4196352):
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-52428799, default 52428799): +2G
Partition 2 of type Linux and of size 2 GiB is set
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (8390656-52428799, default 8390656):
Using default value 8390656
Last sector, +sectors or +size{K,M,G} (8390656-52428799, default 52428799): +2G
Partition 3 of type Linux and of size 2 GiB is set
Command (m for help): p
Disk /dev/sdb: 26.8 GB, 26843545600 bytes, 52428800 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
Disk label type: dos
Disk identifier: 0xefc86b95
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 8390655 2097152 83 Linux
/dev/sdb3 8390656 12584959 2097152 83 Linux
2.更改文件系统:
fdisk /dev/sdb
Command (m for help): t
Partition number (1-3, default 3): 1
Hex code (type L to list all codes): l
8e Linux LVM
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-3, default 3): 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-3, default 3): 3
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: 26.8 GB, 26843545600 bytes, 52428800 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
Disk label type: dos
Disk identifier: 0xefc86b95
Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 8e Linux LVM
/dev/sdb2 4196352 8390655 2097152 8e Linux LVM
/dev/sdb3 8390656 12584959 2097152 8e Linux LVM
4.2、分区建立成物理卷
pvcreate 创建物理卷
pvscan 查看物理卷
pvdisplay 查看物理卷详细信息
pvremove 删除物理卷
创建物理卷
[root@localhost ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]# pvcreate /dev/sdb2
Physical volume "/dev/sdb2" successfully created.
[root@localhost ~]# pvcreate /dev/sdb3
Physical volume "/dev/sdb3" successfully created.
查看物理卷
[root@localhost ~]# pvscan
PV /dev/sda2 VG centos lvm2 [<19.00 GiB / 0 free]
PV /dev/sdb3 lvm2 [2.00 GiB]
PV /dev/sdb2 lvm2 [2.00 GiB]
PV /dev/sdb1 lvm2 [2.00 GiB]
Total: 4 [<25.00 GiB] / in use: 1 [<19.00 GiB] / in no VG: 3 [6.00 GiB]
删除物理卷
[root@localhost ~]# pvremove /dev/sdb3
Labels on physical volume "/dev/sdb3" successfully wiped.
4.3、创建卷组
vgcreate 新建卷组
vgcreate [命令选项] 卷组名 物理卷名
-s 指定PE大小
vgscan 查看卷组
vgdisplay 查看卷组
vgextend 扩容卷组
vgreduce 缩减卷组
vgremove 删除卷组
创建xvg卷组
[root@localhost ~]# vgcreate xvg /dev/sdb1 /dev/sdb2
Volume group "xvg" successfully created
查看卷组
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name xvg 卷组名
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 3.99 GiB 卷组大小
PE Size 4.00 MiB PE大小
Total PE 1022
Alloc PE / Size 0 / 0 已用PE数量/大小
Free PE / Size 1022 / 3.99 GiB 空闲PE数量/大小
VG UUID aepGgb-ijOl-xjAb-mf9X-3TIb-sSFb-NC1ce1
扩容卷组
[root@localhost ~]# vgextend xvg /dev/sdb3
Volume group "xvg" successfully extended
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name xvg
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 2
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 <5.99 GiB
PE Size 4.00 MiB
Total PE 1533
Alloc PE / Size 0 / 0
Free PE / Size 1533 / <5.99 GiB
VG UUID aepGgb-ijOl-xjAb-mf9X-3TIb-sSFb-NC1ce1
缩减
[root@localhost ~]# vgreduce xvg /dev/sdb3
Removed "/dev/sdb3" from volume group "xvg"
删除卷组
[root@localhost ~]# vgremove xvg
Volume group "xvg" successfully removed
4.4、生成逻辑卷
lvcreate 生成逻辑卷
lvcreate [命令选项] [-n 逻辑卷名] 卷组名
-L 容量,指定逻辑卷大小
-n 逻辑卷名
lvdisplay 查看逻辑卷
lvscan 查看逻辑卷
生成逻辑卷
[root@localhost ~]# lvcreate -L 3G -n xlv xvg
Logical volume "xlv" created.
查看逻辑卷
[root@localhost ~]# lvscan
ACTIVE '/dev/xvg/xlv' [3.00 GiB] inherit
4.5、挂载使用
格式化
[root@localhost ~]# mkfs.xfs /dev/xvg/xlv
挂载
[root@localhost ~]# mkdir /xyj
[root@localhost ~]# mount /dev/xvg/xlv /xyj/
[root@localhost ~]# df -h
/dev/mapper/xvg-xlv 3.0G 33M 3.0G 2% /lvm
扩容
5G的意思就是你要把这个分区容量要增加到的大小,比如要把这个分区增加到20G,那么就写20G
[root@localhost ~]# lvresize -L 5G /dev/xvg/xlv
Size of logical volume xvg/xlv changed from 3.00 GiB (768 extents) to 5.00 GiB (1280 extents).
Logical volume xvg/xlv successfully resized.
[root@localhost ~]# df -h
/dev/mapper/xvg-xlv 3.0G 33M 3.0G 2% /lvm
[root@localhost ~]# xfs_growfs /dev/xvg/xlv
[root@localhost ~]# df -h
/dev/mapper/xvg-xlv 5.0G 33M 5.0G 1% /lvm
如果想永久生效,就需要修改/etc/fstab文件,加入下面的内容,
/dev/xvg/xlv /xyj xfs defaults 0 0