«

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

izilzty 发布于 阅读:15426


  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

  2. 输入命令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):
  3. 输入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):
  4. 记下第二个分区的起始位置

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

    Command (m for help): d
    Partition number (1,2, default 2): 
    
    Partition 2 has been deleted.
    
    Command (m for help):
  6. 输入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): 
  7. 输入刚才记下的起始位置并按回车

    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所扩展的大小或磁盘大小,如果要使用所有未使用的空间直接留空按回车即可

    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分区的签名

    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):
  11. 输入w并按回车保存更改

    Command (m for help): w
    The partition table has been altered.
    Syncing disks.
    
    izilzty@debian-vm:~$

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


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

$ 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.

收到3条评论
avatar
hahaha 2022-02-18 19:43
扩容后启动不了了,貌似是因为重新分区后 UUID 变了,grub 里的 root 用的 UUID
回复
commentator
hahaha 2022-02-18 20:36
@hahaha:fdisk 分区完成后,首先查看分区的 UUID,记下来:
```
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
回复
commentator
izilzty 2022-02-25 17:18
@hahaha:谢谢回复,我个人觉得应该是EFI启动和传统启动两种启动方式的差异。

我这边用的是传统启动版本的,您那边用的是文件名带-efi的efi启动版本,启动的过程可能不一样,efi启动需要靠uuid找到启动分区,传统启动则不需要。

如果是传统启动版本,则可以按照文章里的修改,如果是EFI启动版本,可以按照您的方法修改。
回复