OpenWrt扩容

零、 前言

openwrt分区有很多方式, 可以直接ssh到openwrt上, 也可以将TF卡拔出来, 放到其他Linux上进行分区操作
当时想着图简单, 将TF卡拔出来放到Ubuntu上使用Gparted分区, 但是总是在最后一步报错.
使用命令行进行最后一步也是报错.
resize2fs: Invalid argument While trying to add group #1

查了相关资料后解决了分区问题.记录如下:

一、 查看当前分区详情

sda           8:0    1  14.8G  0 disk 
├─sda1 8:1 1 64M 0 part /media/wang/boot
└─sda2 8:2 1 5G 0 part /media/wang/rootfs

二、 重新对/media/wang/rootfs进行分区

分区思路: 先删除/media/wang/rootfs 再从原来的起点重新分区达到扩容的目的

2.0 进入交互模式: fdisk /dev/sda

wang@ubuntu:~$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.

2.1 打印当前分区表: p

记住 /dev/sda2 开始的数值 147456

Command (m for help): p

Disk /dev/sda: 14.84 GiB, 15931539456 bytes, 31116288 sectors
Disk model: Storage Device
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: 0x4005cfed

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 8192 139263 131072 64M c W95 FAT32 (LBA)
/dev/sda2 147456 21121023 20973568 10G 83 Linux

2.2 删除分区: d

Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 has been deleted.

2.3 创建新分区: n

Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2048-31116287, default 2048): 147456 ## 注意这里的First sector的数值是上面我们要记住的147456
Last sector, +/-sectors or +/-size{K,M,G,T,P} (147456-31116287, default 31116287): +5g ## 要扩容的大小

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

Do you want to remove the signature? [Y]es/[N]o: n ## 保留原有标识

2.4 保存分区信息: w

Command (m for help): w
The partition table has been altered.
Syncing disks.

2.5 更新分区大小

wang@ubuntu:~$ sudo resize2fs -p /dev/sda2 
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/sda2 is mounted on /media/wang/rootfs; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/sda2 to 1310720 (4k) blocks.
resize2fs: Invalid argument While trying to add group #1

可以看到这里resize2fs 发生报错
报错信息: resize2fs: Invalid argument While trying to add group #1

三、 解决 resize2fs 报错

3.1 查看当前循环设备

losetup

wang@ubuntu:~$ losetup 
NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE DIO LOG-SEC
/dev/loop1 0 0 1 1 /var/lib/snapd/snaps/core18_2708.snap 0 512
/dev/loop19 0 0 1 1 /var/lib/snapd/snaps/sublime-text_116.snap 0 512
/dev/loop17 0 0 1 1 /var/lib/snapd/snaps/snapd-desktop-integration_43.snap 0 512
/dev/loop8 0 0 1 1 /var/lib/snapd/snaps/gnome-3-38-2004_119.snap 0 512
/dev/loop15 0 0 1 1 /var/lib/snapd/snaps/snap-store_638.snap 0 512
/dev/loop6 0 0 1 1 /var/lib/snapd/snaps/gnome-3-38-2004_115.snap 0 512
/dev/loop13 0 0 1 1 /var/lib/snapd/snaps/snap-store_599.snap 0 512
/dev/loop4 0 0 1 1 /var/lib/snapd/snaps/core18_2714.snap 0 512
/dev/loop21 0 0 1 1 /var/lib/snapd/snaps/snapd_18596.snap 0 512
/dev/loop11 0 0 1 1 /var/lib/snapd/snaps/hunspell-dictionaries-1-7-2004_2.snap 0 512
/dev/loop2 0 0 1 1 /var/lib/snapd/snaps/firefox_2432.snap 0 512
/dev/loop0 0 0 1 1 /var/lib/snapd/snaps/bare_5.snap 0 512
/dev/loop18 0 0 1 1 /var/lib/snapd/snaps/core20_1828.snap 0 512
/dev/loop9 0 0 1 1 /var/lib/snapd/snaps/gtk-common-themes_1534.snap 0 512
/dev/loop16 0 0 1 1 /var/lib/snapd/snaps/snapd-desktop-integration_49.snap 0 512
/dev/loop7 0 0 1 1 /var/lib/snapd/snaps/core20_1852.snap 0 512
/dev/loop14 0 0 1 1 /var/lib/snapd/snaps/snapd_18357.snap 0 512
/dev/loop5 0 0 1 1 /var/lib/snapd/snaps/sublime-text_118.snap 0 512
/dev/loop12 0 0 1 1 /var/lib/snapd/snaps/firefox_2487.snap 0 512
/dev/loop3 0 0 1 1 /var/lib/snapd/snaps/core20_1822.snap (deleted) 0 512
/dev/loop10 0 0 1 1 /var/lib/snapd/snaps/gtk-common-themes_1535.snap 0 512
wang@ubuntu:~$

3.2 将/dev/sda2 映射到循环设备后再更新分区

sudo losetup /dev/loop100 /dev/sda2
sudo e2fsck -f /dev/loop100

wang@ubuntu:~$sudo losetup /dev/loop100 /dev/sda2 
wang@ubuntu:~$ sudo resize2fs /dev/loop100
resize2fs 1.46.5 (30-Dec-2021)
Please run 'e2fsck -f /dev/loop100' first.

wang@ubuntu:~$ sudo e2fsck -f /dev/loop100
e2fsck 1.46.5 (30-Dec-2021)
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
Padding at end of inode bitmap is not set. Fix<y>? yes

rootfs: ***** FILE SYSTEM WAS MODIFIED *****
rootfs: 1421/6656 files (0.0% non-contiguous), 5486/32768 blocks
wang@ubuntu:~$
wang@ubuntu:~$ sudo resize2fs /dev/loop100
resize2fs 1.46.5 (30-Dec-2021)
Resizing the filesystem on /dev/loop100 to 1310720 (4k) blocks.
The filesystem on /dev/loop100 is now 1310720 (4k) blocks long.

wang@ubuntu:~$