OpenWrt官方squashfs固件安装时扩容方法

OpenWrt固件的squashfs分区会在首次启动时自动扩展到所分配的全部空间,所以我们只需要使用fdisk简单地更改squashfs分区的大小,然后重启开始自动安装即可,不需要重新编译或使用固件生成器重新生成。

注意:
此方法仅适合刚把镜像写入磁盘还未启动以及还未将镜像写入磁盘这两种情况,一旦系统启动,squashfs分区大小就已经确定了,如果要更改只能使用losetup挂载并执行resize.f2fs扩展大小。


2024-07-15更新:
  以下步骤适合非efi启动的固件,如果你是efi固件,那么需要在删除分区前使用i显示并记下要删除分区的uuid,并且在新分区创建完成后使用x进入fdisk的高级模式,使用u修改分区的uuid为前面你记下的分区。完成后使用r返回,w保存退出(在文章最后有增加示例)。或按评论区的方法,修改grub文件内的uuid,让它能找到修改后的分区。


详细步骤如下:

  1. 如果要修改的是IMG文件,需要先使用dd命令扩展文件的大小,如果IMG已经写入了磁盘则直接从第2步开始

    izilzty@debian-vm:~$ dd if=/dev/zero bs=1M count=1024 >> openwrt-21.02.0-x86-64-generic-squashfs-combined.img
    记录了1024+0 的读入
    记录了1024+0 的写出
    1073741824 bytes (1.1 GB, 1.0 GiB) copied, 1.92703 s, 557 MB/s
    

其中count=1024为要增加的大小,单位为MB

  1. 输入命令sudo fdisk openwrt-21.02.0-x86-64-generic-squashfs-combined.img并按回车进入分区状态,如果IMG已经写入了磁盘则将文件路径改为磁盘路径即可,例如sudo fdisk /dev/sda

注意:要在linux平台上执行,mac-intel不行

1
2
3
4
5
6
7
izilzty@debian-vm:~$ sudo fdisk openwrt-21.02.0-x86-64-generic-squashfs-combined.img

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

Command (m for help):

3. 输入p并按回车,显示当前分区表

1
2
3
4
5
6
7
8
9
10
11
12
13
Command (m for help): p
Disk openwrt-21.02.0-x86-64-generic-squashfs-combined.img: 1 GiB, 1091043328 bytes, 2130944 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: 0xd0f061a8

Device Boot Start End Sectors Size Id Type
openwrt-21.02.0-x86-64-generic-squashfs-combined.img1 * 512 33279 32768 16M 83 Linux
openwrt-21.02.0-x86-64-generic-squashfs-combined.img2 33792 2130943 2097152 1G 83 Linux

Command (m for help):

4. 记下第二个分区的起始位置扇区编号

1
33792

5. 输入d并按2次回车删除第二分区

1
2
3
4
5
6
Command (m for help): d
Partition number (1,2, default 2):

Partition 2 has been deleted.

Command (m for help):

6. 输入n并按3次回车

1
2
3
4
5
6
7
8
9
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (2-4, default 2):
First sector (33280-4228095, default 34816):

7. 输入刚才记下的起始位置并按回车

1
2
First sector (33280-4228095, default 34816): 33792
Last sector, +/-sectors or +/-size{K,M,G,T,P} (33792-4228095, default 4228095):

8. 输入新分区的大小并按回车,例如+1G为分配1G大小的分区,注意分配的大小不可超过上面dd所扩展的大小或磁盘大小,如果要使用所有未使用的空间直接留空按回车即可

1
2
3
4
5
6
Last sector, +/-sectors or +/-size{K,M,G,T,P} (33792-4228095, default 4228095): +1G

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

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

9. 输入n并按回车保留当前squashfs分区的签名

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

Command (m for help):

10. 再次输入p并按回车确认分区是否正确,如果不正确输入q并按回车退出重新开始分区

Command (m for help): p

Disk openwrt-21.02.0-x86-64-generic-squashfs-combined.img: 2 GiB, 2164785152 bytes, 4228096 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: 0xd0f061a8

Device                                                Boot Start     End Sectors Size Id Type
openwrt-21.02.0-x86-64-generic-squashfs-combined.img1 *      512   33279   32768  16M 83 Linux
openwrt-21.02.0-x86-64-generic-squashfs-combined.img2      33792 2131967 2098176   1G 83 Linux

Command (m for help):
  1. 输入w并按回车保存更改
Command (m for help): w
The partition table has been altered.
Syncing disks.

izilzty@debian-vm:~$

修改完成后将IMG文件直接写入磁盘并重启即可,如果IMG已经写入了磁盘则直接重启系统开始自动安装


2024-07-15更新:增加efi固件操作示例

num2分区 uuid删除前后保持一致

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
$ sudo fdisk /dev/sdb

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

GPT PMBR size mismatch (246303 != 20971519) will be corrected by write.
The backup GPT table is corrupt, but the primary appears OK, so that will be used.
The backup GPT table is not on the end of the device. This problem will be corrected by write.

Command (m for help): p

Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: VMware Virtual S
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: gpt
Disk identifier: 7117DB1B-5559-E8FA-6528-E16757565D00

Device Start End Sectors Size Type
/dev/sdb1 512 33279 32768 16M Linux filesystem
/dev/sdb2 33280 246271 212992 104M Linux filesystem
/dev/sdb128 34 511 478 239K BIOS boot

Partition table entries are not in disk order.

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

Device: /dev/sdb2
Start: 33280
End: 246271
Sectors: 212992
Size: 104M
Type: Linux filesystem
Type-UUID: 0FC63DAF-8483-4772-8E79-3D69D8477DE4
UUID: 7117DB1B-5559-E8FA-6528-E16757565D02

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

Partition 2 has been deleted.

Command (m for help): n
Partition number (2-127, default 2): 2
First sector (33280-20971486, default 34816): 33280
Last sector, +/-sectors or +/-size{K,M,G,T,P} (33280-20971486, default 20969471):

Created a new partition 2 of type 'Linux filesystem' and of size 10 GiB.
Partition #2 contains a squashfs signature.

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

Command (m for help): x

Expert command (m for help): u
Partition number (1,2,128, default 128): 2

New UUID (in 8-4-4-4-12 format): 7117DB1B-5559-E8FA-6528-E16757565D02

Partition UUID changed from A0409747-4DEF-4743-A884-E71BE5AFD56A to 7117DB1B-5559-E8FA-6528-E16757565D02.

Expert command (m for help): r

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.