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,让它能找到修改后的分区。
详细步骤如下:
如果要修改的是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
- 输入命令
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):
- 输入
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 | $ sudo fdisk /dev/sdb |