我该如何更好地计算缩小 LV 和文件系统的目标大小?

我该如何更好地计算缩小 LV 和文件系统的目标大小?

我有一个带有 ext4 文件系统的 LV。

lubuntu@lubuntu:~$ sudo parted -l
Model: Linux device-mapper (linear) (dm)
Disk /dev/mapper/lubuntu--vg-root: 499GB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Disk Flags: 

Number  Start  End    Size   File system  Flags
 1      0.00B  499GB  499GB  ext4

我想将 LV 缩小到其大小的 1/10。我非常不擅长估计文件系统和 LV 的目标大小,如下面的过程所示,不知道您是否可以提供一些建议来帮助我将来做得更好?

(1)我根据块数找出LV的大小,并计算目标大小

lubuntu@lubuntu:~$ sudo e2fsck -f /dev/mapper/lubuntu--vg-root
/dev/mapper/lubuntu--vg-root: 169997/30433280 files (0.2% non-contiguous), 3088725/121713664 blocks

lubuntu@lubuntu:~$ echo "121713664/10"  | bc
12171366

(2)我缩小了LV上文件系统的大小

lubuntu@lubuntu:~$ sudo resize2fs /dev/mapper/lubuntu--vg-root 12171366
resize2fs 1.44.1 (24-Mar-2018)
Resizing the filesystem on /dev/mapper/lubuntu--vg-root to 12171366 (4k) blocks.
The filesystem on /dev/mapper/lubuntu--vg-root is now 12171366 (4k) blocks long.

验证 fs 的大小已更改

ubuntu@lubuntu:~$ mkdir /tmp/root
lubuntu@lubuntu:~$ sudo mount /dev/mapper/lubuntu--vg-root /tmp/root
lubuntu@lubuntu:~$ df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/lubuntu--vg-root   45G  3.5G   39G   9% /tmp/root
lubuntu@lubuntu:~$ sudo umount /tmp/root

(3) 缩小了LV的尺寸。我不确定要缩小到什么大小,所以我给出了一个比df -h.

lubuntu@lubuntu:~$ sudo lvreduce -L46G /dev/mapper/lubuntu--vg-root
  WARNING: Reducing active logical volume to 46.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lubuntu-vg/root? [y/n]: y
  Size of logical volume lubuntu-vg/root changed from 464.30 GiB (118861 extents) to 46.00 GiB (11776 extents).
  Logical volume lubuntu-vg/root successfully resized.


lubuntu@lubuntu:~$ sudo fdisk -l

Disk /dev/mapper/lubuntu--vg-root: 46 GiB, 49392123904 bytes, 96468992 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

(4) 试图将fs“扩展”到LV,但为什么我不能?

lubuntu@lubuntu:~$ sudo resize2fs /dev/mapper/lubuntu--vg-root
resize2fs 1.44.1 (24-Mar-2018)
Please run 'e2fsck -f /dev/mapper/lubuntu--vg-root' first.

lubuntu@lubuntu:~$ sudo e2fsck -f /dev/mapper/lubuntu--vg-root
e2fsck 1.44.1 (24-Mar-2018)
The filesystem size (according to the superblock) is 12171366 blocks
The physical size of the device is 12058624 blocks
Either the superblock or the partition table is likely to be corrupt!
Abort<y>? yes

(5) 再次尝试将 LV“扩展”为大于 fs,但为什么我不能?

lubuntu@lubuntu:~$ sudo lvextend -l 12171366 /dev/mapper/lubuntu--vg-root
  Insufficient free space: 12159590 extents needed, but only 107085 available

(6) 第三次尝试将 LV“扩展”到更大的大小,幸运的是我的猜测大于文件系统的大小。但这只是一个大胆的猜测,我怎样才能得出足够大的目标大小来容纳文件系统呢?

lubuntu@lubuntu:~$ sudo lvextend -L 48G /dev/mapper/lubuntu--vg-root
  Size of logical volume lubuntu-vg/root changed from 46.00 GiB (11776 extents) to 48.00 GiB (12288 extents).
  Logical volume lubuntu-vg/root successfully resized.

