LVM磁盘扩容
一、基本概念熟悉
LVM是 Logical Volume Manager(逻辑卷管理)它是Linux环境下对磁盘分区进行管理的一种机制。
LVM 的核心概念有:
- 物理卷 (PV): 一块物理硬盘, 或者硬盘上的一个分区. 也就是底层的物理存储设备.
- 卷组 (VG): 多个 PV 组成的一个存储池, 相当于一块虚拟大硬盘.
- 逻辑卷 (LV): 从 VG 之中划分, 相当于一个虚拟分区
将 PV 划分为许多小的数据存储块 (PE, 默认 4MB), VG 就是一堆 PE 的集合, 然后把 PE 分配给 LV. 当上层软件 (比如 filesystem 文件系统) 访问 LV 时, LVM 找出要访问的数据对应哪个 PE, 从而访问对应的 PV。
二、LVM扩容
Linux操作系统运行一段时间后,磁盘空间不足,需进行扩容。现将扩容过程进行记录。
1. 记录扩容前磁盘使用情况
查看磁盘分区情况:
[root@PAGL-FXQ-TEST ~]# fdisk -l
Disk /dev/vda: 1099.5 GB, 1099511627776 bytes, 2147483648 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: 0x000a9d54
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 2147483647 1072692224 8e Linux LVM
Disk /dev/mapper/centos-root: 2181.0 GB, 2181029691392 bytes, 4259823616 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 /dev/mapper/centos-swap: 16.9 GB, 16911433728 bytes, 33030144 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 /dev/vdb: 1099.5 GB, 1099511627776 bytes, 2147483648 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: 0x3cfffab4
Device Boot Start End Blocks Id System
/dev/vdb1 2048 2147483647 1073740800 83 Linux
查看pv、vg、lv 情况(清楚pv的剩余情况;vg的名称和剩余情况;lv名称和剩余情况)
[root@PAGL-FXQ-TEST ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 centos lvm2 a-- <1023.00g 0
/dev/vdb1 centos lvm2 a-- <1024.00g 0
[root@PAGL-FXQ-TEST ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 2 2 0 wz--n- <2.00t 0
[root@PAGL-FXQ-TEST ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 1.98t
swap centos -wi-ao---- 15.75g
列出所有块设备的基本信息,包括设备名、大小和挂载点等
[root@PAGL-FXQ-TEST ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 4.4G 0 rom /mnt/cdrom
vda 252:0 0 1T 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 1023G 0 part
├─centos-root 253:0 0 2T 0 lvm /
└─centos-swap 253:1 0 15.8G 0 lvm [SWAP]
vdb 252:16 0 1T 0 disk
└─vdb1 252:17 0 1024G 0 part
└─centos-root 253:0 0 2T 0 lvm /
查看磁盘空间占用情况(知道根分区空间占93%,空间严重不足)
[root@PAGL-FXQ-TEST ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 16G 0 16G 0% /dev
tmpfs 16G 9.3M 16G 1% /dev/shm
tmpfs 16G 1.5G 15G 10% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/mapper/centos-root 2.0T 1.9T 148G 93% /
/dev/vda1 1014M 202M 813M 20% /boot
tmpfs 3.2G 52K 3.2G 1% /run/user/0
/dev/sr0 4.4G 4.4G 0 100% /mnt/cdrom
2. 虚拟机添加磁盘
3. 添加磁盘后检查
可以看出多了/dev/vdc磁盘,如下:
[root@PAGL-FXQ-TEST ~]# fdisk -l
Disk /dev/vda: 1099.5 GB, 1099511627776 bytes, 2147483648 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: 0x000a9d54
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 2147483647 1072692224 8e Linux LVM
Disk /dev/mapper/centos-root: 2181.0 GB, 2181029691392 bytes, 4259823616 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 /dev/mapper/centos-swap: 16.9 GB, 16911433728 bytes, 33030144 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 /dev/vdb: 1099.5 GB, 1099511627776 bytes, 2147483648 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: 0x3cfffab4
Device Boot Start End Blocks Id System
/dev/vdb1 2048 2147483647 1073740800 83 Linux
Disk /dev/vdc: 2199.0 GB, 2199023255552 bytes, 4294967296 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
4.将新硬盘分区初始化
[root@PAGL-FXQ-TEST centos]# pvcreate /dev/vdc
Physical volume "/dev/vdc" successfully created.
[root@PAGL-FXQ-TEST centos]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 centos lvm2 a-- <1023.00g 0
/dev/vdb1 centos lvm2 a-- <1024.00g 0
/dev/vdc lvm2 --- 2.00t 2.00t
5.将初始化后的分区添加到VG中
[root@PAGL-FXQ-TEST centos]# vgextend centos /dev/vdc
Volume group "centos" successfully extended
[root@PAGL-FXQ-TEST centos]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 3 2 0 wz--n- <4.00t <2.00t
5.将VG分配给lv
lvm的名称为/dev/centos/root
[root@PAGL-FXQ-TEST centos]# lvextend -l +100%FREE /dev/centos/root
Size of logical volume centos/root changed from 1.98 TiB (519998 extents) to 3.98 TiB (1044285 extents).
Logical volume centos/root successfully resized.
[root@PAGL-FXQ-TEST centos]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 3 2 0 wz--n- <4.00t 0
[root@PAGL-FXQ-TEST centos]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- 3.98t
swap centos -wi-ao---- 15.75g
6.扩展文件系统
注意:lvextend扩展后只是扩展了lv的大小,而此时文件系统并未感知到,所有还需要使用xfs_growfs、resize2fs等命令来扩展文件系统,xfs_growf命令是扩展xfs文件系统,resize2fs是扩展ext4文件系统。
[root@PAGL-FXQ-TEST centos]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512 agcount=9, agsize=66010624 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=532477952, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=128927, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 532477952 to 1069347840
[root@PAGL-FXQ-TEST centos]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 16G 0 16G 0% /dev
tmpfs 16G 9.3M 16G 1% /dev/shm
tmpfs 16G 1.5G 15G 10% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/mapper/centos-root 4.0T 1.9T 2.2T 47% /
/dev/vda1 1014M 202M 813M 20% /boot
tmpfs 3.2G 52K 3.2G 1% /run/user/0
/dev/sr0 4.4G 4.4G 0 100% /mnt/cdrom
扩容成功!!
参考
https://blog.csdn.net/MssGuo/article/details/120475752
https://www.bilibili.com/read/cv35761174/
https://blog.csdn.net/ichen820/article/details/138958377