LVM概述
全称: Logical Volume Manager,是基于内核的一种逻辑卷管理器,允许用户动态调整文件系统(任意文件目录)大小,大多数linux发行版中的默认分区机制。
LVM逻辑卷管理器的几个重要概念:
- 物理卷(PV):最底层概念,是LVM逻辑存储块,物理卷与磁盘分区是逻辑的对应关系,多个物理卷可以合并和拆分,从而实现扩容和缩减
- 卷组(VG):是LVM逻辑概念上的磁盘设备,通过单个或多个物理卷组合成卷组,大小取决于物理卷的容量以及个数
- 物理长度(PE):将物理卷组合成为卷组后,所划分的最小存储单位,LVM默认PE小为4MB
- 逻辑卷(LV):LVM逻辑意义上的分区,可以指定从卷组中提取多少容量来创建逻辑卷
LVM常用命令
LVM逻辑卷管理器的几个重要命令:
普通分区转换成物理卷:pvcreate
物理卷组合为卷组的命令:vgcreate
卷组中提取容量创建逻辑卷的命令:lvcreate
创建后查看创建信息的命令:pvdisplay、vgdisplay、lvdisplay
实验操作:磁盘扩容
测试场景:系统根目录空间不足,需要进行动态调整大小,
测试目的:将第二块磁盘sdb扩容至根目录
测试环境:
- 磁盘分区情况
- 测试环境的LVM分区示意图:
物理磁盘:8G
PV:/dev/sda2
VG:centos
扩容操作步骤:
- 对sdb新磁盘进行分区
[root@yunmx ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0x9faf1a38 创建新的 DOS 磁盘标签。
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-41943039,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):
将使用默认值 41943039
分区 1 已设置为 Linux 类型,大小设为 20 GiB
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
- 检查sdb磁盘分区情况:
- 对sdb1进行物理卷PV创建:
[root@yunmx ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
- 将创建的物理卷加入centos卷组中:
[root@yunmx ~]# vgextend centos /dev/sdb1
Volume group "centos" successfully extended
- 查看centos卷组信息:
[root@yunmx ~]# vgdisplay centos
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 2
Act PV 2
VG Size 26.99 GiB
PE Size 4.00 MiB
Total PE 6910
Alloc PE / Size 1790 / 6.99 GiB
Free PE / Size 5120 / 20.00 GiB # 20G空间已加入centos卷组中
VG UUID gw01h4-Tj7V-ZZsI-87hm-AZWI-Rof7-xxc2w2
- 查看所有的逻辑卷情况:
[root@yunmx ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID zRKv1H-1CK4-0wGe-Pch0-u25X-Gcv7-DdFOdG
LV Write Access read/write
LV Creation host, time localhost, 2021-11-11 20:43:55 +0800
LV Status available
# open 2
LV Size 1.00 GiB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/centos/home
LV Name home
VG Name centos
LV UUID vZAlto-THBR-KVdM-VkOk-brdN-pItn-uT3a62
LV Write Access read/write
LV Creation host, time localhost, 2021-11-11 20:43:56 +0800
LV Status available
# open 1
LV Size 4.00 GiB
Current LE 1024
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID T2ydoQ-suHO-AAxz-XAKo-7mtR-5Hl2-MD3Pdg
LV Write Access read/write
LV Creation host, time localhost, 2021-11-11 20:43:58 +0800
LV Status available
# open 1
LV Size 1.99 GiB
Current LE 510
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
- 我们需要扩容的逻辑卷信息:
LV Path /dev/centos/root
LV Name root
- 将逻辑卷增加20G空间大小:
[root@yunmx ~]# lvextend -L +20G /dev/centos/root
Size of logical volume centos/root changed from 1.99 GiB (510 extents) to 21.99 GiB (5630 extents).
Logical volume centos/root successfully resized.
- 刷新逻辑卷最大的可用大小:
[root@yunmx ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=130560 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=522240, 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 522240 to 5765120
- 检查调整后的大小:
[root@yunmx ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 8G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 7G 0 part
├─centos-root 253:0 0 22G 0 lvm /
├─centos-swap 253:1 0 1G 0 lvm [SWAP]
└─centos-home 253:2 0 4G 0 lvm /home
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─centos-root 253:0 0 22G 0 lvm /
sr0 11:0 1 1024M 0 rom
可以看出根目录空间大小已经从最开始的2G扩展到22G了,这就OK了
总结:
- 优点:
-
文件系统可以跨磁盘
-
动态扩容。
-
方便导出整个卷组到另外一台机器。
- 缺点:
- 卷组中的一个磁盘损坏时,整个卷组都会受到影响。
- 因为加入了额外的操作,存储性能受到影响。