背景
前人在部署伺服器的時候,給根目錄預留的空間太小了,導致總告警,雖然會定時自動清理,但是總觸發告警總是不好的。
檢視發現 /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
檔案中