CentOS环境下下调整home和根分区大小

Posted 星火燎原

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS环境下下调整home和根分区大小相关的知识,希望对你有一定的参考价值。

项目建设方给提供了3台CentOS的服务器,连接进去之后发现磁盘空间很大,但是都放在了home目录下,所以需要调整一下。

1、查看磁盘使用情况

[[email protected] ~]# df -h
Filesystem                     Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root   50G  9.9G   37G  22% /
tmpfs                          3.9G   72K  3.9G   1% /dev/shm
/dev/sda1                      485M   40M  421M   9% /boot
/dev/mapper/vg_centos-lv_home  439G  200M  417G   1% /home

 

2、卸载/home

[[email protected] ~]# umount /home
[[email protected] ~]# 
[[email protected] ~]# df -h
Filesystem                     Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root   50G   11G   37G  22% /
tmpfs                          3.9G   72K  3.9G   1% /dev/shm
/dev/sda1                      485M   40M  421M   9% /boot

 

3、调整分区大小

[[email protected] ~]# resize2fs -p /dev/mapper/vg_centos-lv_home 40G
resize2fs 1.41.12 (17-May-2010)
请先运行 e2fsck -f /dev/mapper/vg_centos-lv_home.
[[email protected] ~]# e2fsck -f /dev/mapper/vg_centos-lv_home
e2fsck 1.41.12 (17-May-2010)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/mapper/vg_centos-lv_home: 163/29212672 files (0.0% non-contiguous), 1884450/116819968 blocks

再次执行

[[email protected] ~]# resize2fs -p /dev/mapper/vg_centos-lv_home 40G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vg_centos-lv_home to 10485760 (4k) blocks.
Begin pass 2 (max = 32975)
正在重定位块            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 3566)
正在扫描inode表          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 4 (max = 84)
正在更新inode引用       XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/mapper/vg_centos-lv_home is now 10485760 blocks long.

 

4、挂载/home

[[email protected] ~]# mount /home

查看磁盘使用情况:

[[email protected] ~]# df -h
Filesystem                     Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root   50G   11G   37G  22% /
tmpfs                          3.9G   72K  3.9G   1% /dev/shm
/dev/sda1                      485M   40M  421M   9% /boot
/dev/mapper/vg_centos-lv_home   40G  178M   38G   1% /home

 

5、设置空闲空间

[[email protected] ~]# lvreduce -L 40G /dev/mapper/vg_centos-lv_home
  WARNING: Reducing active and open logical volume to 40.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_home? [y/n]: y
  Reducing logical volume lv_home to 40.00 GiB
  Logical volume lv_home successfully resized

使用lvreduce指令用于减少LVM逻辑卷占用的空间大小。可能会删除逻辑卷上已有的数据,所以在操作前必须进行确认。记得输入 “y”
[[email protected] ~]# lvreduce -L 20G /dev/mapper/VolGroup-lv_home
注:lvreduce -L 20G的意思为设置当前文件系统为20G,如果lvreduce -l 20G是指从当前文件系统上减少20G
使用lvreduce减小逻辑卷的大小。注意:减小后的大小不能小于文件的大小,否则会丢失数据。
 
可以使用vgdisplay命令等查看一下可以操作的大小。也可以是用fdisk -l命令查看详细信息。
[[email protected] ~]# vgdisplay
注:vgdisplay为显示LVM卷组的元数据信息。

 

6、将空闲空间挂载到根目录下

[[email protected] ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  5
  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       24032 / 93.88 GiB
  Free  PE / Size       103842 / 405.63 GiB
  VG UUID               ASX7Lb-Y0p6-YCGH-hNYp-ewdK-iFse-fq7uWw
   
[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# lvextend -L +400G /dev/mapper/vg_centos-lv_root
  Extending logical volume lv_root to 450.00 GiB
  Logical volume lv_root successfully resized
[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  6
  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       126432 / 493.88 GiB
  Free  PE / Size       1442 / 5.63 GiB
  VG UUID               ASX7Lb-Y0p6-YCGH-hNYp-ewdK-iFse-fq7uWw
   
[[email protected] ~]# df -h
Filesystem                     Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root   50G   11G   37G  22% /
tmpfs                          3.9G   72K  3.9G   1% /dev/shm
/dev/sda1                      485M   40M  421M   9% /boot
/dev/mapper/vg_centos-lv_home   40G  178M   38G   1% /home
[[email protected] ~]# 
[[email protected]CentOS ~]# 
[[email protected] ~]# resize2fs -p /dev/mapper/vg_centos-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_centos-lv_root is mounted on /; on-line resizing required
old desc_blocks = 4, new_desc_blocks = 29
Performing an on-line resize of /dev/mapper/vg_centos-lv_root to 117964800 (4k) blocks.
The filesystem on /dev/mapper/vg_centos-lv_root is now 117964800 blocks long.

[[email protected] ~]# df -h
Filesystem                     Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root  443G   11G  411G   3% /
tmpfs                          3.9G   72K  3.9G   1% /dev/shm
/dev/sda1                      485M   40M  421M   9% /boot
/dev/mapper/vg_centos-lv_home   40G  178M   38G   1% /home

以上是关于CentOS环境下下调整home和根分区大小的主要内容,如果未能解决你的问题,请参考以下文章

Centos/Linux下调整分区大小(以home和根分区为例)

调整home分区和根分区的大小

去繁化简解决CentOS下调整home和根分区大小的方法

XFS格式硬盘调整分区

centos7调整根目录挂载分区容量大小

Centos7调整root分区大小