linux-LVM 逻辑卷管理
linux-LVM 逻辑卷管理
- 一、LVM的核心组件
- 二、案例
- 1. 创建PV
- 2. 创建VG
- 3. 创建LV
- 4. 格式化LV
- 5. 挂载使用
- 6. 开机自动挂载
- 7. LV扩容
- VG里有空间
- VG里没有空间了,需要给VG扩容
- 三、正确关闭LVM的步骤
- 四、RAID与LVM
- 总结
一、LVM的核心组件
LVM(Logical Volume Manager):
PV 物理卷 Physical Volume
--》股东
被 LVM 管理的物理存储设备(如硬盘分区 /dev/sda1、整个硬盘 /dev/sdb 或 SSD),是 LVM 的最底层基础VG 卷组 Volume Vroup
--》公司
由一个或多个 PV 组合而成的逻辑存储池,相当于一个 “虚拟磁盘”,将多个物理设备的存储空间合并为一个整体LV 逻辑卷 Logical Volume
--》各项开支
从VG中划分出的逻辑存储空间,相当于传统分区,但大小可动态调整。用户可直接在 LV 上创建文件系统(如 ext4、XFS),并挂载使用
二、案例
案例描述:
公司的邮件服务器由于用户数量众多,邮件存储需要大量的空间,考虑到动态扩容的需要,计划增加两块SCSI硬盘并构建LVM逻辑卷,挂载到“/mail”目录专门用于存放邮件数据
1. 创建PV
pvcreate
pvscan
[root@rocky-2 ~]# pvcreate /dev/sdf # 创建Physical volume "/dev/sdf" successfully created.
[root@rocky-2 ~]# pvcreate /dev/sdhPhysical volume "/dev/sdh" successfully created.
[root@rocky-2 ~]# pvscan # 查看PV /dev/sda2 VG rl lvm2 [<99.00 GiB / 0 free]PV /dev/sdh lvm2 [600.00 GiB]PV /dev/sdf lvm2 [600.00 GiB]Total: 3 [<1.27 TiB] / in use: 1 [<99.00 GiB] / in no VG: 2 [1.17 TiB]
2. 创建VG
vgcreate
vgscan
vgdisplay
[root@rocky-2 ~]# vgcreate mail_store /dev/sdh /dev/sdfVolume group "mail_store" successfully created
[root@rocky-2 ~]# vgscan Found volume group "rl" using metadata type lvm2Found volume group "mail_store" using metadata type lvm2
[root@rocky-2 ~]# vgdisplay mail_store # 详细查看指定 VG 信息--- Volume group ---VG Name mail_storeSystem ID Format lvm2Metadata Areas 2Metadata Sequence No 1VG Access read/writeVG Status resizableMAX LV 0Cur LV 0Open LV 0Max PV 0Cur PV 2Act PV 2VG Size 1.17 TiBPE Size 4.00 MiBTotal PE 307198Alloc PE / Size 0 / 0 分配出去的PE allocate 分配Free PE / Size 307198 / 1.17 TiB 空余的PEVG UUID t5spYg-MiJt-K2sB-qSW0-P4yj-cZQd-l2DSSG
3. 创建LV
lvcreate
lvscan
lvdisplay
[root@rocky-2 ~]# lvcreate -L 500G -n scmail mail_storeLogical volume "scmail" created.
[root@rocky-2 ~]# lvscan ACTIVE '/dev/rl/swap' [7.75 GiB] inheritACTIVE '/dev/rl/home' [29.93 GiB] inheritACTIVE '/dev/rl/root' [<61.31 GiB] inheritACTIVE '/dev/mail_store/scmail' [500.00 GiB] inherit# 再创建一个LV ndmail
[root@rocky-2 ~]# lvcreate -L 600G -n ndmail mail_storeLogical volume "ndmail" created.[root@rocky-2 ~]# lvscan ACTIVE '/dev/rl/swap' [7.75 GiB] inheritACTIVE '/dev/rl/home' [29.93 GiB] inheritACTIVE '/dev/rl/root' [<61.31 GiB] inheritACTIVE '/dev/mail_store/scmail' [500.00 GiB] inheritACTIVE '/dev/mail_store/ndmail' [600.00 GiB] inherit[root@rocky-2 ~]# lvdisplay /dev/mail_store/scmail --- Logical volume ---LV Path /dev/mail_store/scmailLV Name scmailVG Name mail_storeLV UUID gyMYcw-mm4j-NV9e-RtOC-6xof-ck2j-yxa31DLV Write Access read/writeLV Creation host, time rocky-2, 2025-07-17 11:43:22 +0800LV Status available# open 0LV Size 500.00 GiBCurrent LE 128000Segments 1Allocation inheritRead ahead sectors auto- currently set to 256Block device 253:3
4. 格式化LV
mkfs.
[root@rocky-2 ~]# mkfs.xfs /dev/mail_store/scmail
meta-data=/dev/mail_store/scmail isize=512 agcount=4, agsize=32768000 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=1, sparse=1, rmapbt=0= reflink=1 bigtime=1 inobtcount=1 nrext64=0
data = bsize=4096 blocks=131072000, imaxpct=25= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=64000, version=2= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0[root@rocky-2 ~]# mkfs.xfs /dev/mail_store/ndmail
meta-data=/dev/mail_store/ndmail isize=512 agcount=4, agsize=39321600 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=1, sparse=1, rmapbt=0= reflink=1 bigtime=1 inobtcount=1 nrext64=0
data = bsize=4096 blocks=157286400, imaxpct=25= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=76800, version=2= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
5. 挂载使用
mount
[root@rocky-2 ~]# mkdir /scmail
[root@rocky-2 ~]# mkdir /ndmail
[root@rocky-2 ~]# mount /dev/mail_store/scmail /scmail
[root@rocky-2 ~]# mount /dev/mail_store/ndmail /ndmail
[root@rocky-2 ~]# df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
devtmpfs devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs tmpfs 3.7G 0 3.7G 0% /dev/shm
tmpfs tmpfs 1.5G 9.2M 1.5G 1% /run
/dev/mapper/rl-root xfs 62G 21G 42G 33% /
/dev/sda1 xfs 960M 242M 719M 26% /boot
/dev/mapper/rl-home xfs 30G 246M 30G 1% /home
tmpfs tmpfs 756M 0 756M 0% /run/user/0
/dev/mapper/mail_store-scmail xfs 500G 3.6G 497G 1% /scmail
/dev/mapper/mail_store-ndmail xfs 600G 4.3G 596G 1% /ndmail[root@rocky-2 ~]# cd /dev/mapper/
[root@rocky-2 mapper]# ls
control mail_store-ndmail mail_store-scmail rl-home rl-root rl-swap
[root@rocky-2 mapper]# ll
总用量 0
crw------- 1 root root 10, 236 7月 17 11:30 control
lrwxrwxrwx 1 root root 7 7月 17 11:46 mail_store-ndmail -> ../dm-4
lrwxrwxrwx 1 root root 7 7月 17 11:45 mail_store-scmail -> ../dm-3
lrwxrwxrwx 1 root root 7 7月 17 11:30 rl-home -> ../dm-2
lrwxrwxrwx 1 root root 7 7月 17 11:30 rl-root -> ../dm-0
lrwxrwxrwx 1 root root 7 7月 17 11:30 rl-swap -> ../dm-1
6. 开机自动挂载
修改/etc/fstab
文件
[root@rocky-2 ~]# vim /etc/fstab
/dev/mapper/mail_store-scmail /scmail xfs defaults 0 0
/dev/mapper/mail_store-ndmail /ndmail xfs defaults 0 0
[root@rocky-2 ~]# mount -a
[root@rocky-2 ~]# systemctl daemon-reload[root@rocky-2 ~]# df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
devtmpfs devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs tmpfs 3.7G 0 3.7G 0% /dev/shm
tmpfs tmpfs 1.5G 9.2M 1.5G 1% /run
/dev/mapper/rl-root xfs 62G 21G 42G 33% /
/dev/sda1 xfs 960M 242M 719M 26% /boot
/dev/mapper/mail_store-ndmail xfs 600G 4.3G 596G 1% /ndmail
/dev/mapper/mail_store-scmail xfs 500G 3.6G 497G 1% /scmail
/dev/mapper/rl-home xfs 30G 246M 30G 1% /home
tmpfs tmpfs 756M 0 756M 0% /run/user/0
7. LV扩容
lvextend
xfs_growfs
VG里有空间
[root@rocky-2 ~]# lvextend -L +200G /dev/mail_store/scmail Insufficient free space: 51200 extents needed, but only 25598 available
[root@rocky-2 ~]# lvextend -L +20G /dev/mail_store/scmail Size of logical volume mail_store/scmail changed from 500.00 GiB (128000 extents) to 520.00 GiB (133120 extents).Logical volume mail_store/scmail successfully resized.
[root@rocky-2 ~]# lvextend -L +200G /dev/mail_store/scmail Insufficient free space: 51200 extents needed, but only 20478 available
[root@rocky-2 ~]# vgdisplay --- Volume group ---VG Name rlSystem ID Format lvm2Metadata Areas 1Metadata Sequence No 4VG Access read/writeVG Status resizableMAX LV 0Cur LV 3Open LV 3Max PV 0Cur PV 1Act PV 1VG Size <99.00 GiBPE Size 4.00 MiBTotal PE 25343Alloc PE / Size 25343 / <99.00 GiBFree PE / Size 0 / 0 VG UUID XPUGbD-018q-p1PY-L3Z6-ox8q-9b81-BN8qvO--- Volume group ---VG Name mail_storeSystem ID Format lvm2Metadata Areas 2Metadata Sequence No 4VG Access read/writeVG Status resizableMAX LV 0Cur LV 2Open LV 2Max PV 0Cur PV 2Act PV 2VG Size 1.17 TiBPE Size 4.00 MiBTotal PE 307198Alloc PE / Size 286720 / 1.09 TiBFree PE / Size 20478 / 79.99 GiBVG UUID t5spYg-MiJt-K2sB-qSW0-P4yj-cZQd-l2DSSG[root@rocky-2 ~]# df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
devtmpfs devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs tmpfs 3.7G 0 3.7G 0% /dev/shm
tmpfs tmpfs 1.5G 18M 1.5G 2% /run
/dev/mapper/rl-root xfs 62G 21G 41G 34% /
/dev/sda1 xfs 960M 242M 719M 26% /boot
/dev/mapper/mail_store-ndmail xfs 600G 4.3G 596G 1% /ndmail
/dev/mapper/mail_store-scmail xfs 500G 3.6G 497G 1% /scmail
/dev/mapper/rl-home xfs 30G 246M 30G 1% /home
tmpfs tmpfs 756M 0 756M 0% /run/user/0
在lv里对应的文件系统,重新识别下xfs文件系统
[root@rocky-2 ~]# xfs_growfs /dev/mail_store/scmail
meta-data=/dev/mapper/mail_store-scmail isize=512 agcount=4, agsize=32768000 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=1, sparse=1, rmapbt=0= reflink=1 bigtime=1 inobtcount=1 nrext64=0
data = bsize=4096 blocks=131072000, imaxpct=25= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=64000, version=2= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 131072000 to 136314880[root@rocky-2 ~]# df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
devtmpfs devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs tmpfs 3.7G 0 3.7G 0% /dev/shm
tmpfs tmpfs 1.5G 18M 1.5G 2% /run
/dev/mapper/rl-root xfs 62G 21G 41G 34% /
/dev/sda1 xfs 960M 242M 719M 26% /boot
/dev/mapper/mail_store-ndmail xfs 600G 4.3G 596G 1% /ndmail
/dev/mapper/mail_store-scmail xfs 520G 3.7G 517G 1% /scmail
/dev/mapper/rl-home xfs 30G 246M 30G 1% /home
tmpfs tmpfs 756M 0 756M 0% /run/user/0
VG里没有空间了,需要给VG扩容
vgextend
创建PV,扩容到VG里
[root@rocky-2 ~]# pvcreate /dev/sdiPhysical volume "/dev/sdi" successfully created.[root@rocky-2 ~]# vgextend mail_store /dev/sdi # 扩容VG卷组名 PV的名字Volume group "mail_store" successfully extended[root@rocky-2 ~]# lvextend -L +200G /dev/mail_store/scmail Size of logical volume mail_store/scmail changed from 520.00 GiB (133120 extents) to 720.00 GiB (184320 extents).Logical volume mail_store/scmail successfully resized.[root@rocky-2 ~]# xfs_growfs /dev/mail_store/scmail
meta-data=/dev/mapper/mail_store-scmail isize=512 agcount=5, agsize=32768000 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=1, sparse=1, rmapbt=0= reflink=1 bigtime=1 inobtcount=1 nrext64=0
data = bsize=4096 blocks=136314880, imaxpct=25= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=64000, version=2= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 136314880 to 188743680
三、正确关闭LVM的步骤
umount
卸载LVM文件系统lvremove
删除逻辑卷vgremove
删除卷组pvremove
删除物理卷- 清除
/etc/fstab
中LVM的挂载记录 fdisk
修改分区ID
四、RAID与LVM
RAID
(Redundant Array of Independent Disks)独立磁盘冗余阵列 --》多块磁盘组合在一起,不同组合方式会有不同的效果
- 硬件RAID -->推荐使用 --》有单独的raid卡芯片,不使用服务器的cpu资源
速度快,性能好,支持热插拔
需专门的RAID磁盘阵列卡,价格昂贵,还需配合相应的驱动
- 软件RAID -->没有单独的raid卡来分配数据存取,需要消耗服务器的cpu资源
使用mdadm软件仿真磁盘阵列功能
无需专门硬件,性价比较好
设备文件标识是/dev/md0
根据可靠性、性能和成本对RADI划分了等级
RAID0、RAID1、RAID5、RAID6、RAID10、RAID01
RAID级别 | 核心原理 | 最少磁盘数 | 磁盘利用率 | 特点 |
---|---|---|---|---|
RAID0 | 条带卷 | 2块 | 100% | 读写性能好,没有冗余功能 |
RAID1 | 镜像卷 | 2块 | 50% | 但是有冗余功能 |
RAID5 | 条带+分布校验 | 3块 | n-1/n | 读写性能好,有冗余功能,只能坏一块磁盘 |
RAID6 | 条带+分布校验 | 4块 | n-2/n | 读写性能好,有冗余功能,只能坏2块磁盘 |
RAID10 (RAID 1+0) | 先镜像,再条带化 | 4块 | 50% | 读写性能好,有冗余功能,每组镜像中允许 1 块盘故障(最多同时坏 2 块,且不在同一镜像组) |
什么时候创建RAID?
服务器在启动的过程中,进入RAID配置界面,去配置raid阵列
RAID和LVM是怎么样结合的?
将 RAID 阵列视为一个 “大物理设备”,再在其上部署 LVM,从而同时获得 RAID 的可靠性 / 性能优势和 LVM 的灵活管理能力
总结
LVM 的核心价值是打破传统分区的固定性限制
通过 “物理卷 - 卷组 - 逻辑卷” 的三层抽象,实现存储资源的弹性管理、动态分配和灵活扩展,特别适合服务器、虚拟机等需要频繁调整存储空间的场景