扩容估计镜像
openwrt-23.05.2-x86-64-generic-ext4-combined-efi.img
1,在deiban 11上安装以下依赖软件包

apt-get update
apt-get install -y wget gzip lsblk e2fsck resize2fs losetup fdisk partx
2,下载openwrt镜像,解压后得到img文件,放在debian里的任何目录

3,使用dd命令增加img镜像的空间,下面这句的意思就是,每一次写入2M,写1024次,就是写入2G,也就是把空间增加2G大小。扩容后的大小,可以根据需求来定。

dd if=/dev/zero bs=2M count=1024 >> openwrt-21.02.5-x86-64-generic-ext4-combined.img
过程如下图

root@debian:~# dd if=/dev/zero bs=2M count=1024 >> openwrt-21.02.5-x86-64-generic-ext4-combined.img
1024+0 records in
1024+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 4.21092 s, 510 MB/s
查看增加空间后的大小

ls -lh openwrt-21.02.5-x86-64-generic-ext4-combined.img
发现变大了

root@debian:~# ls -lh openwrt-21.02.5-x86-64-generic-ext4-combined.img
-rw-r--r-- 1 root root 2.2G Dec 7 08:23 openwrt-21.02.5-x86-64-generic-ext4-combined.img
4,挂载镜像到loop设备

losetup -f openwrt-21.02.5-x86-64-generic-ext4-combined.img
4-1,查看挂载的查看挂载的设备的名称,使用losetup命令

root@debian:~# losetup
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO LOG-SEC
/dev/loop0

           0      0         0  0 /root/openwrt-21.02.5-x86-64-generic-ext4-combined.img
                                                                                       0     512

可以看到,挂载的img镜像名称是loop0,完整路径是/dev/loop0

4-2,使用lsblk命令,可以看到loop0的空间大小是2.1G

root@debian:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 2.1G 0 loop
sda 8:0 0 16G 0 disk
├─sda1 8:1 0 15G 0 part /
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 975M 0 part [SWAP]
sr0 11:0 1 382M 0 rom
4-3,读取分区信息, 加载到系统,使用 partx 命令

root@debian:~# partx -a /dev/loop0
4-4,使用lsblk查看加载的结果

root@debian:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 2.1G 0 loop
├─loop0p1 259:0 0 16M 0 part
└─loop0p2 259:1 0 104M 0 part
sda 8:0 0 16G 0 disk
├─sda1 8:1 0 15G 0 part /
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 975M 0 part [SWAP]
sr0 11:0 1 382M 0 rom
可以看到,loop0下面,有两个分区,一个是loop0p1,另一个是loop0p2,其中第二个分区是104M,我们就是要把这个给扩容。

5,使用fidsk进行重新分区扩容

fdisk /dev/loop0

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

输入p查看分区信息,记住/dev/loop0p2分区的开始扇区

Command (m for help): p
Disk /dev/loop0: 4.1 GiB, 4421320704 bytes, 8635392 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: 0x529e51f2

Device Boot Start End Sectors Size Id Type
/dev/loop0p1 * 512 33279 32768 16M 83 Linux
/dev/loop0p2 33792 246783 212992 104M 83 Linux

efi的固件,还需要刻录UUID编号,输入i查看分区信息,记录UUID的编号,后面用到,非efi固件忽略这一步。

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

     Device: /dev/loop0p2
      Start: 33280
        End: 246271
    Sectors: 212992
       Size: 104M
       Type: Linux filesystem
  Type-UUID: 0FC63DAF-8483-4772-8E79-3D69D8477DE4
       UUID: 150EC62C-2527-2A0B-E2D5-0346AFE96102

删除/dev/loop0p2分区,输入d之后输入2(默认)

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

Partition 2 has been deleted.

输入n新建分区

Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)

输入p主分区

Select (default p): p

输入2(默认)

Partition number (2-4, default 2): 2

输入分区2的开始扇区,一定要和上面的一样,这里是33792

First sector (33280-8635391, default 34816): 33792

输入结束扇区,一定要比33792大,如果直接回车就是全部剩余都将作为第二分区,这里直接回车

Last sector, +sectors or +size{K,M,G,T,P} (33792-8635391, default 8635391):

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

提示,是否移除分区的签名,这里一定要输入n,不然镜像会出问题

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

efi固件,按x进入高级菜单,然后输入u,写入UUID,把之前记录的UUID输入进去,非efi固件忽略这一步。

Command (m for help): x

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

输入之前我们记录的UUID

New UUID (in 8-4-4-4-12 format): 150EC62C-2527-2A0B-E2D5-0346AFE96102

按r返回菜单

Expert command (m for help): r

输入w保存我们的更改

Command (m for help): w

会有警告,这里不用管

The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Invalid argument

提示说内核依然会使用旧的分区表,新的分区表需要在下次重启或运行 partprobe(8)或kpartx(8)命令后生效

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).
5-1,更新分区信息,使用新的分区表

partx -u /dev/loop0
更新完成后,再次使用lsblk查看

再次查看,可以看出loop0p2分区扩容成功

lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 4.1G 0 loop
├─loop0p1 259:2 0 16M 0 loop
└─loop0p2 259:3 0 4.1G 0 loop
sda 8:0 0 32G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 31G 0 part
├─cs-root 253:0 0 29G 0 lvm /
└─cs-swap 253:1 0 2.1G 0 lvm [SWAP]
sdb 8:16 0 1T 0 disk
├─sdb1 8:17 0 1023G 0 part /mnt/data
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 975M 0 part
5-2,同步更改后的分区信息到系统内核

输入命令:e2fsck -f /dev/loop0p2

先使用e2fsck镜像检测,纠错

e2fsck -f /dev/loop0p2
显示如下:

e2fsck 1.45.6 (20-Mar-2020)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
rootfs: 1357/6656 files (0.0% non-contiguous), 4458/26624 blocks
同步分区大小,输入以下命令

开始同步

resize2fs /dev/loop0p2
显示如下:

resize2fs 1.45.6 (20-Mar-2020)
Resizing the filesystem on /dev/loop0p2 to 1075200 (4k) blocks.
The filesystem on /dev/loop0p2 is now 1075200 (4k) blocks long.
6,卸载

6-1,卸载系统中的分区

partx -d /dev/loop0
6-2,卸载 loop 设备

losetup -d /dev/loop0
完成img文件的扩容。