在运行时扩展 rootfs 分区

Extend rootfs partition at runtime

我有一块带 16Go eMMC 闪存的嵌入式 Linux 开发板。

当我启动映像并且 运行 fdisk -l 我得到这个:

root@menzu:~# fdisk -l
Disk /dev/mmcblk2: 14.62 GiB, 15678308352 bytes, 30621696 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
Disklabel type: dos
Disk identifier: 0xe00e5569

Device         Boot  Start     End Sectors  Size Id Type
/dev/mmcblk2p1 *     16384  186775  170392 83.2M  c W95 FAT32 (LBA)
/dev/mmcblk2p2      196608 9177991 8981384  4.3G 83 Linux

如您所见,我的 eMMC /dev/mmcblk2 有 14.62 Gb 大小。

但是,我的Linuxrootfs分区只有4.3G,

如何在 运行 时将其大小扩展为 10Gb 或 12Gb?

我试过 resize2fs /dev/mmcblk2p2 但它把块大小更改为 1K,之后它只显示:

root@menzu:~# resize2fs /dev/mmcblk2p2
resize2fs 1.45.3 (14-Jul-2019)
The filesystem is already 1122673 (4k) blocks long.  Nothing to do!

我可以强制 Yocto 构建为 12Go,但这不是一个好的解决方案,因为图像会很大。

在使用 resize2fs 之前,您需要使用 fdisk 更改分区的大小:删除当前分区并创建一个与您删除的分区从同一块开始的新分区。

例如:

$ fdisk /dev/mmcblk1

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/mmcblk1: 27.86 GiB, 29896998912 bytes, 58392576 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
Disklabel type: dos
Disk identifier: 0x35a60061

Device         Boot  Start     End Sectors  Size Id Type
/dev/mmcblk1p1 *     16384  186775  170392 83.2M  c W95 FAT32 (LBA)
/dev/mmcblk1p2      196608 7071881 6875274  3.3G 83 Linux

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): p
Disk /dev/mmcblk1: 27.86 GiB, 29896998912 bytes, 58392576 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
Disklabel type: dos
Disk identifier: 0x35a60061

Device         Boot Start    End Sectors  Size Id Type
/dev/mmcblk1p1 *    16384 186775  170392 83.2M  c W95 FAT32 (LBA)

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2048-58392575, default 2048): 196608
Last sector, +/-sectors or +/-size{K,M,G,T,P} (196608-58392575, default 58392575): 

Created a new partition 2 of type 'Linux' and of size 27.8 GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): p

Disk /dev/mmcblk1: 27.86 GiB, 29896998912 bytes, 58392576 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
Disklabel type: dos
Disk identifier: 0x35a60061

Device         Boot  Start      End  Sectors  Size Id Type
/dev/mmcblk1p1 *     16384   186775   170392 83.2M  c W95 FAT32 (LBA)
/dev/mmcblk1p2      196608 58392575 58195968 27.8G 83 Linux

Command (m for help): w
The partition table has been altered.
Syncing disks.

之后您需要重新启动并 运行 resize2fs。