Linux 逻辑卷扩容

时间:2024-01-03 13:29:32

Linux 逻辑卷扩容

关键词:pv(物理卷)、vg(卷组) 、lv(逻辑卷)

今天在用linux过程中,根分区容量不够了,突然想起来好久没更新博客,就来说说逻辑卷扩容的问题吧。

1、扩容前的检查

记住/dev/mapper/rhel-root 我们等会给它扩容,记好root的分区类型为xfs

[root@hsun ~]# df -hT
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root xfs    10G 6.7G 3.4G 67% /
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.9G 4.0K 1.9G 1% /dev/shm
tmpfs tmpfs 1.9G 9.2M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/rhel-usr xfs 35G 24G 12G 68% /usr
/dev/sda1 xfs 1014M 145M 870M 15% /boot
/dev/mapper/rhel-var xfs 8.0G 5.5G 2.6G 69% /var
/dev/mapper/rhel-opt xfs 4.0G 1.3G 2.8G 32% /opt
/dev/mapper/rhel-tmp xfs 4.0G 34M 4.0G 1% /tmp
tmpfs tmpfs 378M 0 378M 0% /run/user/1002
tmpfs tmpfs 378M 0 378M 0% /run/user/20001116
tmpfs tmpfs 378M 0 378M 0% /run/user/0

可以看见root的VG(卷组)是rhel

[root@hsun ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
opt rhel -wi-ao---- .00g
root rhel -wi-ao---- .90g
swap rhel -wi-ao---- .00g
tmp rhel -wi-ao---- .00g
usr rhel -wi-ao---- <.00g
var rhel -wi-ao---- .00g

而vg(卷组)和pv(物理卷)的剩余空间都为0,所以我们要增加一块新的磁盘

[root@hsun ~]# vgs
VG #PV #LV #SN Attr VSize VFree
rhel wz--n- <.00g
[root@hsun ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- <.00g

我们先将机器关机

[root@hsun ~]# poweroff -f
Powering off.

2、增加新磁盘(给机器插入一块新的硬盘)

在VMware中增加一块硬盘,大小为4G。增加完了之后开机

Linux  逻辑卷扩容

3、扩容磁盘

3.1 开机之后检查新的硬盘

可以看见,刚刚插入的磁盘已经读出来了,在我的机器上为/dev/sdb大小为4G

[root@hsun ~]# fdisk -l | grep /dev/sd
磁盘 /dev/sdb: MB, 字节, 个扇区
磁盘 /dev/sda:75.2 GB, 字节, 个扇区
/dev/sda1 * Linux
/dev/sda2 8e Linux LVM

3.2 创建PV

[root@hsun ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- <.00g
[root@hsun ~]# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created.
[root@hsun ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- <.00g
/dev/sdb lvm2 --- .00g .00g

3.3 将PV的空间划入名为rhel的VG

[root@hsun ~]# vgs
VG #PV #LV #SN Attr VSize VFree
rhel wz--n- <.00g
[root@hsun ~]# vgextend rhel /dev/sdb
Volume group "rhel" successfully extended
[root@hsun ~]# vgs
VG #PV #LV #SN Attr VSize VFree
rhel wz--n- .99g <.00g
可以看到卷组vg已经成功的有69G扩容到73G,剩余4G未使用的空间

3.4 对root逻辑卷进行扩容操作

[root@hsun ~]# lvextend -L +.9G /dev/rhel/root
Rounding size to boundary between physical extents: 3.90 GiB.
Size of logical volume rhel/root changed from 10.00 GiB ( extents) to 13.90 GiB ( extents).
Logical volume rhel/root successfully resized.

3.5 刷新容量

紧接着刷新一下容量,因为分区类型为xfs,所以使用命令xfs_growfs刷新容量

如果分区类型为ext类型的,则用resize2fs命令进行刷新

[root@hsun ~]# xfs_growfs /dev/rhel/root 

meta-data=/dev/mapper/rhel-root isize= agcount=, agsize= blks
= sectsz= attr=, projid32bit=
= crc= finobt= spinodes=
data = bsize= blocks=, imaxpct=
= sunit= swidth= blks
naming =version bsize= ascii-ci= ftype=
log =internal bsize= blocks=, version=
= sectsz= sunit= blks, lazy-count=
realtime =none extsz= blocks=, rtextents=
data blocks changed from to

此时可以看见root已经扩容完成,大小为14G

[root@hsun ~]# df -hT
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root xfs 14G .7G .3G % /
devtmpfs devtmpfs .9G .9G % /dev
tmpfs tmpfs .9G .0K .9G % /dev/shm
tmpfs tmpfs .9G 9.2M .9G % /run
tmpfs tmpfs .9G .9G % /sys/fs/cgroup
/dev/mapper/rhel-usr xfs 35G 24G 12G % /usr
/dev/sda1 xfs 1014M 145M 870M % /boot
/dev/mapper/rhel-var xfs .0G .5G .6G % /var
/dev/mapper/rhel-opt xfs .0G .3G .8G % /opt
/dev/mapper/rhel-tmp xfs .0G 34M .0G % /tmp
tmpfs tmpfs 378M 378M % /run/user/
tmpfs tmpfs 378M 378M % /run/user/
tmpfs tmpfs 378M 378M % /run/user/

逻辑卷扩容到此结束。

如果有什么不懂的地方或者意见,请私信我或者在评论区进行留言,看到之后第一时间回复。

本人linux新手,希望各位linux大佬多多指教。