LVM增大缩小分区,复现减小分区报错并修复

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVM增大缩小分区,复现减小分区报错并修复相关的知识,希望对你有一定的参考价值。

LVM的安装和LVM的在线增大比较简单,不记录,LVM的分区离线缩小坑较多

要缩小LVM设备为/dev/mapper/test-lv_test 的大小为200M

第一步:卸载设备(减小文件系统时需要卸载,文件系统和resize工具都不支持在线减小。)
umount /dev/mapper/test-lv_test
第二步:执行文件系统检查(先检查一下分区的完整性。)
fsck -f /dev/mapper/test-lv_test
第三步:调整文件系统大小(将文件系统减小到200M)
resize2fs /dev/mapper/test-lv_test 200M
第四步:缩小分区
lvreduce -L 200M /dev/mapper/test-lv_test 将分区缩小到200M
PS:参数:-200M 缩小了200M,参数:200M 缩小到200M

[root@master ~]# mkfs.ext3 /dev/mapper/test-lv_test
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
128016 inodes, 512000 blocks
25600 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=67633152
63 block groups
8192 blocks per group, 8192 fragments per group
2032 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

正在写入inode表: 完成
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@master ~]# mount /dev/mapper/test-lv_test /aas/

一.lvm在线增大
[root@master aas]# lvextend -L +200M /dev/mapper/test-lv_test
Extending logical volume lv_test to 700.00 MiB
Logical volume lv_test successfully resized
[root@master aas]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 6.2G 11G 38% /
tmpfs 940M 0 940M 0% /dev/shm
/dev/sda1 485M 40M 421M 9% /boot
/dev/mapper/oracle-lv_oracle 21G 8.5G 12G 44% /oracle
/dev/mapper/test-lv_test 485M 11M 449M 3% /aas
调整文件系统大小
[root@master aas]# resize2fs /dev/mapper/test-lv_test
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/test-lv_test is mounted on /aas; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/mapper/test-lv_test to 716800 (1k) blocks.
The filesystem on /dev/mapper/test-lv_test is now 716800 blocks long.

[root@master aas]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 6.2G 11G 38% /
tmpfs 940M 0 940M 0% /dev/shm
/dev/sda1 485M 40M 421M 9% /boot
/dev/mapper/oracle-lv_oracle 21G 8.5G 12G 44% /oracle
/dev/mapper/test-lv_test 678M 11M 633M 2% /aas

