当前方案只适用于Ext4格式ROM

事件背景

要安装的软件越来越多,很多OpenWrt固件的预留空间并不多。例如我常用的ImmortalWrt整个镜像只有300MiB大小。可用于安装的容量就捉襟见肘了。扩容分区也就成了非常重要的需求。

我的家庭网络环境是:PVE+OpenWrt+iKuai+NAS。iKuai作为主路由用于拨号上网以及动态域名更新等操作,OpenWrt作为旁路由为我提供魔法服务。

操作步骤

PVE操作扩容磁盘

PVE下的OpenWrt虚机会被转换为qcow2格式的磁盘,此时我们可以借助PVE后台界面的磁盘操作菜单里的调整大小功能为虚拟磁盘进行扩容。如下图所示:

image-20240822004807204

OpenWrt扩容分区

磁盘扩容完后,登录OpenWrt的终端进行分区创建等操作。命令如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
root@ImmortalWrt:~# fdisk -l
Disk /dev/sda: 3.33 GiB, 3573547008 bytes, 6979584 sectors
Disk model: QEMU HARDDISK
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: 0x20c60f33

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 512 66047 65536 32M 83 Linux
/dev/sda2 66560 680959 614400 300M 83 Linux
root@ImmortalWrt:~# fdisk /dev/sda

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

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


Command (m for help): p

Disk /dev/sda: 3.33 GiB, 3573547008 bytes, 6979584 sectors
Disk model: QEMU HARDDISK
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: 0x20c60f33

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 512 66047 65536 32M 83 Linux
/dev/sda2 66560 680959 614400 300M 83 Linux

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

Created a new partition 3 of type 'Linux' and of size 2.5 GiB.

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

截图如下:

image-20240822001639964

查看一下是否已经新建一个新分区,并格式化新分区:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
root@ImmortalWrt:~# fdisk -l
Disk /dev/sda: 3.33 GiB, 3573547008 bytes, 6979584 sectors
Disk model: QEMU HARDDISK
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: 0x20c60f33

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 512 66047 65536 32M 83 Linux
/dev/sda2 66560 680959 614400 300M 83 Linux
/dev/sda3 681000 5924863 5243864 2.5G 83 Linux

root@ImmortalWrt:~# mkfs.ext4 /dev/sda3
mke2fs 1.47.0 (5-Feb-2023)
Discarding device blocks: done
Creating filesystem with 655360 4k blocks and 164160 inodes
Filesystem UUID: 67bab07a-3319-426c-85c3-15c568dece01
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912

Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

OpenWrt挂载点挂载

创建好分区后,登录后台,进入挂载点页面,新增挂载点,如图所示:

image-20240822003156048

复制给出的命令列表,并修改部分:

1
2
3
4
5
6
7
mkdir -p /tmp/introot
mkdir -p /tmp/extroot
mount --bind / /tmp/introot
mount /dev/sda3 /tmp/extroot
tar -C /tmp/introot -cvf - . | tar -C /tmp/extroot -xf -
umount /tmp/introot
umount /tmp/extroot

在OpenWrt命令行执行并重启后,整个过程就结束了。效果如下图所示:

image-20240822003958339