参考链接: https://blog.csdn.net/weixin_44917349/article/details/118768845
由于安装 CentOS 系统时使用的默认分区配置,导致
/
目录只有 50G,而/home
根目录则有 392G。
分配严重不合理,所以需要减少/home
空间分配给/
根目录。
-
首先需要压缩备份
/home
至/root/
或其他目录下[root@node159 ~]$ tar -czvf /root/home.tgz -C /home
-
查看当前磁盘分区情况
[root@node159 ~]$ df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 50G 5.5G 45G 11% / devtmpfs 7.3G 0 7.3G 0% /dev tmpfs 7.3G 0 7.3G 0% /dev/shm tmpfs 7.3G 27M 7.3G 1% /run tmpfs 7.3G 0 7.3G 0% /sys/fs/cgroup /dev/sda1 1014M 143M 872M 15% /boot /dev/mapper/centos-home 392G 33M 392G 1% /home
-
卸载
/home
分区[root@node159 ~]$ umount /home umount: /home: target is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1))
若提示占用则强制卸载
[root@node159 ~]$ umount -lf /home
-
减少
/home
分区 200G 空间[root@node159 ~]$ lvreduce -L -200G /dev/mapper/centos-home WARNING: Reducing active and open logical volume to <191.62 GiB. 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 <391.62 GiB (100254 extents) to <191.62 GiB (49054 extents). Logical volume centos/home successfully resized.
-
增加
/
分区 200G 空间[root@node159 ~]$ lvextend -L +200G /dev/mapper/centos-root Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 250.00 GiB (64000 extents). Logical volume centos/root successfully resized.
-
执行扩容操作
[root@node159 ~]$ 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 65536000
-
挂载
/home
分区(错误步骤,仅做记录,不执行)
此时挂载分区,提示未知文件系统,使用mkfs
命令进行格式化分区系统时提示占用
根据网上资料,需要首先使用lvremove
命令删除卷,但是删除卷依旧提示占用[root@node159 ~]$ mount -t /dev/mapper/centos-home /home mount: unknown filesystem type '/dev/mapper/centos-home' [root@node159 ~]$ mkfs -t xfs /dev/mapper/centos-home mkfs.xfs: cannot open /dev/mapper/centos-home: Device or resource busy [root@node159 ~]$ lvremove /dev/mapper/centos-home Logical volume centos/home contains a filesystem in use. [root@node159 ~]$ lvremove -f /dev/mapper/centos-home Logical volume centos/home contains a filesystem in use.
-
查资料得知,需要使用
fuser
命令解除占用,但提示没有该命令[root@node159 ~]$ fuser -kuc /dev/mapper/centos-home -bash: fuser: command not found
-
需要安装
psmisc
才可以使用fuser
命令,但是在线安装死活不成功[root@node159 ~]$ yum install -y psmisc Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.163.com * updates: mirrors.huaweicloud.com Resolving Dependencies --> Running transaction check ---> Package psmisc.x86_64 0:22.20-17.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================================================================================================================================================================== Package Arch Version Repository Size ================================================================================================================================================================================================================================================== Installing: psmisc x86_64 22.20-17.el7 base 141 k Transaction Summary ================================================================================================================================================================================================================================================== Install 1 Package Total download size: 141 k Installed size: 475 k Downloading packages: psmisc-22.20-17.el7.x86_64.rpm | 141 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction psmisc-22.20-17.el7.x86_64 was supposed to be installed but is not! Verifying : psmisc-22.20-17.el7.x86_64 1/1 Verifying : psmisc-22.20-17.el7.x86_64 2/1 Failed: psmisc.x86_64 0:22.20-17.el7 Complete!
-
只好下载安装包手动安装 下载链接
[root@node159 ~]$ yum localinstall psmisc-22.20-17.el7.x86_64.rpm Loaded plugins: fastestmirror Examining psmisc-22.20-17.el7.x86_64.rpm: psmisc-22.20-17.el7.x86_64 Marking psmisc-22.20-17.el7.x86_64.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package psmisc.x86_64 0:22.20-17.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================================================================================================================================================================== Package Arch Version Repository Size ================================================================================================================================================================================================================================================== Installing: psmisc x86_64 22.20-17.el7 /psmisc-22.20-17.el7.x86_64 475 k Transaction Summary ================================================================================================================================================================================================================================================== Install 1 Package Total size: 475 k Installed size: 475 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : psmisc-22.20-17.el7.x86_64 1/1 Verifying : psmisc-22.20-17.el7.x86_64 1/1 Installed: psmisc.x86_64 0:22.20-17.el7 Complete!
-
-
继续正常步骤,使用
fuser
解除占用并删除卷[root@node159 ~]$ fuser -kuc /dev/mapper/centos-home [root@node159 ~]$ lvremove -f /dev/mapper/centos-home Logical volume "home" successfully removed
-
重新创建 home 卷,分配大小为 190G
[root@node159 ~]$ lvcreate -L 190GB -n home centos WARNING: xfs signature detected on /dev/centos/home at offset 0. Wipe it? [y/n]: y Wiping xfs signature on /dev/centos/home. Logical volume "home" created.
-
格式化新分区
[root@node159 ~]$ mkfs.xfs /dev/centos/home meta-data=/dev/centos/home isize=512 agcount=4, agsize=12451840 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=49807360, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=24320, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
-
再次挂载到
/home
下,查看分区是否成功[root@node159 ~]$ mount /dev/mapper/centos-home [root@node159 ~]$ df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 250G 5.4G 245G 3% / devtmpfs 7.3G 0 7.3G 0% /dev tmpfs 7.3G 0 7.3G 0% /dev/shm tmpfs 7.3G 27M 7.3G 1% /run tmpfs 7.3G 0 7.3G 0% /sys/fs/cgroup /dev/sda1 1014M 143M 872M 15% /boot tmpfs 1.5G 0 1.5G 0% /run/user/0 /dev/mapper/centos-home 190G 33M 190G 1% /home
-
将剩余没分配完的空间全部分给根目录
/
[root@node159 ~]$ lvextend -r -l +100%FREE /dev/mapper/centos-root Size of logical volume centos/root changed from 250.00 GiB (64000 extents) to 251.62 GiB (64415 extents). Logical volume centos/root successfully resized. meta-data=/dev/mapper/centos-root isize=512 agcount=20, agsize=3276800 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=65536000, 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 65536000 to 65960960
-
恢复
/home
备份[root@node159 ~]$ tar -xzvf /root/home.tgz -C /home
以上就是一次完整的扩缩容分区过程,其中一些命令及参数并不明白,需要查资料学习了解。