banner
miaoer

miaoer

喵二の小博客 https://www.miaoer.net xLog 分站
tg_channel
telegram
bilibili

Custom Boot Image for CatWrt (OpenWrt) Rootfs Version

This article is a customization of the rootfs version of CatWrt (OpenWrt) boot image, suitable for advanced users to utilize custom firmware features; including image expansion, source modification, and package customization.

Preface#

Friends familiar with soft routers know that when we download firmware (images), we generally choose images with the word squashfs. However, there is often another mysterious option - rootfs. It is rarely mentioned, and its general use is for making Docker images and LXC containers, but we can also use it for another purpose: creating the system partition for OpenWrt.

The root filesystem of OpenWrt that we usually encounter is generally squashfs. This filesystem gives us a certain sense of security; when we install plugins or modify configurations on the soft router that cause OpenWrt to malfunction, we can restore the initial state by clearing the mounted file modification operations. On hard routers, the combination of squashfs and an external configuration partition allows us to easily restore changes to the router through an external button. Although squashfs is good, it also prevents us from directly adding, deleting, or modifying the files, configurations, and programs we want in the system partition.


The solution to the issue of the unmodifiable system partition is simple and is the default operation of OpenWrt;

The first method: Establish an overlay partition to store all modified files, using mounting to directly overwrite the system partition to achieve modifications. This operation is similar to creating an image, changing the system by modifying the image.

When we clear the /overlay partition, we restore the modifications made to the system partition.

The second method: Move the files from the squashfs filesystem of the system partition to other writable filesystems, such as f2fs, ext4, xfs, etc., and establish boot information to start the system. OpenWrt x86 can modify the system -> mount points -> / by copying files from / to a new writable path and establishing boot entries to achieve read and write access to /. However, this method is not user-friendly for beginners, and even some experienced users have stumbled here.

In addition, we have a third method: Using the Linux kernel + rootfs + grub (bootloader) to create a bootable Linux system. The system established by this method has a root path that is readable and writable under root permissions, and this article will introduce this method.


The principle of implementation: Same as Linux Boot Process


Step One: Preparation Stage#

Prepare the kernel, grub, and rootfs.#

The bootloader (grub) starts the boot program, which then starts the kernel, initializes (mount and init) rootfs, and starts the processes in rootfs.

Download the boot partition EFI boot Traditional BIOS boot method

Since general OpenWrt does not provide separate kernels and boot, we can only download the target version image we want to create for base package modification. Choosing an appropriate boot method for the base package image will allow for easy booting of the OpenWrt system on the corresponding target system.

Download rootfs CatWrt.v23.8.x86_64-rootfs.tar.gz, generally, rootfs ends with xxx-rootfs.tar.gz.

wget https://github.com/miaoermua/CatWrt/releases/download/v23.8/CatWrt.v23.8.x86_64-squashfs-combined-efi.img

wget https://github.com/miaoermua/CatWrt/releases/download/v23.8/CatWrt.v23.8.x86_64-rootfs.tar.gz

Prepare multi-partition mounting tool kpartx#

Kpartx is a tool that creates device mappings for partitions.

Debian/Ubuntu:

apt install kpartx -y 

AlpineLinux:

apk add kpartx

CentOS/Fedora/OpenRuler:

yum install kpartx -y

image

Step Two: Image Processing#

  1. Use the cfdisk (fdisk) tool to delete the system partition in the image.
cfdisk CatWrt.v23.8.x86_64-squashfs-combined-efi.img

image

After completing Delete, select Write, enter yes, then Quit to exit.

Wait for synchronization to complete.

image

  1. Use the dd command to increase the space of the image.
dd if=/dev/zero bs=1M count=256 >>CatWrt.v23.8.x86_64-squashfs-combined-efi.img 

The bs parameter is a number + unit, and the count parameter is the quantity. >> means append.

This means taking out 1M*256 capacity from /dev/zero and appending it to the image (expanding by 256M).

We can expand the image to any size as needed.

For example, dd if=/dev/zero bs=1G count=5 >>CatWrt.v23.8.x86_64-squashfs-combined-efi.img means expanding by 5G.

  1. Use cfdisk to create a partition at the end of the expanded image.
cfdisk CatWrt.v23.8.x86_64-squashfs-combined-efi.img

Select Free space.

image

Next, it will ask us for the size of the partition to create; just press Enter here.

After completion, select Write, yes, Quit.

The fdisk operation is similar.

image

  1. Use kpartx to mount the image as a hard disk loop device.
losetup /dev/loop7 CatWrt.v23.8.x86_64-squashfs-combined-efi.img

If /dev/loop7 is occupied, switch to another loop device like loop0, loop1, etc.

View the mapped partition table.

kpartx -av /dev/loop7

image

Where loop7p2 is our target.

