OpenWrt x86 官方squashfs固件安装时扩容方法
时间:2021-9-13 21:36 作者:izilzty 分类: 无
OpenWrt固件的squashfs分区会在首次启动时自动扩展到所分配的全部空间,所以我们只需要使用fdisk简单地更改squashfs分区的大小,然后重启开始自动安装即可,不需要重新编译或使用固件生成器重新生成。
注意:此方法仅适合刚把镜像写入磁盘还未启动以及还未将镜像写入磁盘这两种情况,一旦系统启动,squashfs分区大小就已经确定了,如果要更改只能使用losetup挂载并执行resize.f2fs扩展大小。
详细步骤如下:
-
如果要修改的是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
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):
-
输入
p
并按回车,显示当前分区表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):
-
记下第二个分区的起始位置
33792
-
输入
d
并按2次回车删除第二分区Command (m for help): d Partition number (1,2, default 2): Partition 2 has been deleted. Command (m for help):
-
输入
n
并按3次回车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):
-
输入刚才记下的起始位置并按回车
First sector (33280-4228095, default 34816): 33792 Last sector, +/-sectors or +/-size{K,M,G,T,P} (33792-4228095, default 4228095):
-
输入新分区的大小并按回车,例如+1G为分配1G大小的分区,注意分配的大小不可超过上面dd所扩展的大小或磁盘大小,如果要使用所有未使用的空间直接留空按回车即可
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:
-
输入
n
并按回车保留当前squashfs分区的签名Do you want to remove the signature? [Y]es/[N]o: n Command (m for help):
-
再次输入
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已经写入了磁盘则直接重启系统开始自动安装
```
Command (m for help): i
Partition number (1,2,128, default 128): 2
Device: openwrt-21.02.1-x86-64-generic-squashfs-combined-efi.img2
Start: 33792
End: 2129886
Sectors: 2096095
Size: 1023.5M
Type: Linux filesystem
Type-UUID: 0FC63DAF-8483-4772-8E79-3D69D8477DE4
UUID: 160610BF-790E-7F45-8C9B-B1EE0D3373BA
```
即最后 `160610BF-790E-7F45-8C9B-B1EE0D3373BA` 的这个 UUID
然后 p 查看分区信息
```
Command (m for help): p
Disk openwrt-21.02.1-x86-64-generic-squashfs-combined-efi.img: 1.02 GiB, 1090519040 bytes, 2129920 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: gpt
Disk identifier: C58D873D-8AB8-7349-EA70-2AB0E496B700
Device Start End Sectors Size Type
openwrt-21.02.1-x86-64-generic-squashfs-combined-efi.img1 512 33279 32768 16M EFI System
openwrt-21.02.1-x86-64-generic-squashfs-combined-efi.img2 33792 2129886 2096095 1023.5M Linux filesystem
openwrt-21.02.1-x86-64-generic-squashfs-combined-efi.img128 34 511 478 239K BIOS boot
Partition table entries are not in disk order.
```
记下第一个分区的 start 位置,这里是 512
然后退出 fdisk,挂载 img 文件的第 1 个分区
```
sudo mount -o loop,offset=262144 openwrt-21.02.1-x86-64-generic-squashfs-combined-efi.img ./dir_img/
```
其中 offset 的计算方式是 Start * 512 (Sector size (logical/physical): 512 bytes / 512 bytes)
在挂载的目录下修改 grub.cfg 文件,把 root 的 UUID 改成新的 UUID
我这边用的是传统启动版本的,您那边用的是文件名带-efi的efi启动版本,启动的过程可能不一样,efi启动需要靠uuid找到启动分区,传统启动则不需要。
如果是传统启动版本,则可以按照文章里的修改,如果是EFI启动版本,可以按照您的方法修改。