Linux磁盘分配 把home的空间扩容给root

1 背景

系统用来一段时间后,写入数据发现硬盘空间提示不足,当时感觉很奇怪,我的硬盘是500G,也没有什么大文件,为什么存在硬盘不足的情况呢。一查看硬盘使用情况,发现root分区只有50G,home有450G,现在知道为什么提示硬盘不足了,root分区既然只有50G,为什么root分区只有50G呢?后来在网上查了下才知道,再安装系统的时候,如果不进行操作硬盘分区,默认的情况下,root默认比较小,大部分分区会给home。知道原因了,现在只有想办法把home的空间扩容给root。

2 查看现在的分区情况

利用df -h 显示,centos-home 有442G,centos-root 只有50G

[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 7.8G     0  7.8G    0% /dev
tmpfs                    7.8G     0  7.8G    0% /dev/shm
tmpfs                    7.8G  5.8M  7.8G    1% /run
tmpfs                    7.8G     0  7.8G    0% /sys/fs/cgroup
/dev/mapper/centos-root   50G   26G   25G   51% /                      # root 的空间
/dev/sda1                497M  165M  332M   34% /boot
/dev/mapper/centos-home  442G   33M  442G    1% /home                  # home 的空间

利用vgdisplay 显示,没有空闲的空间

[root@localhost /]# 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                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <499.51 GiB
  PE Size               4.00 MiB
  Total PE              127874
  Alloc PE / Size       25056 / <97.88 GiB
  Free  PE / Size       0 / 0                                               # 没有空闲的空间
  VG UUID               CLUqlt-jF82-fC40-de7C-lHA6-78ff-khtCWW

3 从home分区转移出空间

3.1 备份home目录

# 切换到根下
cd /

# 打包home下所有文件
tar -czvf home.tgz home/

# 存放到tmp路径下
mv home.tgz /tmp

3.2 卸载home目录

# 杀掉home下的所有进程(不杀进程后边卸载会报错)如果提示fuser命令不存在,就安装fuser:yum -y install psmisc
fuser -km /home/ 

# 卸载home
umount /home

# 删除逻辑卷
lvremove /dev/centos/home

利用vgdisplay 显示,空闲空间401G

[root@localhost /]# 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                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <499.51 GiB
  PE Size               4.00 MiB
  Total PE              127874
  Alloc PE / Size       25056 / <97.88 GiB
  Free  PE / Size       102818 / 401.63 GiB                          # 空闲空间401G
  VG UUID               CLUqlt-jF82-fC40-de7C-lHA6-78ff-khtCWW

3.3 从新就home分区并且还原home

# 创建新的home卷,并分配空间。
# L表示大小,默认单位为M;n表示卷名;这里的centos是CentOS7安装系统的时候就默认建立好的卷组名。
[root@localhost /]# lvcreate -L 40G -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.

利用vgdisplay 显示,home分区为40G

[root@localhost /]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                nvewrv-rB8K-K1oW-jrWo-8aRU-Li2N-PNd0Uv
  LV Write Access        read/write
  LV Creation host, time localhost, 2022-11-03 15:22:30 +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                J3pQhK-l2WV-ps3V-AVs5-EZ2H-1Ebs-xcOCcS
  LV Write Access        read/write
  LV Creation host, time localhost, 2022-11-03 15:22:30 +0800
  LV Status              available
  # open                 1
  LV Size                50.00 GiB
  Current LE             12800
  Segments               1
  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                s1ApJs-Aaok-KkTb-klfk-Da7p-pVYA-Hy2fzD
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2022-12-02 21:21:16 +0800
  LV Status              available
  # open                 1
  LV Size                40.00 GiB
  Current LE             10240
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

再次查看闲置空间大小:vgdisplay 我这显示还有300多,准备都给到root

[root@localhost /]# 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                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <499.51 GiB
  PE Size               4.00 MiB
  Total PE              127874
  Alloc PE / Size       25056 / <97.88 GiB
  Free  PE / Size       82818 / 361.63 GiB
  VG UUID               CLUqlt-jF82-fC40-de7C-lHA6-78ff-khtCWW
# 在home上建立xfs文件系统
[root@localhost /]# mkfs -t xfs /dev/centos/home
meta-data=/dev/centos/home       isize=512    agcount=4, agsize=2621440 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=10485760, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=5120, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
# 挂载home
[root@localhost /]# mount /dev/centos/home /home
# 查看
[root@localhost /]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 7.8G     0  7.8G    0% /dev
tmpfs                    7.8G     0  7.8G    0% /dev/shm
tmpfs                    7.8G  5.8M  7.8G    1% /run
tmpfs                    7.8G     0  7.8G    0% /sys/fs/cgroup
/dev/mapper/centos-root   50G   26G   25G   51% /
/dev/sda1                497M  165M  332M   34% /boot
overlay                   50G   26G   25G   51% 
/dev/mapper/centos-home   40G   33M    0G    0% /home                  # home 的空间
# 解压之前备份的压缩包至home目录
[root@localhost /]# tar -xzvf /tmp/home.tgz -C /home
# 这时会有两层home目录,把里边的内容移动出来:
[root@localhost /]# mv /home/home/* /home/
# 然后删除内部的home目录:
[root@localhost /]# rm -rf /home/home/

4 扩容root

4.1 分配空间给root

# 分配400G空间给root(根据自己的空闲空间决定)
[root@localhost /]# lvextend -L +400G /dev/centos/root
  Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 450.00 GiB (115200 extents).
  Logical volume centos/root successfully resized.

再次查看闲置空间大小:lvdisplay,root的空间为450G

[root@localhost /]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                nvewrv-rB8K-K1oW-jrWo-8aRU-Li2N-PNd0Uv
  LV Write Access        read/write
  LV Creation host, time localhost, 2022-11-03 15:22:30 +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                J3pQhK-l2WV-ps3V-AVs5-EZ2H-1Ebs-xcOCcS
  LV Write Access        read/write
  LV Creation host, time localhost, 2022-11-03 15:22:30 +0800
  LV Status              available
  # open                 1
  LV Size                450.00 GiB
  Current LE             115200
  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                s1ApJs-Aaok-KkTb-klfk-Da7p-pVYA-Hy2fzD
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2022-12-02 21:21:16 +0800
  LV Status              available
  # open                 1
  LV Size                40.00 GiB
  Current LE             10240
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   

4.2 扩展root 卷XFS 文件系统的大小

# 扩展root 卷XFS 文件系统的大小
[root@localhost /]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=3276800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        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=0
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 117964800

4.3 检查分配结果,大功告成

[root@localhost /]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 7.8G     0  7.8G    0% /dev
tmpfs                    7.8G     0  7.8G    0% /dev/shm
tmpfs                    7.8G  5.8M  7.8G    1% /run
tmpfs                    7.8G     0  7.8G    0% /sys/fs/cgroup
/dev/mapper/centos-root  450G   26G  425G    6% /
/dev/sda1                497M  165M  332M   34% /boot
overlay                  450G   26G  425G    6% 
/dev/mapper/centos-home   40G   33M   40G    1% /home

你可能感兴趣的:(Linux,linux,运维,5G)