下面的“Inode 30571 范围块通过检查,但校验和与范围不匹配”是什么意思?

lubuntu@lubuntu:~$ sudo e2fsck -f /dev/mapper/lubuntu--vg-root
e2fsck 1.44.1 (24-Mar-2018)
Pass 1: Checking inodes, blocks, and sizes
Inode 30571 extent block passes checks, but checksum does not match extent
    (logical block 303, physical block 54575, len 1)
Fix<y>? yes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/lubuntu--vg-root: 169997/3047424 files (0.6% non-contiguous), 1364383/12171366 blocks

现在我可以将 fs 扩展到 LV

lubuntu@lubuntu:~$ sudo resize2fs /dev/mapper/lubuntu--vg-root
resize2fs 1.44.1 (24-Mar-2018)
Resizing the filesystem on /dev/mapper/lubuntu--vg-root to 12582912 (4k) blocks.
The filesystem on /dev/mapper/lubuntu--vg-root is now 12582912 (4k) blocks long.

谢谢。

答案1

直观的选择是使用-r, --resizefs提供的选项lvresize:

-r,--resizefs

使用以下命令调整底层文件系统和逻辑卷的大小FSADM(8)

下面显示的测试输出来自我的 CentOS 7 系统,但原理对于 Ubuntu 也应该是相同的。

我们从 10GiB LV 开始:

[root@testvm1 ~]# lvs testvg/testlv
  LV     VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  testlv testvg -wi-a----- <10.00g

现在我们调整 LV 的大小,指定 2GiB 的目标大小以及-r调整文件系统大小的标志:

[root@testvm1 ~]# lvresize -r -L 2G /dev/mapper/testvg-testlv
fsck from util-linux 2.23.2
/dev/mapper/testvg-testlv: clean, 12/655360 files, 83130/2620416 blocks
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/testvg-testlv to 524288 (4k) blocks.
The filesystem on /dev/mapper/testvg-testlv is now 524288 blocks long.

  Size of logical volume testvg/testlv changed from <10.00 GiB (2559 extents) to 2.00 GiB (512 extents).
  Logical volume testvg/testlv successfully resized.

验证转换是否按预期进行:

[root@testvm1 ~]# lvs testvg/testlv
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  testlv testvg -wi-a----- 2.00g

如果您坚持分开执行这两个步骤,那么最好还是使用大小而不是计算范围和块的数量。

首先,我们通过指定目标大小来调整文件系统的大小:

[root@testvm1 ~]# resize2fs /dev/mapper/testvg-testlv 2G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/testvg-testlv to 524288 (4k) blocks.
The filesystem on /dev/mapper/testvg-testlv is now 524288 blocks long.

然后我们通过指定相同的大小来调整 LV 的大小:

[root@testvm1 ~]# lvresize -L 2G /dev/mapper/testvg-testlv
  WARNING: Reducing active logical volume to 2.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce testvg/testlv? [y/n]: y
  Size of logical volume testvg/testlv changed from <10.00 GiB (2559 extents) to 2.00 GiB (512 extents).
  Logical volume testvg/testlv successfully resized.

希望这能解决您关于如何一起调整文件系统和 LV 大小的问题的核心。


(4) 中的错误很简单。您调整大小的工作导致分区(12058624 块)小于文件系统(12171366 块)。这是不合逻辑的,因此e2fsck警告您文件系统或分区表已损坏。

(5) 正在发生,因为您指定了-l 12171366。您在这里混淆了块和范围。块是文件系统使用的概念,而盘区是 LVM 使用的概念。文件系统块代表文件系统的最小工作单元。 LVM 盘区是使用 LVM 时可以分配的最小空间单位。

为了更好地说明这一点,以下是我的 ~10 GiB 分区和 ~10 GiB 文件系统的示例:

block count (2620416) * block size (4K) = size of filesystem (10236M)
number of extents (2559) * extent size (4M) = size of volume (10236M)

可以使用 来检索 ext4 文件系统的块计数和块大小tune2fs -l。可以使用pvdisplay或重试范围大小和计数pvs

相关内容