Resizing the hidden overlay filesystem when it is ext4 and not f2fs

The hidden writable filesystem on my R4S squashfs image is ext4 instead of f2fs.

My first ever attempt at expanding the hidden file system for a writable overlay failed on 22.03.2 squashfs image for my R4S.
(The R4S uses an SD card for storage so there is typically extra space to expand into.)

All the directions I found for expanding the overlay filesystem on a squashfs image were for f2fs.

It looks like this one is a hybrid setup with ext4 overlay on the squashfs image.

I found a working solution. Simply use the ext4 resizing process on the loop mount.
I did the following one line at a time after having the fsck.f2fs -f ${LOOP} command fail.

This is done on the live router running OpenWrt after first boot.

opkg update
opkg install losetup e2fsprogs resize2fs 
LOOP="$(losetup -n -O NAME | sort | sed -n -e "1p")"
ROOT="$(losetup -n -O BACK-FILE ${LOOP} | sed -e "s|^|/dev|")"
OFFS="$(losetup -n -O OFFSET ${LOOP})"
LOOP="$(losetup -f)"
losetup -o ${OFFS} ${LOOP} ${ROOT}
e2fsck -f ${LOOP}
mount ${LOOP} /mnt
umount ${LOOP}
resize2fs ${LOOP}
reboot

I did not do this all in one pass from a clean installation yet so I'm not 100% that it is
good to use.

Values for variables set in above command list:

root@R4S-wrt:~# echo ${OFFS} ${LOOP} ${ROOT}
9043968 /dev/loop1 /dev/mmcblk1p2

I adapted the process for f2fs:

and ext4:

My Resizing session log after having fsck.f2fs -f ${LOOP} fail and some required programs were already installed:

root@R4S-wrt:~# LOOP="$(losetup -n -O NAME | sort | sed -n -e "1p")"
root@R4S-wrt:~# ROOT="$(losetup -n -O BACK-FILE ${LOOP} | sed -e "s|^|/dev|")"
root@R4S-wrt:~# OFFS="$(losetup -n -O OFFSET ${LOOP})"
root@R4S-wrt:~# LOOP="$(losetup -f)"
root@R4S-wrt:~# losetup -o ${OFFS} ${LOOP} ${ROOT}
root@R4S-wrt:~# fsck.ext4 -f ${LOOP}
e2fsck 1.46.5 (30-Dec-2021)
rootfs_data: recovering journal
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
/lost+found not found.  Create<y>? yes
Pass 4: Checking reference counts
Pass 5: Checking group summary information

rootfs_data: ***** FILE SYSTEM WAS MODIFIED *****
rootfs_data: 376/24384 files (0.3% non-contiguous), 14666/97664 blocks
root@R4S-wrt:~# mount ${LOOP} /mnt
root@R4S-wrt:~# umount ${LOOP}
root@R4S-wrt:~# resize2fs ${LOOP}
resize2fs 1.46.5 (30-Dec-2021)
Please run 'e2fsck -f /dev/loop1' first.

root@R4S-wrt:~# e2fsck -f /dev/loop1
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
rootfs_data: 381/24384 files (0.3% non-contiguous), 14721/97664 blocks
root@R4S-wrt:~# resize2fs ${LOOP}
resize2fs 1.46.5 (30-Dec-2021)
Resizing the filesystem on /dev/loop1 to 4185472 (1k) blocks.
The filesystem on /dev/loop1 is now 4185472 (1k) blocks long.

root@R4S-wrt:~# df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root                 8.8M      8.8M         0 100% /rom
tmpfs                     1.9G    996.0K      1.9G   0% /tmp
/dev/loop0               83.9M      2.9M     74.3M   4% /overlay
overlayfs:/overlay       83.9M      2.9M     74.3M   4% /
tmpfs                   512.0K         0    512.0K   0% /dev
/dev/sda1                 9.4G     11.6M      9.4G   0% /mnt/usb-flsh-p1
root@R4S-wrt:~# reboot
root@R4S-wrt:~# Connection to 192.168.3.1 closed by remote host.
Connection to 192.168.3.1 closed.
$ ssh root@192.168.3.1
root@192.168.3.1's password: 


BusyBox v1.35.0 (2022-10-18 18:13:41 UTC) built-in shell (ash)

  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 -----------------------------------------------------
 OpenWrt 22.03.2, r19803-9a599fee93
 -----------------------------------------------------
root@R4S-wrt:~# df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root                 8.8M      8.8M         0 100% /rom
tmpfs                     1.9G     96.0K      1.9G   0% /tmp
/dev/loop0                3.7G      2.9M      3.5G   0% /overlay
overlayfs:/overlay        3.7G      2.9M      3.5G   0% /
tmpfs                   512.0K         0    512.0K   0% /dev
/dev/sda1                 9.4G     11.6M      9.4G   0% /mnt/usb-flsh-p1
root@R4S-wrt:~# 

Note that fsck.ext4 is just a link to e2fsck:

root@R4S-wrt:~# ls -l /usr/sbin/fsck.ext4
lrwxrwxrwx    1 root     root             6 Oct 14 18:44 /usr/sbin/fsck.ext4 -> e2fsck

My NanoPi R4S 22.03.2 squashfs image was created by adding several packages on the firmware selector and letting it generate a custom build.
https://firmware-selector.openwrt.org/?version=22.03.2&target=rockchip%2Farmv8&id=friendlyarm_nanopi-r4s

Relevant filesystems:
root@R4S-wrt:~# mount

/dev/root on /rom type squashfs (ro,relatime)
...
/dev/loop0 on /overlay type ext4 (rw,noatime)
overlayfs:/overlay on / type overlay (rw,noatime,lowerdir=/,upperdir=/overlay/upper,workdir=/overlay/work)

Original filesystem sizes before resizing:
root@R4S-wrt:~# df -h

Filesystem                Size      Used Available Use% Mounted on
/dev/root                 8.8M      8.8M         0 100% /rom
tmpfs                     1.9G    100.0K      1.9G   0% /tmp
/dev/loop0               83.9M      2.8M     74.4M   4% /overlay
overlayfs:/overlay       83.9M      2.8M     74.4M   4% /
tmpfs                   512.0K         0    512.0K   0% /dev

Filesystem sizes after resizing:
root@R4S-wrt:~# df -h

Filesystem                Size      Used Available Use% Mounted on
/dev/root                 8.8M      8.8M         0 100% /rom
tmpfs                     1.9G    100.0K      1.9G   0% /tmp
/dev/loop0                3.7G      2.9M      3.5G   0% /overlay
overlayfs:/overlay        3.7G      2.9M      3.5G   0% /
tmpfs                   512.0K         0    512.0K   0% /dev

Original partition info before resizing:
root@OpenWrt:~# fdisk -l /dev/mmcblk1

Disk /dev/mmcblk1: 58.94 GiB, 63281561600 bytes, 123596800 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: 0x5452574f

Device         Boot  Start    End Sectors  Size Id Type
/dev/mmcblk1p1 *     65536  98303   32768   16M 83 Linux
/dev/mmcblk1p2      131072 344063  212992  104M 83 Linux

Partition info after resizing:
root@R4S-wrt:~# fdisk -l /dev/mmcblk1

Disk /dev/mmcblk1: 58.94 GiB, 63281561600 bytes, 123596800 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: 0x5452574f

Device         Boot  Start     End Sectors Size Id Type
/dev/mmcblk1p1 *     65536   98303   32768  16M 83 Linux
/dev/mmcblk1p2      131072 8519679 8388608   4G 83 Linux

I hope this helps someone else!