RHEL / CentOS 系统减小 LVM 分区大小

有时,当我们在 linux 系统中耗尽磁盘空间时,如果分区是在 LVM 上创建的,那么我们可以通过使用 lvreduce 命令缩小 LVM 在卷组中腾出一些空闲空间。

在本指南中,我们将学习如何在 RHEL 和 CentOS 系统中减少或缩小 LVM 分区大小。

注意: 这些步骤仅适用于使用基于 LVM 的分区并格式化为 ext4 或 ext3 的情况。

假设我们想减少 /home 2GB(LVM 分区,EXT4 格式)

[root@cloud ~]# df -h /home/
 Filesystem Size Used Avail Use% Mounted on
 /dev/mapper/vg_cloud-LogVol00
 12G 9.2G 1.9G 84% /home

(1) 卸载文件系统

使用下面的 umount 命令卸载文件系统

[root@cloud ~]# umount /home/

(2) 检查文件系统是否有错误

执行 e2fsck 命令检查文件系统是否存在错误。当使用 -f 标志时,即使文件系统已经被清理,它也会强制检查或扫描文件系统。

[root@cloud ~]# e2fsck -f /dev/mapper/vg_cloud-LogVol00
 e2fsck 1.41.12 (17-May-2010)
 Pass 1: Checking inodes, blocks, and sizes
 Pass 2: Checking directory structure
 Pass 3: Checking directory connectivity
 Pass 4: Checking reference counts
 Pass 5: Checking group summary information
 /dev/mapper/vg_cloud-LogVol00: 12/770640 files (0.0% non-contiguous), 2446686/3084288 blocks

(3) 缩小 /home 到指定尺寸

As shown in the above scenario, size of /home is 12 GB , so by reducing it by 2GB , then the size will become 10GB. Run the following resize2fs command followed by the file system and size.

/home 大小为12GB,通过将其减少2GB,然后大小将变为10GB。运行以下 resize2fs 命令

[root@cloud ~]# resize2fs /dev/mapper/vg_cloud-LogVol00 10G
 resize2fs 1.41.12 (17-May-2010)
 Resizing the filesystem on /dev/mapper/vg_cloud-LogVol00 to 2621440 (4k) blocks.
 The filesystem on /dev/mapper/vg_cloud-LogVol00 is now 2621440 blocks long.

(4) 使用 lvreduce 命令降低 LVM 尺寸

执行以下 lvreduce 命令

[root@cloud ~]# lvreduce -L 10G /dev/mapper/vg_cloud-LogVol00
 WARNING: Reducing active logical volume to 10.00 GiB
 THIS MAY DESTROY YOUR DATA (filesystem etc.)
 Do you really want to reduce LogVol00? [y/n]: y
 Reducing logical volume LogVol00 to 10.00 GiB
 Logical volume LogVol00 successfully resized

(5) 检查文件系统中的错误 (可选)

再次运行 e2fsck 命令,检查文件系统在减小大小后是否有错误。

[root@cloud ~]# e2fsck -f /dev/mapper/vg_cloud-LogVol00
 e2fsck 1.41.12 (17-May-2010)
 Pass 1: Checking inodes, blocks, and sizes
 Pass 2: Checking directory structure
 Pass 3: Checking directory connectivity
 Pass 4: Checking reference counts
 Pass 5: Checking group summary information
 /dev/mapper/vg_cloud-LogVol00: 12/648960 files (0.0% non-contiguous), 2438425/2621440 blocks

(6) 挂载文件系统并验证其大小

[root@cloud ~]# mount /home/
 [root@cloud ~]# df -h /home/
 Filesystem Size Used Avail Use% Mounted on
 /dev/mapper/vg_cloud-LogVol00
 9.9G 9.2G 208M 98% /home

完美,上面的命令输出证实 /home 尺寸缩小2GB,现在新的文件系统大小为 10GB。

我的开源项目

作者:鸠摩智首席音效师原文地址:https://segmentfault.com/a/1190000043472468

%s 个评论

要回复文章请先登录注册