无人在意的文章
·
RAID存储
RAID实践
管理RAID0
1.创建RAID0
#安装mdadm工具【CENTOS]系统
[root@centos7 ~ 09:55:19]# yum install -y mdadm
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
#创建RAID0阵列:设备名/DEV/MD0,级别0,成员盘2块(sdb,sdc)
[root@centos7 ~ 10:09:08]# mdadm --create /dev/md0 --level 0 --raid-devices 2 /dev/sd{b,c}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
先制定RAID级别,再指定数量,最后定点到具体盘是谁
2.查看RAID0状态
#查看RAID概要信息(内核态RAID状态)
[root@centos7 ~ 10:25:35]# cat /proc/mdstat
Personalities : [raid0]
md0 : active raid0 sdc[1] sdb[0]
41908224 blocks super 1.2 512k chunks
unused devices: <none>
#查看RAID设备详细信息
[root@centos7 ~ 10:25:43]# mdadm --detail /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Thu Apr 9 10:25:35 2026
Raid Level : raid0
Array Size : 41908224 (39.97 GiB 42.91 GB)
Raid Devices : 2
Total Devices : 2
Persistence : Superblock is persistent
Update Time : Thu Apr 9 10:25:35 2026
State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0
Chunk Size : 512K
Consistency Policy : none
Name : centos7.my.cloud:0 (local to host centos7.my.cloud)
UUID : e8844310:90eeec54:3c0cca72:8016a202
Events : 0
Number Major Minor RaidDevice State
0 8 16 0 active sync /dev/sdb
1 8 32 1 active sync /dev/sdc
关注级别,状态,条带快大小和成员盘状态
#查看RAID设备与物理盘的映射关系
[root@centos7 ~ 10:25:53]# lsblk /dev/md0
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
md0 9:0 0 40G 0 raid0
[root@centos7 ~ 10:26:03]# lsblk /dev/sdb /dev/sdc
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
└─md0 9:0 0 40G 0 raid0
sdc 8:32 0 20G 0 disk
└─md0 9:0 0 40G 0 raid0
3.格式化与挂载RAID 0
#格式化RAID设备为XFS文件系统
[root@centos7 ~ 10:26:12]# mkfs.xfs /dev/md0
meta-data=/dev/md0 isize=512 agcount=16, agsize=654720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=10475520, imaxpct=25
= sunit=128 swidth=256 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=5120, version=2
= sectsz=512 sunit=8 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
#创建挂载点
[root@centos7 ~ 10:26:28]# mkdir -p /raid/raid0
#挂载RAID设备到挂载点
[root@centos7 ~ 10:26:41]# mount /dev/md0 /raid/raid0
#验证挂载结果
[root@centos7 ~ 10:26:56]# df -h /raid/raid0
Filesystem Size Used Avail Use% Mounted on
/dev/md0 40G 33M 40G 1% /raid/raid0
#测试数据的写入
[root@centos7 ~ 10:27:04]# cp /etc/ho* /raid/raid0
[root@centos7 ~ 10:27:18]# ls /raid/raid0
host.conf hostname hosts hosts.allow hosts.deny
4.删除RAID 0
#卸载挂载点
[root@centos7 ~ 10:27:27]# umount /dev/md0
#销毁阵列
[root@centos7 ~ 10:27:40]# mdadm --stop /dev/md0
mdadm: stopped /dev/md0
#清除物理盘上的RAID超级块(恢复为普通磁盘)
[root@centos7 ~ 10:27:47]# mdadm --zero-superblock /dev/sd{b,c}
5.补充说明
1.RAID 0不支持新增成员盘扩展容量:
[root@centos7 ~ 10:27:55]# mdadm --add /dev/md0 /dev/sdd mdadm: add new device failed for /dev/sdd as 2: Invalid argument
2.RAID 0不支持标记单盘故障(无冗余,单盘故障即阵列失效):
[root@centos7 ~]# mdadm --fail /dev/md0 /dev/sdc mdadm: Cannot remove /dev/sdc from /dev/md0, array will be failed.
管理 RAID 1
1 创建 RAID 1
[root@centos7 ~ 10:41:50]# mdadm --create /dev/md1 --level 1 --raid-devices 2 /dev/sd{b,c}
mdadm: Note: this array has metadata at the start and
may not be suitable as a boot device. If you plan to
store '/boot' on this device please ensure that
your boot-loader understands md/v1.x metadata, or use
--metadata=0.90
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.
注释:RAID 1元数据默认存储在磁盘开头,若用于/boot分区需指定--metadata=0.90兼容老旧引导程序。
2 查看 RAID 1 状态
[root@centos7 ~ 11:03:00]# mdadm --detail /dev/md1
/dev/md1:
Version : 1.2
Creation Time : Thu Apr 9 11:03:00 2026
Raid Level : raid1
Array Size : 20954112 (19.98 GiB 21.46 GB)
Used Dev Size : 20954112 (19.98 GiB 21.46 GB)
Raid Devices : 2
Total Devices : 2
Persistence : Superblock is persistent
Update Time : Thu Apr 9 11:03:26 2026
State : clean, resyncing
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0
Consistency Policy : resync
Resync Status : 28% complete
Name : centos7.my.cloud:1 (local to host centos7.my.cloud)
UUID : 6ce58269:8b97fdd2:1022ce38:1d93b30d
Events : 4
Number Major Minor RaidDevice State
0 8 16 0 active sync /dev/sdb
1 8 32 1 active sync /dev/sdc
注释:resyncing表示镜像数据正在同步,需等待同步完成(100%)后再进行格式化操作。
[root@centos7 ~ 11:03:29]# lsblk /dev/md1
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
md1 9:1 0 20G 0 raid1
[root@centos7 ~ 11:03:48]# lsblk /dev/sdb /dev/sdc
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
└─md1 9:1 0 20G 0 raid1
sdc 8:32 0 20G 0 disk
└─md1 9:1 0 20G 0 raid1
3 格式化与挂载 RAID 1
# 等待同步完成后,格式化RAID设备
[root@centos7 ~ 11:04:05]# mkfs.xfs /dev/md1
mkfs.xfs: /dev/md1 appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.
# 创建挂载点
[root@centos7 ~ 11:04:30]# mkdir /raid/raid1
# 挂载设备
[root@centos7 ~ 11:04:57]# mount /dev/md1 /raid/raid1
mount: /dev/md1: can't read superblock
# 验证挂载
[root@centos7 ~ 11:05:06]# df -h /raid/raid1
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 50G 1.6G 49G 4% /
# 测试数据写入
[root@centos7 ~]# cp /etc/ho* /raid/raid1
[root@centos7 ~ 11:05:55]# cp /etc/ho* /raid/raid1
[root@centos7 ~ 11:06:20]# ls /raid/raid1
host.conf hostname hosts hosts.allow hosts.deny
4 增加热备盘
[root@centos7 ~ 11:06:29]# mdadm --add /dev/md1 /dev/sdd
mdadm: added /dev/sdd
# 查看热备盘状态(spare为备用)
[root@centos7 ~ 11:06:39]# mdadm --detail /dev/md1 |tail -5
Number Major Minor RaidDevice State
0 8 16 0 active sync /dev/sdb
1 8 32 1 active sync /dev/sdc
2 8 48 - spare /dev/sdd
5 模拟磁盘故障
[root@centos7 ~ 11:06:48]# mdadm --fail /dev/md1 /dev/sdc
mdadm: set /dev/sdc faulty in /dev/md1
# 查看故障后状态(sdd自动顶替并同步)
[root@centos7 ~ 11:07:28]# mdadm --detail /dev/md1 |tail -5
Number Major Minor RaidDevice State
0 8 16 0 active sync /dev/sdb
2 8 48 1 spare rebuilding /dev/sdd
1 8 32 - faulty /dev/sdc# 验证数据可正常访问
[root@centos7 ~ 11:07:37]# ls /raid/raid1/
host.conf hostname hosts hosts.allow hosts.deny
[root@centos7 ~ 11:07:50]# cat /raid/raid1/hostname
centos7.my.cloud
6 删除故障磁盘
# 移除故障盘sdc
[root@centos7 ~ 11:07:58]# mdadm --remove /dev/md1 /dev/sdc
mdadm: hot removed /dev/sdc from /dev/md1
# 验证移除结果
[root@centos7 ~ 11:08:08]# mdadm --detail /dev/md1 |tail -5
Events : 28
Number Major Minor RaidDevice State
0 8 16 0 active sync /dev/sdb
2 8 48 1 spare rebuilding /dev/sdd
7 再次模拟故障
[root@centos7 ~ 11:08:17]# mdadm --fail /dev/md1 /dev/sdd
mdadm: set /dev/sdd faulty in /dev/md1
[root@centos7 ~ 11:08:46]# mdadm -D /dev/md1|tail -5
Number Major Minor RaidDevice State
0 8 16 0 active sync /dev/sdb
- 0 0 1 removed
2 8 48 - faulty /dev/sdd
[root@centos7 ~ 11:08:54]# ls /raid/raid1
host.conf hostname hosts hosts.allow hosts.deny
[root@centos7 ~ 11:09:02]# echo hello raid1 > /raid/raid1/test.txt
[root@centos7 ~ 11:09:08]#
[root@centos7 ~ 11:09:09]# umount /raid/raid1
umount: /raid/raid1: not mounted
[root@centos7 ~ 11:09:15]# mount /dev/md1 /raid/raid1
mount: /dev/md1: can't read superblock
[root@centos7 ~ 11:09:22]# ls /raid/raid1
host.conf hostname hosts hosts.allow hosts.deny test.txt
总结:raid1阵列中任一成员故障,不影响数据的完整性。
8 删除 RAID 1
# 卸载挂载点
[root@centos7 ~ 11:09:30]# umount /dev/md1
umount: /dev/md1: not mounted
# 停止RAID阵列
[root@centos7 ~ 11:10:04]# mdadm --stop /dev/md1
mdadm: stopped /dev/md1
# 清除物理盘超级块
[root@centos7 ~ 11:10:11]# mdadm --zero-superblock /dev/sd{b..d}
ddg工具彻底清盘
[root@centos7 ~ 11:18:35]# dd if=/dev/zero of=/dev/sdb bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 8.67236 s, 124 MB/s
9 补充说明
RAID 1核心价值是数据冗余,而非扩容:即使新增磁盘,阵列总容量仍等于单盘容量(镜像机制),无法通过加盘提升可用空间。
管理 RAID 5
1 创建 RAID 5
# 创建RAID 5阵列:设备名/dev/md5,级别5,成员盘4块(sdb、sdc、sdd、sde)
[root@centos7 ~ 11:35:17]# mdadm --create /dev/md5 --level 5 --raid-devices 4 /dev/sd{b..e}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md5 started.
注释:RAID 5最少需3块盘,此处用4块盘(1块用于分布式校验)。
2 查看 RAID 5 状态
[root@centos7 ~ 11:36:13]# mdadm --detail /dev/md5
/dev/md5:
Version : 1.2
Creation Time : Thu Apr 9 11:36:12 2026
Raid Level : raid5
Array Size : 62862336 (59.95 GiB 64.37 GB)
Used Dev Size : 20954112 (19.98 GiB 21.46 GB)
Raid Devices : 4
Total Devices : 4
Persistence : Superblock is persistent
Update Time : Thu Apr 9 11:36:32 2026
State : clean, degraded, recovering
Active Devices : 3
Working Devices : 4
Failed Devices : 0
Spare Devices : 1
Layout : left-symmetric
Chunk Size : 512K
Consistency Policy : resync
Rebuild Status : 24% complete
Name : centos7.my.cloud:5 (local to host centos7.my.cloud)
UUID : 10f1bf8a:164d818f:01ea7986:875b2301
Events : 4
Number Major Minor RaidDevice State
0 8 16 0 active sync /dev/sdb
1 8 32 1 active sync /dev/sdc
2 8 48 2 active sync /dev/sdd
4 8 64 3 spare rebuilding /dev/sde
注释:recovering表示阵列正在构建/同步,需等待同步完成后再格式化。
[root@centos7 ~ 11:36:39]# ls
ls lsblk lshw lsipc lslogins lsmod lspci
lsattr lscpu lsinitrd lslocks lsmem lsns lsscsi
[root@centos7 ~ 11:36:39]# lsblk /dev/md5
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
md5 9:5 0 60G 0 raid5
[root@centos7 ~ 11:37:01]# lsblk /dev/sd{b..e}
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
└─md5 9:5 0 60G 0 raid5
sdc 8:32 0 20G 0 disk
└─md5 9:5 0 60G 0 raid5
sdd 8:48 0 20G 0 disk
└─md5 9:5 0 60G 0 raid5
sde 8:64 0 20G 0 disk
└─md5 9:5 0 60G 0 raid5
3 格式化与挂载 RAID 5
[root@centos7 ~ 11:37:48]# mkfs.xfs /dev/md5
meta-data=/dev/md5 isize=512 agcount=16, agsize=982144 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=15714304, imaxpct=25
= sunit=128 swidth=384 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=7680, version=2
= sectsz=512 sunit=8 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@centos7 ~ 11:38:05]# mkdir /raid/raid5
[root@centos7 ~ 11:38:27]# mount /dev/md5 /raid/raid5
[root@centos7 ~ 11:38:50]# df -h /raid/raid5/
Filesystem Size Used Avail Use% Mounted on
/dev/md5 60G 33M 60G 1% /raid/raid5
# 测试数据写入
[root@centos7 ~ 11:39:08]# cp /etc/ho* /raid/raid5
[root@centos7 ~ 11:39:31]# ls /raid/raid5/
host.conf hostname hosts hosts.allow hosts.deny
4 增加热备盘
# 为RAID 5添加热备盘sdf
[root@centos7 ~ 11:39:40]# mdadm --add /dev/md5 /dev/sdf
mdadm: added /dev/sdf
#查看热备状态
[root@centos7 ~ 11:40:06]# mdadm --detail /dev/md5 | tail -7
Number Major Minor RaidDevice State
0 8 16 0 active sync /dev/sdb
1 8 32 1 active sync /dev/sdc
2 8 48 2 active sync /dev/sdd
4 8 64 3 active sync /dev/sde
5 8 80 - spare /dev/sdf
5 模拟磁盘故障
# 标记sdb为故障盘
[root@centos7 ~ 11:40:41]# mdadm --fail /dev/md5 /dev/sdb
mdadm: set /dev/sdb faulty in /dev/md5
# 查看故障后状态(sdf自动顶替并同步)
[root@centos7 ~ 11:41:15]# mdadm --detail /dev/md5 |tail -7
Number Major Minor RaidDevice State
5 8 80 0 spare rebuilding /dev/sdf
1 8 32 1 active sync /dev/sdc
2 8 48 2 active sync /dev/sdd
4 8 64 3 active sync /dev/sde
0 8 16 - faulty /dev/sdb
# 验证数据可正常访问
[root@centos7 ~ 11:41:45]# ls /raid/raid5/
host.conf hostname hosts hosts.allow hosts.deny
[root@centos7 ~ 11:43:47]# cat /raid/raid5/hostname
centos7.my.cloud
6 删除故障磁盘
# 移除故障盘sdb
[root@centos7 ~ 13:45:31]# mdadm --remove /dev/md5 /dev/sdb
mdadm: hot removed /dev/sdb from /dev/md5
# 验证移除结果
[root@centos7 ~ 13:45:53]# mdadm --detail /dev/md5 |tail -5
Number Major Minor RaidDevice State
5 8 80 0 active sync /dev/sdf
1 8 32 1 active sync /dev/sdc
2 8 48 2 active sync /dev/sdd
4 8 64 3 active sync /dev/sde
7 扩容 RAID 5
注释:RAID 5仅支持扩容(增加成员盘),不支持减容;扩容仅在阵列“clean”正常状态下可执行,降级/重构时禁止。
# 新增2块盘(sdb、sdg)到RAID 5
[root@centos7 ~ 13:46:17]# mdadm --add /dev/md5 /dev/sdb /dev/sdg
mdadm: added /dev/sdb
mdadm: added /dev/sdg
# 查看新增盘状态(spare为备用)
[root@centos7 ~ 13:47:43]# mdadm --detail /dev/md5 |tail -8
Number Major Minor RaidDevice State
5 8 80 0 active sync /dev/sdf
1 8 32 1 active sync /dev/sdc
2 8 48 2 active sync /dev/sdd
4 8 64 3 active sync /dev/sde
6 8 16 - spare /dev/sdb
7 8 96 - spare /dev/sdg
# 扩展阵列成员数为5(--grow为扩容参数)
[root@centos7 ~ 13:48:03]# mdadm --grow /dev/md5 --raid-devices 5
# 等待阵列重构完成(查看进度)
[root@centos7 ~ 13:49:16]# mdadm --detail /dev/md5
......
Reshape Status : 16% complete
Delta Devices : 1, (4->5)
Name : centos7.my.cloud:5 (local to host centos7.my.cloud)
UUID : 1d4e3f21:1178a4fd:db214497:593e3353
Events : 79
Number Major Minor RaidDevice State
5 8 80 0 active sync /dev/sdf
1 8 32 1 active sync /dev/sdc
2 8 48 2 active sync /dev/sdd
4 8 64 3 active sync /dev/sde
7 8 96 4 active sync /dev/sdg
6 8 16 - spare /dev/sdb
# 验证RAID容量(从60G扩容至80G)
[root@centos7 ~ 13:49:39]# lsblk /dev/md5
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
md5 9:5 0 80G 0 raid5 /raid/raid5
# 扩展文件系统(XFS文件系统用xfs_growfs)
[root@centos7 ~ 13:49:59]# xfs_growfs /raid/raid5
[root@centos7 ~ 13:50:22]# df -h /raid/raid5/
Filesystem Size Used Avail Use% Mounted on
/dev/md5 80G 604M 80G 1% /raid/raid5
8 再次模拟磁盘故障
[root@centos7 ~ 13:50:55]# mdadm --remove /dev/md5 /dev/sdb
[root@centos7 ~ 13:51:17]# mdadm --fail /dev/md5 /dev/sdg
[root@centos7 ~ 13:54:32]# mdadm -D /dev/md5 |tail
Events : 97
Number Major Minor RaidDevice State
5 8 80 0 active sync /dev/sdf
1 8 32 1 active sync /dev/sdc
2 8 48 2 active sync /dev/sdd
4 8 64 3 active sync /dev/sde
- 0 0 4 removed
7 8 96 - faulty /dev/sdg
[root@centos7 ~ 13:54:48]# ls /raid/raid5
host.conf hostname hosts hosts.allow hosts.deny
[root@centos7 ~ 13:54:57]# echo hello raid5 > /raid/raid5/test.txt
[root@centos7 ~ 13:55:04]# umount /raid/raid5
[root@centos7 ~ 13:50:13]# mount /dev/md5 /raid/raid5
[root@centos7 ~ 13:55:27]# ls /raid/raid5
host.conf hostname hosts hosts.allow hosts.deny test.txt
9 删除 RAID 5
# 卸载挂载点
[root@centos7 ~ 13:55:48]# umount /dev/md5
# 停止RAID阵列
[root@centos7 ~ 13:56:14]# mdadm --stop /dev/md5
mdadm: stopped /dev/md5
# 清除物理盘超级块
[root@centos7 ~ 13:56:47]# mdadm --zero-superblock /dev/sd{b..g}
也可以使用dd彻底擦除:
for device in /dev/sd{b..g}
do
dd if=/dev/zero of=$device bs=1M count=1024
done
10 重构 RAID 5
注释:若停止阵列后未清除超级块,可通过以下命令重构阵列,数据不丢失:
[root@centos7 ~]# mdadm --assemble /dev/md5 /dev/sd{b..g}
补充
raid阵列可以进一步使用分区技术分割成多个分区,然后再格式化使用。特别是raid5这种大容量的盘。
逻辑卷基本管理
创建物理卷(PV)
物理卷是 LVM 的基础,需先将块设备初始化为 PV 才能纳入 LVM 管理:
#创单个物理卷
[root@centos7 ~ 14:06:35]# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created.
#创多个物理卷(一次性)
[root@centos7 ~ 14:22:24]# pvcreate /dev/sd{c,d}
Physical volume "/dev/sdc" successfully created.
Physical volume "/dev/sdd" successfully created.
#查看所有PV列表
[root@centos7 ~ 14:22:44]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <199.00g 4.00m
/dev/sdb lvm2 --- 20.00g 20.00g
/dev/sdc lvm2 --- 20.00g 20.00g
/dev/sdd lvm2 --- 20.00g 20.00g
#查看单个PV详细信息
[root@centos7 ~ 14:45:29]# pvdisplay /dev/sdb
"/dev/sdb" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb
VG Name
PV Size 20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID Z0YcuI-HSGa-MZpx-Uih8-YtjY-sfVS-myr4lE
6 创建卷组(VG)
卷组整合多个物理卷形成统一存储池,可按需创建单 PV 或多 PV 的 VG:
#创建PV卷组
[root@centos7 ~ 14:45:38]# vgcreate webapp /dev/sdb
Volume group "webapp" successfully created
[root@centos7 ~ 14:46:55]# vgcreate dbapp /dev/sd{c,d}
Volume group "dbapp" successfully created
#查看PV归属
[root@centos7 ~ 14:47:27]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <199.00g 4.00m
/dev/sdb webapp lvm2 a-- <20.00g <20.00g
/dev/sdc dbapp lvm2 a-- <20.00g <20.00g
/dev/sdd dbapp lvm2 a-- <20.00g <20.00g
#查看VG列表
[root@centos7 ~ 14:47:32]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 3 0 wz--n- <199.00g 4.00m
dbapp 2 0 0 wz--n- 39.99g 39.99g
webapp 1 0 0 wz--n- <20.00g <20.00g
#查看VG信息
[root@centos7 ~ 14:47:38]# vgdisplay dbapp
--- Volume group ---
VG Name dbapp
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 39.99 GiB
PE Size 4.00 MiB
Total PE 10238
Alloc PE / Size 0 / 0
Free PE / Size 10238 / 39.99 GiB
VG UUID XnSlVD-h7nl-xiS6-Ee6s-f2cj-yaqo-pEiNDt
7.创逻辑卷
逻辑卷从卷组中划分,可直接创建文件系统,支持跨 PV 分配空间:
#创PV卷
[root@centos7 ~ 14:47:49]# lvcreate -n webapp01 -L 5G webapp
Logical volume "webapp01" created.
[root@centos7 ~ 14:48:15]# lvcreate -n data01 -L 25G dbapp
Logical volume "data01" created.
#查看LV列表
[root@centos7 ~ 14:48:34]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home centos -wi-ao---- <146.87g
root centos -wi-ao---- 50.00g
swap centos -wi-ao---- 2.12g
data01 dbapp -wi-a----- 25.00g
webapp01 webapp -wi-a----- 5.00g
逻辑卷设备名格式说明
逻辑卷支持三种访问路径,本质指向同一设备:
[root@centos7 ~ 14:48:38]# ls -l /dev/dbapp/data01 /dev/mapper/dbapp-data01
lrwxrwxrwx 1 root root 7 6月 17 18:26 /dev/dbapp/data01 -> ../dm-4
lrwxrwxrwx 1 root root 7 6月 17 18:26 /dev/mapper/dbapp-data01 -> ../dm-4
/dev/<VG名>/<LV名>:直观易记,推荐使用;/dev/mapper/<VG名>-<LV名>:系统映射路径;/dev/dm-N:内核设备映射器路径(N为数字)。
查看逻辑卷详细信息
[root@centos7 ~ 14:49:00]# lvdisplay /dev/dbapp/data01
--- Logical volume ---
LV Path /dev/dbapp/data01
LV Name data01
VG Name dbapp
LV UUID JkpZIg-gg9Y-chgm-K2fM-PGi5-N06g-3vBom7
LV Write Access read/write
LV Creation host, time centos7.my.cloud, 2026-04-09 14:48:34 +0800
LV Status available
# open 0
LV Size 25.00 GiB
Current LE 6400
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:4
#验证PV空间使用
[root@centos7 ~ 14:49:43]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <199.00g 4.00m
/dev/sdb webapp lvm2 a-- <20.00g <15.00g
/dev/sdc dbapp lvm2 a-- <20.00g 0
/dev/sdd dbapp lvm2 a-- <20.00g 14.99g
#验证LV跨盘
[root@centos7 ~ 14:49:50]# lsblk /dev/sd{b..d}
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sdb 8:16 0 20G 0 disk
└─webapp-webapp01 253:3 0 5G 0 lvm
sdc 8:32 0 20G 0 disk
└─dbapp-data01 253:4 0 25G 0 lvm
sdd 8:48 0 20G 0 disk
└─dbapp-data01 253:4 0 25G 0 lvm
8 在逻辑卷上创建文件系统
逻辑卷创建后需格式化文件系统并挂载,才能供业务使用:
1.格式化XFS文件系统
[root@centos7 ~ 14:50:00]# mkfs.xfs /dev/webapp/webapp01
meta-data=/dev/webapp/webapp01 isize=512 agcount=4, agsize=327680 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
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
#临时挂载逻辑卷
[root@centos7 ~ 14:50:35]# mount /dev/webapp/webapp01 /usr/share/ngnix/html
mount: mount point /usr/share/ngnix/html does not exist
#持久化挂载
[root@centos7 ~ 14:51:51]# vim /etc/fstab
末置/dev/webapp/webapp01 /usr/share/nginx/html xfs defaults 0 0
9 LVM 清理(删除 PV/VG/LV)
如需清理 LVM 配置,需按“卸载文件系统 → 删除 LV → 删除 VG → 删除 PV”的顺序操作:
[root@centos7 ~ 14:52:34]# umount /dev/webapp/webapp01
umount: /dev/webapp/webapp01: not mounted
[root@centos7 ~ 14:56:26]# lvremove /dev/webapp/webapp01 /dev/dbapp/data01
Do you really want to remove active logical volume webapp/webapp01? [y/n]: y
Logical volume "webapp01" successfully removed
Do you really want to remove active logical volume dbapp/data01? [y/n]: y
Logical volume "data01" successfully removed
[root@centos7 ~ 14:57:07]# vgremove webapp dbapp
Volume group "webapp" successfully removed
Volume group "dbapp" successfully removed
[root@centos7 ~ 14:57:22]# pvremove /dev/sd{b..d}
Labels on physical volume "/dev/sdb" successfully wiped.
Labels on physical volume "/dev/sdc" successfully wiped.
Labels on physical volume "/dev/sdd" successfully wiped.
卷组的扩展与缩减
卷组的容量可通过添加/移除 PV 灵活调整,满足业务存储需求变化。
1 环境准备
先创建基础卷组和逻辑卷,用于后续扩展/缩减测试:
#创建webapp
[root@centos7 ~ 14:57:48]# vgcreate webapp /dev/sdb
Physical volume "/dev/sdb" successfully created.
Volume group "webapp" successfully created
#创逻辑卷webapp01
[root@centos7 ~ 15:10:22]# lvcreate -n webapp01 -L 10G webapp
WARNING: xfs signature detected on /dev/webapp/webapp01 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/webapp/webapp01.
Logical volume "webapp01" created.
2 扩展卷组(VG)
当卷组空间不足时,可添加新 PV 扩展容量:
[root@centos7 ~ 15:11:02]# vgextend webapp /dev/sd{c,d}
Physical volume "/dev/sdc" successfully created.
Physical volume "/dev/sdd" successfully created.
Volume group "webapp" successfully extended
[root@centos7 ~ 15:11:28]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <199.00g 4.00m
/dev/sdb webapp lvm2 a-- <20.00g <10.00g
/dev/sdc webapp lvm2 a-- <20.00g <20.00g
/dev/sdd webapp lvm2 a-- <20.00g <20.00g
3 缩减卷组(VG)
如需移除卷组中的 PV(如更换更大硬盘),需确保 PV 未被使用,步骤如下:
[root@centos7 ~ 15:12:02]# vgreduce webapp /dev/sdb
Physical volume "/dev/sdb" still in use
[root@centos7 ~ 15:12:17]# pvmove /dev/sdb /dev/sdd
/dev/sdb: Moved: 4.02%
/dev/sdb: Moved: 100.00%
[root@centos7 ~ 15:13:03]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <199.00g 4.00m
/dev/sdb webapp lvm2 a-- <20.00g <20.00g
/dev/sdc webapp lvm2 a-- <20.00g <20.00g
/dev/sdd webapp lvm2 a-- <20.00g <10.00g
[root@centos7 ~ 15:14:10]# vgreduce webapp /dev/sdb
Removed "/dev/sdb" from volume group "webapp"
[root@centos7 ~ 15:14:31]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <199.00g 4.00m
/dev/sdb lvm2 --- 20.00g 20.00g
/dev/sdc webapp lvm2 a-- <20.00g <20.00g
/dev/sdd webapp lvm2 a-- <20.00g <10.00g
逻辑卷的扩展与缩减
逻辑卷的容量可直接调整,需结合卷组空闲空间操作。
1 扩展逻辑卷(LV)
卷组有空闲空间时,可直接扩展 LV 容量:
[root@centos7 ~ 15:14:38]# lvextend -L +2G /dev/webapp/webapp01
Size of logical volume webapp/webapp01 changed from 10.00 GiB (2560 extents) to 12.00 GiB (3072 extents).
Logical volume webapp/webapp01 successfully resized.
[root@centos7 ~ 15:15:15]# lvs /dev/webapp/webapp01
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
webapp01 webapp -wi-a----- 12.00g
2 缩减逻辑卷(LV)
逻辑卷空间充足时,可缩减容量(注意:缩减有数据丢失风险,需谨慎):
[root@centos7 ~ 15:15:44]# lvreduce -L -2G /dev/webapp/webapp01
WARNING: Reducing active logical volume to 10.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce webapp/webapp01? [y/n]: y
Size of logical volume webapp/webapp01 changed from 12.00 GiB (3072 extents) to 10.00 GiB (2560 extents).
Logical volume webapp/webapp01 successfully resized.
[root@centos7 ~ 15:16:12]# lvs /dev/webapp/webapp01
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
webapp01 webapp -wi-a----- 10.00g
文件系统的扩展与缩减
LV 容量调整后,需同步调整文件系统大小,否则无法使用新增空间(或缩减后空间异常)。
1 扩展 XFS 文件系统
XFS 是 CentOS7 默认文件系统,仅支持扩展,不支持缩减,步骤如下:
环境准备
#格式化为xfs
[root@centos7 ~ 15:16:19]# mkfs.xfs /dev/webapp/webapp01
meta-data=/dev/webapp/webapp01 isize=512 agcount=4, agsize=655360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=2621440, 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
#挂载
[root@centos7 ~ 15:42:58]# mkdir -p /usr/share/nginx/html
[root@centos7 ~ 15:43:32]# mount /dev/webapp/webapp01 /usr/share/nginx/html/
#写入测试数据
[root@centos7 ~ 15:44:02]# cp /etc/host* /usr/share/nginx/html/
[root@centos7 ~ 15:45:06]# ls /usr/share/nginx/html
host.conf hostname hosts hosts.allow hosts.deny
扩展操作
#扩展LV到15G
[root@centos7 ~ 15:45:18]# lvextend -L 15G /dev/webapp/webapp01
Size of logical volume webapp/webapp01 changed from 10.00 GiB (2560 extents) to 15.00 GiB (3840 extents).
Logical volume webapp/webapp01 successfully resized.
[root@centos7 ~ 15:46:18]# lvs /dev/webapp/webapp01
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
webapp01 webapp -wi-ao---- 15.00g #扩展XFS文件系统
[root@centos7 ~ 15:46:34]# xfs_growfs /usr/share/nginx/html/
meta-data=/dev/mapper/webapp-webapp01 isize=512 agcount=4, agsize=655360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=2621440, 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 2621440 to 3932160
#验证文件大小
[root@centos7 ~ 15:46:52]# df -h /usr/share/nginx/html/
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/webapp-webapp01 15G 33M 15G 1% /usr/share/nginx/html
[root@centos7 ~ 15:47:12]# ls /usr/share/nginx/html/
host.conf hostname hosts hosts.allow hosts.deny
#快捷方式
[root@centos7 ~ 15:47:29]# lvextend -rL 20G /dev/webapp/webapp01
Size of logical volume webapp/webapp01 changed from 15.00 GiB (3840 extents) to 20.00 GiB (5120 extents).
Logical volume webapp/webapp01 successfully resized.
meta-data=/dev/mapper/webapp-webapp01 isize=512 agcount=6, agsize=655360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=3932160, 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 3932160 to 5242880
#验证最终大小
[root@centos7 ~ 15:48:12]# lvs /dev/webapp/webapp01
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
webapp01 webapp -wi-ao---- 20.00g
[root@centos7 ~ 15:48:30]# df -h /usr/share/nginx/html/
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/webapp-webapp01 20G 33M 20G 1% /usr/share/nginx/html
逻辑卷快照
LVM 快照可保留逻辑卷某一时刻的完整数据,用于数据备份或恢复,核心特性:快照容量需不小于原 LV 容量(避免数据溢出)
# 1. 创建快照(-s 表示快照,-n 快照名,-L 快照大小,指定原 LV)
[root@centos7 ~ 16:32:40]# lvcreate -s -n webapp01-snap1 -L 9G /dev/webapp/webapp01
Logical volume "webapp01-snap1" created.
# 2. 挂载快照(查看快照中的数据)
# 快照的文件系统和原先逻辑卷的文件系统的uuid是一致的。 默认情况下,快照和原卷同一时刻只能挂载一个。
# 一定要挂载,则需要修改uuid
[root@centos7 ~ 16:33:36]# uuidgen
d8564a5e-3c81-4a6e-8241-bde3c39dfc6b
[root@centos7 ~ 16:34:40]# xfs_admin -U d8564a5e-3c81-4a6e-8241-bde3c39dfc6b/dev/webapp/webapp01-snap01
[root@centos7 ~ 16:35:20]# mkdir /webapp/webapp01-snap1
[root@centos7 ~ 16:36:13]# mount /dev/webapp/webapp01-snap1 /webapp/webapp01-snap1
# 3. 验证快照数据(与原 LV 一致)
[root@centos7 ~ 16:36:40]# ls /webapp/webapp01-snap1
host.conf hostname hosts hosts.allow hosts.deny
# 4. 在快照 LV 中写入新数据(验证快照独立性)
[root@centos7 ~ 16:37:20]# echo hello world > /webapp/webapp01-snap1/hello.txt
[root@centos7 ~ 16:37:48]# cat /webapp/webapp01-snap1/hello.txt
hello world
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐



所有评论(0)