跳到主要内容返回博客列表CentOS LVM 扩容实战:home 分区合并到根分区
今天遇到一个常见的问题:服务器的根分区只有 50G,而 /home 分区有 957G 却几乎没用。这种默认分区很不合理,所以决定把 /home 的空间都给根分区用。 记录一下操作过程,方便以后查阅。 查看磁盘空间: bash root@lucifer ~ df -h Filesystem Size Used Avail Use% Mounted on /dev/map
- 2738+ 篇实战文章
- 1500+ 步骤截图
- 10 大核心模块
前言
本章目标:掌握本章核心知识点
前置要求:完成前序章节学习
预计时长:60 分钟
今天遇到一个常见的问题:服务器的根分区只有 50G,而 /home 分区有 957G 却几乎没用。这种默认分区很不合理,所以决定把 /home 的空间都给根分区用。
记录一下操作过程,方便以后查阅。
操作过程
查看当前磁盘情况
查看磁盘空间:
1[root@lucifer ~]# df -h
2Filesystem Size Used Avail Use% Mounted on
3/dev/mapper/centos-root 50G 1.8G 49G 4% /
4/dev/mapper/centos-home 957G 33M 957G 1% /home
根分区 50G,home 分区 957G,很多软件都不建议安装到 /home 目录下,所以不建议把空间都划分到 home 分区下。
1[root@lucifer ~]# lsblk
2NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
3sda 8:0 0 1T 0 disk
4├─sda1 8:1 0 200M 0 part /boot/efi
5├─sda2 8:2 0 1G 0 part /boot
6└─sda3 8:3 0 1022.8G 0 part
7 ├─centos-root 253:0 0 50G 0 lvm /
8 ├─centos-swap 253:1 0 15.8G 0 lvm [SWAP]
9 └─centos-home 253:2 0 957G 0 lvm /home
备份 home 目录
先看看 /home 下面有什么:
1[root@lucifer /]# cd /home/
2[root@lucifer home]# ls
3lucifer shell
就两个目录,先备份:
1[root@lucifer /]# mkdir -p /root/home_backup
2[root@lucifer /]# cp -a /home/* /root/home_backup/
停止相关服务
停掉可能会用到 /home 的服务:
1[root@lucifer /]# systemctl stop crond
2[root@lucifer /]# systemctl stop atd
3Failed to stop atd.service: Unit atd.service not loaded.
atd 服务没装,无所谓。
卸载 home 分区
1[root@lucifer /]# umount -l /home
删除 home 逻辑卷
1[root@lucifer /]# lvremove /dev/centos/home
2Do you really want to remove active logical volume centos/home? [y/n]: y
3 Logical volume "home" successfully removed
扩展根分区
把释放出来的空间全部给根分区:
1[root@lucifer /]# lvextend -l +100%FREE /dev/centos/root
2 Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 1007.05 GiB (257805 extents).
3 Logical volume centos/root successfully resized.
从 50G 扩展到了 1007G,爽!
扩展文件系统
逻辑卷扩展了,文件系统也要跟着扩展:
1[root@lucifer /]# xfs_growfs /dev/centos/root
2meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=3276800 blks
3...
4data blocks changed from 13107200 to 263992320
修改启动配置
编辑/etc/fstab,把/home 那行删掉或注释掉:
1[root@lucifer /]# vi /etc/fstab
2[root@lucifer /]# cat /etc/fstab
3
4#
5# /etc/fstab
6# Created by anaconda on Fri Aug 22 05:57:40 2025
7#
8# Accessible filesystems, by reference, are maintained under '/dev/disk'
9# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
10#
11/dev/mapper/centos-root / xfs defaults 0 0
12UUID=29b0e279-9b23-46ed-9271-31726f790177 /boot xfs defaults 0 0
13UUID=F141-4160 /boot/efi vfat umask=0077,shortname=winnt 0 0
14/dev/mapper/centos-swap swap swap defaults 0 0
可以看到已经没有 /home 的挂载信息了。
恢复数据
测试一下配置有没有问题:
1[root@lucifer /]# mount -a
没报错就是没问题。把备份的数据恢复回去:
1[root@lucifer /]# cd /home/
2[root@lucifer home]# ll
3total 0
4[root@lucifer home]# cp -a /root/home_backup/* /home/
5[root@lucifer home]# ll
6total 0
7drwx------. 2 jump jump 83 Aug 26 09:55 jump
8drwxr-xr-x. 2 root root 26 Aug 26 09:41 shell
数据回来了,权限也都在。
验证结果
看看现在的磁盘情况:
1[root@lucifer ~]# lsblk
2NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
3sda 8:0 0 1T 0 disk
4├─sda1 8:1 0 200M 0 part /boot/efi
5├─sda2 8:2 0 1G 0 part /boot
6└─sda3 8:3 0 1022.8G 0 part
7 ├─centos-root 253:0 0 1007.1G 0 lvm /
8 └─centos-swap 253:1 0 15.8G 0 lvm [SWAP]
9
10[root@lucifer ~]# df -h
11Filesystem Size Used Avail Use% Mounted on
12/dev/mapper/centos-root 1008G 1.8G 1006G 1% /
13/dev/sda2 1014M 142M 873M 14% /boot
14/dev/sda1 200M 12M 189M 6% /boot/efi
完美!根分区现在有 1T 空间了。
重启验证
最后重启一下,确保系统能正常启动:
1[root@lucifer home]# reboot
重启正常,修改成功!
总结
整个操作其实挺简单的,主要就是:备份数据 → 卸载分区 → 删除逻辑卷 → 扩展根分区 → 恢复数据 → 重启验证。
全程大概 10 分钟搞定,现在根分区有 1T 空间,再也不用担心空间不够了。最重要的是记得备份数据和修改 fstab,不然重启可能会出问题。
CentOS LVM 扩容实战:home 分区合并到根分区 - DBA 学习之路