记一次 对 xfs格式 逻辑卷进行扩缩容
背景
前人在部署服务器的时候,给根目录预留的空间太小了,导致总告警,虽然会定时自动清理,但是总触发告警总是不好的。
查看发现 /home
目录空间非常大,而且利用率非常低,所以想着把 /home
目录的空间匀一点给根目录。
但是发现逻辑卷的格式是 xfs
格式的,是只能扩容,无法缩容的,缩容后需要是需要重新格式化的,会损坏原有逻辑卷的
所以,需要先备份home文件夹,再做其他操作。
解决
环境
- Centos 7.9系统
查看逻辑卷
$ lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home centos -wi-a----- <1.03t
root centos -wi-ao---- 50.00g
swap centos -wi-ao---- 15.56g
$ vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 3 0 wz--n- 1.09t 4.00m
备份 home 文件夹
安装xfsdump工具
yum install xfsdump -y
备份
$ xfsdump -f home.xfs_dump /home
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.7 (dump format 3.0) - type ^C for status and control
============================= dump label dialog ==============================
please enter label for this dump session (timeout in 300 sec)
-> #自定义备份会话标签,可直接回车
session label entered: ""
--------------------------------- end dialog ---------------------------------
xfsdump: WARNING: no session label specified
xfsdump: level 0 dump of localhost.localdomain:/home
xfsdump: dump date: Wed Dec 4 10:35:26 2024
xfsdump: session id: 0cbdb35b-815a-4d1b-a571-3a07de7742f1
xfsdump: session label: ""
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 6814901952 bytes
xfsdump: /var/lib/xfsdump/inventory created
============================= media label dialog =============================
please enter label for media in drive 0 (timeout in 300 sec)
-> #自定义备份媒体标签,可直接回车
media label entered: ""
--------------------------------- end dialog ---------------------------------
xfsdump: WARNING: no media label specified
xfsdump: creating dump session media file 0 (media 0, file 0)
xfsdump: dumping ino map
xfsdump: dumping directories
xfsdump: dumping non-directory files
xfsdump: ending media file
xfsdump: media file size 6792460464 bytes
xfsdump: dump size (non-dir files) : 6777958024 bytes
xfsdump: dump complete: 111 seconds elapsed
xfsdump: Dump Summary:
xfsdump: stream 0 /root/home.xfs_dump OK (success)
xfsdump: Dump Status: SUCCESS
卸载home分区
umount /home
缩容
$ lvreduce -L -200G /dev/centos/home
WARNING: Reducing active logical volume to <851.02 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos/home? [y/n]: y # 问是否真的确定要缩减home逻辑卷 输入y
Size of logical volume centos/home changed from <1.03 TiB (269060 extents) to <851.02 GiB (217860 extents).
Logical volume centos/home successfully resized.
扩容根分区
将所有空闲的空间都分配给根分区
$ lvextend -l +100%free /dev/centos/root
Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 250.00 GiB (64001 extents).
Logical volume centos/root successfully resized.
刷新文件系统
$ xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512 agcount=16, agsize=819200 blks
= sectsz=4096 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=13107200, imaxpct=25
= sunit=64 swidth=64 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=6400, version=2
= sectsz=4096 sunit=1 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 13107200 to 65537024
确认扩容是否成功
$ df -hT /root/
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 250G 40G 211G 16% /
格式化home分区
$ mkfs.xfs /dev/centos/home -f
meta-data=/dev/centos/home isize=512 agcount=32, agsize=6971520 blks
= sectsz=4096 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=223088640, imaxpct=25
= sunit=64 swidth=64 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=108930, version=2
= sectsz=4096 sunit=1 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
重新挂载
mount -a
因为在/etc/fstab
文件中原本就有配置挂载信息,所以这里只需要 -a
就好了
还原之前备份的内容
$ xfsrestore -f home.xfs_dump /home/
xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 3.1.7 (dump format 3.0) - type ^C for status and control
xfsrestore: searching media for dump
xfsrestore: examining media file 0
xfsrestore: dump description:
xfsrestore: hostname: localhost.localdomain
xfsrestore: mount point: /home
xfsrestore: volume: /dev/mapper/centos-home
xfsrestore: session time: Wed Dec 4 10:35:26 2024
xfsrestore: level: 0
xfsrestore: session label: ""
xfsrestore: media label: ""
xfsrestore: file system id: 5df6993f-f53f-4cf1-b99d-ae7c1f5588d8
xfsrestore: session id: 0cbdb35b-815a-4d1b-a571-3a07de7742f1
xfsrestore: media id: cb52e70c-76a2-4ce4-9e3d-62c95d6ab233
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 201 directories and 21470 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 23 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore: stream 0 /root/home.xfs_dump OK (success)
xfsrestore: Restore Status: SUCCESS
检查核对文件系统是否达到预期
可以看到,已经达到了预期目标。
注意
有时候,挂载点正在使用,卸载的时候会提示繁忙,可以使用 lsof
来检查关闭
lsof +D /home
递归查询 /home
目录下的所有被占用的文件
然后再根据实际情况杀掉这些进程。
有时候,不是对某个目录进行扩容,而是把缩减的空间重新分区挂载
这个时候就需要在格式化新分区后,手动挂载,并且将挂载信息写入到 /etc/fstab
文件中
本文来自博客园,作者:厚礼蝎,转载请注明原文链接:https://www.cnblogs.com/guangdelw/p/18585754