参考: https://zhuanlan.zhihu.com/p/659149805
yum install psmisc
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 24G 0 24G 0% /dev
tmpfs 24G 0 24G 0% /dev/shm
tmpfs 24G 17M 24G 1% /run
tmpfs 24G 0 24G 0% /sys/fs/cgroup
/dev/mapper/centos-root 50G 1.2G 49G 3% /
/dev/sda2 1014M 150M 865M 15% /boot
/dev/mapper/centos-home 2.2T 33M 2.2T 1% /home
tmpfs 4.7G 0 4.7G 0% /run/user/0
[root@localhost ~]#
2、查看文件系统
查看文件系统的类型,/root 根分区和 /home 分区类型一样才能进行压缩扩容调整。
[root@localhost ~]# mount | grep root
/dev/mapper/centos-root on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@localhost ~]#
[root@localhost ~]# mount | grep home
/dev/mapper/centos-home on /home type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@localhost ~]#
3、调整命令格式
ext2/ext3/ext4 文件系统的调整命令是 resize2fs(增大和减小都支持)
lvextend -L 120G /dev/mapper/centos-home //增大至120G
lvextend -L +20G /dev/mapper/centos-home //增加20G
lvreduce -L 50G /dev/mapper/centos-home //减小至50G
lvreduce -L -8G /dev/mapper/centos-home //减小8G
resize2fs /dev/mapper/centos-home //执行调整
xfs 文件系统的调整命令是 xfs_growfs(只支持增大)
lvextend -L 120G /dev/mapper/centos-home //增大至120G
lvextend -L +20G /dev/mapper/centos-home //增加20G
xfs_growfs /dev/mapper/centos-home //执行调整
4、调整步骤
通过检查文件类型就是 xfs,但是 xfs_frowfs 只支持增大,所以这里还能用其他办法。如果是 ext2/ext3/ext4类型,那就使用上面那个几个命令就行了。
(1)使用 umount /home/ 卸载home分区,如果提示home 正忙就先执行 fuser -km /home/ ,再执行umount /home/。
[root@localhost ~]# umount /home/
[root@localhost ~]# lvreduce -L -450G /dev/mapper/centos-home
WARNING: Reducing active logical volume to 1.67 TiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos/home? [y/n]: y
Size of logical volume centos/home changed from 2.11 TiB (553187 extents) to 1.67 TiB (437987 extents).
Logical volume centos/home successfully resized.
[root@localhost ~]#
(2)格式化 home 分区,再次挂载到 home 目录下。
[root@localhost ~]# mkfs.xfs /dev/mapper/centos-home -f
meta-data=/dev/mapper/centos-home isize=512 agcount=4, agsize=112124672 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=448498688, imaxpct=5
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=218993, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# mount /dev/mapper/centos-home /home/
[root@localhost ~]#
(3)再次查询分区大小 df -h 发现 home 分区大小已经减少了。
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 24G 0 24G 0% /dev
tmpfs 24G 0 24G 0% /dev/shm
tmpfs 24G 17M 24G 1% /run
tmpfs 24G 0 24G 0% /sys/fs/cgroup
/dev/mapper/centos-root 50G 1.2G 49G 3% /
/dev/sda2 1014M 150M 865M 15% /boot
tmpfs 4.7G 0 4.7G 0% /run/user/0
/dev/mapper/centos-home 1.7T 33M 1.7T 1% /home
(4)检查 vgdisplay 压缩之后的闲置空间。
[root@localhost ~]# vgdisplay
— Volume group —
VG Name centos
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size 2.18 TiB
PE Size 4.00 MiB
Total PE 572036
Alloc PE / Size 456835 / 1.74 TiB
Free PE / Size 115201 / 450.00 GiB
VG UUID XN9zKl-Xe4Z-tZSC-VHI7-RQ8n-a3Mr-n4N8nc
(5)将 Free 闲置 450G 调整给 root
[root@localhost ~]# lvextend -L +450G /dev/mapper/centos-root
Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 500.00 GiB (128000 extents).
Logical volume centos/root successfully resized.
[root@localhost ~]#
(6)执行调整操作
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=3276800 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=13107200, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=6400, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 13107200 to 131072000
(7)再次验证,此时 /root 分区大小已经增至 500G。
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 24G 0 24G 0% /dev
tmpfs 24G 0 24G 0% /dev/shm
tmpfs 24G 17M 24G 1% /run
tmpfs 24G 0 24G 0% /sys/fs/cgroup
/dev/mapper/centos-root 500G 1.2G 499G 1% /
/dev/sda2 1014M 150M 865M 15% /boot
tmpfs 4.7G 0 4.7G 0% /run/user/0
/dev/mapper/centos-home 1.7T 33M 1.7T 1% /home
[root@localhost ~]#