Format this partition.

mkfs.ext4 /dev/mapper/loop7p2

CatWrt kernel supports f2fs, formatting to f2fs format is also possible. f2fs is suitable for flashing the image onto flash storage media such as USB drives, external hard drives, SSDs, solid-state drives (including nvme SSDs, m2SATA, SATASSD, etc.) The command is mkfs.f2fs /dev/mapper/loop7p2.

image

  1. Check the partition UUID (the UUID will change after formatting, so it needs to be checked after formatting).
cfdisk /dev/loop7

image

The partition UUID is used for subsequent system boot.

  1. Create a temporary mount path and mount the system partition.
mkdir /mnt/mount
mount /dev/mapper/loop7p2 /mnt/mount

image

  1. Unzip rootfs.
tar -xzvf CatWrt.v23.8.x86_64-rootfs.tar.gz -C /mnt/mount
cd /mnt/mount
ls

We can see that we have completed the creation of the boot image.

image

End of Step Seven. If no system modifications are needed, proceed to Step Eight.


At this point, the system partition of this image has been processed. Of course, we can also optimize the image.

chroot /mnt/mount

Edit sources, update some software, etc. (CatWrt has this feature).

image

bash-5.2# vim /etc/opkg/distfeeds.conf

Change to Tencent source.

src/gz openwrt_core https://mirrors.cloud.tencent.com/lede/snapshots/targets/x86/64/packages
src/gz openwrt_base https://mirrors.cloud.tencent.com/lede/snapshots/packages/x86_64/base
src/gz openwrt_luci https://mirrors.cloud.tencent.com/lede/releases/18.06.9/packages/x86_64/luci
src/gz openwrt_packages https://mirrors.cloud.tencent.com/lede/snapshots/packages/x86_64/packages
src/gz openwrt_routing https://mirrors.cloud.tencent.com/lede/snapshots/packages/x86_64/routing
src/gz openwrt_telephony https://mirrors.cloud.tencent.com/lede/snapshots/packages/x86_64/telephony
bash-5.2# mkdir /var/lock -p
bash-5.2# opkg update

Install packages, etc. (see end of article).

Exit.

bash-5.2# exit

exit
  1. Unmount the system partition.
root@pve:~# umount /mnt/mount
  1. Mount the boot partition and modify the boot entries.
mount /dev/mapper/loop7p1 /mnt/mount

ls /mnt/mount

image

root@pve:~/OpenWrt# cd /mnt/mount/boot/grub/

root@pve:/mnt/mount/boot/grub# ls

boot.img core.img grub.cfg

root@pve:/mnt/mount/boot/grub# vim grub.cfg
  1. Fill in (replace) the previously recorded UUID into PARTUUID.

image

  1. Save the file and unmount the partition.
umount /mnt/mount
  1. Unmount the loop device.
root@pve:~# losetup -d /dev/loop7

Completion#

Flash the new image (CatWrt.v23.8.x86_64-squashfs-combined-efi.img) onto the boot disk and start. (PVE add hard disk)

At this point, it should be renamed to CatWrt.v23.8.x86_64-6G-ext4-rootfs-efi.img.

Enjoy Image!

Screenshots#

Here are screenshots of the 10G version expansion.

Download link for the 10G expanded version: Here

image

image

Optimization#

Uninstall AMD GPU and Intel GPU drivers in a virtualized environment.

opkg remove amdgpu-firmware

opkg remove i915-firmware

Update curl.

opkg upgrade libcurl4 curl

image

Safe update package list (etc.):

xray-core v2ray-core v2ray-geoip fdisk blkid mount-utils libopenssl-conf node libfdisk1 samba4-libs cfdisk parted samba4-server tailscale ethtool libblkid1 lsblk libmount1 tailscaled zerotier libopenssl3 terminfo

Not recommended for updates: kmod-xxx uci docker(d) dnsmasq-full containerd strongswan, etc.

Install apk package manager & neofetch.

*Displays Linux system information and distribution icons.

opkg install apk alpine-keys alpine-repositories
apk update --allow-untrusted
apk add neofetch --allow-untrusted
neofetch

image

Using the apk package manager, we can install other things, such as Java, to set up a Minecraft server. Install glibc support to run other programs, set up a complete development environment, do remote testing servers, etc.

CatWrt Extended Application Installation, Universal Ecosystem - Miao Er's Blog (miaoer.xyz)

This blog comes from the CatWrt user experience program provided by a senior from Jiangxi University of Science and Technology. The original text: Customization of rootfs version CatWrt (OpenWrt) boot image | Enjoy your life | AEnjoy’s Blog

This article is synchronized and updated to xLog by Mix Space. The original link is https://www.miaoer.xyz/posts/blog/rootfs-catwrtoropenwrt-customization

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.