CentOS 7 划分 home 空闲空间到根目录

CentOS 安装的时候如果选择了自动分配,那么根目录自会分配 50G,其他的空间会分配给 Home 目录。这就导致我们有些业务无法存储到别的目录,空间会变得很紧张。那么如何划分 home 目录的空间到更目录?

备份 home 目录

因为我这里 home 目录是没有文件的,我们强烈建议在在此类操作过程备份文件。

创建备份文件夹

[root@nginx/]# mkdir /home-backup

移动 home 目录下所有文件到备份文件

[root@nginx /]# mv /home/* /home-backup/

卸载 /home 目录

[root@nginx ~]# umount /home

删除逻辑卷

[root@nginx ~]# lvremove /dev/centos/home

Do you really want to remove active logical volume centos/home? [y/n]: y
Logical volume "home" successfully removed

查看卷组可用空间

[root@nginx ~]# vgdisplay
 --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  7
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               98.80 GiB
  PE Size               4.00 MiB
  Total PE              25293
  Alloc PE / Size       14816 / <57.88 GiB
  Free  PE / Size       10477 / <40.93 GiB
  VG UUID               2efgG6-ImmJ-0Qxe-n9YV-P5Sy-JZGF-wRy3nj

Free PE / Size 中显示的空间为卷组的空闲空间 40.93 G

3.新建 home 卷

创建新的home卷,并分配空间。由于 home 目录是用户目录,如果删除是会报错的。

L表示大小,默认单位为M;n表示卷名;这里的centos是CentOS7安装系统的时候就默认建立好的卷组名。因为我的 home 没有放什么东西,我们就把刚刚的零头划分给 home 目录算了。

[root@nginx ~]# lvcreate -L 0.93G -n home centos

Rounding up size to full physical extent 956.00 MiB
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.

查看逻辑卷home

[root@nginx ~]# lvdisplay

再次查看卷组空间大小,可以看到可分配的还有39.99G

[root@nginx ~]# vgdisplay

--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 8
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 98.80 GiB
PE Size 4.00 MiB
Total PE 25293
Alloc PE / Size 15055 / <58.81 GiB
Free PE / Size 10238 / 39.99 GiB
VG UUID 2efgG6-ImmJ-0Qxe-n9YV-P5Sy-JZGF-wRy3nj

在新建的逻辑卷home上建立xfs文件系统

[root@nginx ~]# mkfs -t xfs /dev/centos/home

把这个新逻辑卷home挂到之前的文件夹/home中去,直接重启用fstab来挂载也行。

[root@nginx  ~]# mount /dev/centos/home /home
home 目录已经变更完成了

再把之前拷出来的东西拷回新的 /home中,不用重启。

[root@nginx ~]# mv /home-backup/* /home/

根目录扩容

把剩下的 39.9G现在分配给root卷

[root@nginx ~]# lvextend -L +39.9G /dev/centos/root

Rounding size to boundary between physical extents: 39.90 GiB.
Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 89.90 GiB (23015 extents).
Logical volume centos/root successfully resized.

查看逻辑卷情况

[root@nginx ~]# lvdisplay  

--- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                eiPqdo-4FCO-luVJ-K6dx-Sq1D-H9XJ-NLnxcL
  LV Write Access        read/write
  LV Creation host, time oa.juhui-tech.com, 2021-12-21 23:13:02 +0800
  LV Status              available
  # open                 2
  LV Size                <7.88 GiB
  Current LE             2016
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                jbc0hA-HzX0-nbmQ-pRRU-rQl6-NV96-k53SgL
  LV Write Access        read/write
  LV Creation host, time oa.juhui-tech.com, 2021-12-21 23:13:03 +0800
  LV Status              available
  # open                 1
  LV Size                89.90 GiB
  Current LE             23015
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/centos/home
  LV Name                home
  VG Name                centos
  LV UUID                DLcx0h-Hvgq-nUyy-6IUQ-LQKw-4JAq-5QzLAe
  LV Write Access        read/write
  LV Creation host, time nginx, 2022-11-25 00:08:04 +0800
  LV Status              available
  # open                 1
  LV Size                956.00 MiB
  Current LE             239
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

扩容 root 目录

[root@nginx ~]# xfs_growfs /dev/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 23567360

最好查看扩容后的情况

如果后续还需要扩容的话,可以参考 Centos 根目录空间扩容

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注