二.LVM离线减少分区大小的步骤
步骤严谨中间漏掉会报错:坏块,比较难修复,需要将大小恢复后再格式化
[root@master /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 6.2G 11G 38% /
tmpfs 940M 0 940M 0% /dev/shm
/dev/sda1 485M 40M 421M 9% /boot
/dev/mapper/oracle-lv_oracle 21G 8.5G 12G 44% /oracle
/dev/mapper/test-lv_test 678M 11M 633M 2% /aas
[root@master /]# umount /dev/mapper/test-lv_test
[root@master /]#
[root@master /]# fsck -f /dev/mapper/test-lv_test
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/mapper/test-lv_test: 12/178816 files (0.0% non-contiguous), 33358/716800 blocks
[root@master /]#
[root@master /]# fsck -f /dev/mapper/test-lv_test
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/mapper/test-lv_test: 12/178816 files (0.0% non-contiguous), 33358/716800 blocks
[root@master /]# resize2fs /dev/mapper/test-lv_test 200M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/test-lv_test to 204800 (1k) blocks.
The filesystem on /dev/mapper/test-lv_test is now 204800 blocks long.

[root@master /]# lvreduce -L 200M /dev/mapper/test-lv_test
WARNING: Reducing active logical volume to 200.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_test? [y/n]: y
Reducing logical volume lv_test to 200.00 MiB
Logical volume lv_test successfully resized
[root@master /]# mount /dev/mapper/test-lv_test /aas/
[root@master /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 6.2G 11G 38% /
tmpfs 940M 0 940M 0% /dev/shm
/dev/sda1 485M 40M 421M 9% /boot
/dev/mapper/oracle-lv_oracle 21G 8.5G 12G 44% /oracle
/dev/mapper/test-lv_test 194M 9.6M 175M 6% /aas

错误案例:
不按照步骤进行,看似成功减小实际并没有,因为没有写入文件系统4
resize2fs该命令执行报错,则未写入到文件系统中

[root@master /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 6.2G 11G 38% /
tmpfs 940M 0 940M 0% /dev/shm
/dev/sda1 485M 40M 421M 9% /boot
/dev/mapper/oracle-lv_oracle 21G 8.5G 12G 44% /oracle
/dev/mapper/test-lv_test 1.2G 11M 1.1G 1% /aas
[root@master /]# umount /aas/
[root@master /]# lvreduce -L -500M /dev/mapper/test-lv_test
WARNING: Reducing active logical volume to 724.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_test? [y/n]: y
Reducing logical volume lv_test to 724.00 MiB
Logical volume lv_test successfully resized

至此缩小分区出错,导致文件系统出现坏块,需要执行文件系统检查
[root@master /]# resize2fs /dev/mapper/test-lv_test
resize2fs 1.41.12 (17-May-2010)
请先运行 ‘e2fsck -f /dev/mapper/test-lv_test‘.

这里检查出来节点不对,但是修复不了,需要后面自己增大空间
[root@master /]# e2fsck -f /dev/mapper/test-lv_test
e2fsck 1.41.12 (17-May-2010)
The 文件系统 size (according to the 超级块) is 1253376 块s
The physical size of the 设备 is 741376 块s
Either the 超级块 or the partition table is likely to be corrupt!
中断<y>? 否

第一步: 检查inode,块,和大小
Error reading block 745475 (无效的参数) while getting next inode from scan. 忽略错误<y>? 是
强制覆盖<y>? 是
Error writing block 745475 (无效的参数) while getting next inode from scan. 忽略错误<y>? 是
Error reading block 745476 (无效的参数) while getting next inode from scan. 忽略错误<y>? 是
强制覆盖<y>? 否
Error reading block 745478 (无效的参数) while getting next inode from scan. 忽略错误<y>? 否
Error while scanning inodes (184912): Can‘t read next inode
e2fsck: 已中止
[root@master ~]#

修复的方法:

1.挂载,查看大小并没有减小
[root@master ~]# mount /dev/mapper/test-lv_test /aas/
[root@master ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 6.2G 11G 38% /
tmpfs 940M 0 940M 0% /dev/shm
/dev/sda1 485M 40M 421M 9% /boot
/dev/mapper/oracle-lv_oracle 21G 8.5G 12G 44% /oracle
/dev/mapper/test-lv_test 1.2G 11M 1.1G 1% /aas
2.按照错误步骤减小了多少,就增加回来
[root@master ~]# lvextend -L +500M /dev/mapper/test-lv_test
Extending logical volume lv_test to 1.20 GiB
Logical volume lv_test successfully resized
[root@master ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 6.2G 11G 38% /
tmpfs 940M 0 940M 0% /dev/shm
/dev/sda1 485M 40M 421M 9% /boot
/dev/mapper/oracle-lv_oracle 21G 8.5G 12G 44% /oracle
/dev/mapper/test-lv_test 1.2G 11M 1.1G 1% /aas
提示并没有修改文件系统,原大小没有更改
[root@master ~]# resize2fs /dev/mapper/test-lv_test
resize2fs 1.41.12 (17-May-2010)
The filesystem is already 1253376 blocks long. Nothing to do!

按照正常缩小分区的步骤再来一遍即可
[root@master ~]# umount /dev/mapper/test-lv_test
[root@master ~]# fsck -f /dev/mapper/test-lv_test
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/mapper/test-lv_test: 12/310896 files (0.0% non-contiguous), 50246/1253376 blocks
[root@master ~]# resize2fs /dev/mapper/test-lv_test -500M
resize2fs 1.41.12 (17-May-2010)
resize2fs:无效选项 -- 5
Usage: resize2fs [-d debug_flags] [-f] [-F] [-M] [-P] [-p] device [new_size]

[root@master ~]# resize2fs /dev/mapper/test-lv_test 500M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/test-lv_test to 512000 (1k) blocks.
The filesystem on /dev/mapper/test-lv_test is now 512000 blocks long.

[root@master ~]# lvreduce -L -500M /dev/mapper/test-lv_test
WARNING: Reducing active logical volume to 724.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_test? [y/n]: y
Reducing logical volume lv_test to 724.00 MiB
Logical volume lv_test successfully resized
[root@master ~]# mount /dev/mapper/test-lv_test /aas/
[root@master ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 6.2G 11G 38% /
tmpfs 940M 0 940M 0% /dev/shm
/dev/sda1 485M 40M 421M 9% /boot
/dev/mapper/oracle-lv_oracle 21G 8.5G 12G 44% /oracle
/dev/mapper/test-lv_test 485M 11M 450M 3% /aas

以上是关于LVM增大缩小分区,复现减小分区报错并修复的主要内容,如果未能解决你的问题,请参考以下文章

Linux(RadHat)基础学习—系统分区逻辑卷lvm

lvm的实现

LVM缩小根分区

LVM的基本使用

LVM根分区缩容

linux--LVM分区和磁盘配额!