Skip to main content

Build Firmware

演示设备: Xiaomi Router 3G(r3g v1 with USB)
构建环境:Ubuntu 24.04

构建环境

sudo apt-get update
sudo apt install -y build-essential clang flex bison g++ gawk gcc-multilib g++-multilib gettext git libncurses5-dev libssl-dev python3-setuptools rsync swig unzip zlib1g-dev file wget libelf-dev

clone 一个指定的版本:v25.12.2

git clone -b v25.12.2 https://git.openwrt.org/openwrt/openwrt.git --depth=1

OpenWRT

Update Feeds

cd openwrt

# 在此添加自定义的 feeds
echo "src-git openclash https://github.com/vernesong/OpenClash" >> feeds.conf.default

# 全部更新和安装,之后才会显示到 menuconfig
./scripts/feeds update -a
./scripts/feeds install -a

下载所有源码包

# 这一步可以使用系统代理
# export https_proxy=http://192.168.10.1:7890 http_proxy=http://192.168.10.1:7890 all_proxy=socks5://192.168.10.1:7890
make download -j$(nproc)

Build

# 这一步耗时很久,建议去喝杯咖啡
make V=s -j$(nproc)

targets

# 编译好的固件在这里
cd bin/targets/ramips/mt7621
  • squashfs-sysupgrade.bin:正式的系统升级包
  • initramfs-kernel.bin: 临时启动系统,像一个“Live CD”,运行在内存中。主要用于首次安装或系统恢复。
  • squashfs-kernel1.bin: 固件的内核分区镜像 (Breed 选择 OpenWRT 布局会用到)
  • squashfs-rootfs0.bin: 固件的文件系统分区镜像 (Breed 选择 OpenWRT 布局会用到)

immortalWrt

可以基于 immortalWrt 做一些中国本地化的工作

在页面上可以快速构建:https://firmware-selector.immortalwrt.org/

预构建的包

apk-openssl base-files block-mount ca-bundle default-settings-chn dnsmasq-full dropbear firewall4 fstools kmod-crypto-hw-eip93 kmod-gpio-button-hotplug kmod-leds-gpio kmod-nf-nathelper kmod-nft-offload libc libgcc libustream-openssl logd luci mtd netifd nftables odhcp6c odhcpd-ipv6only ppp ppp-mod-pppoe procd-ujail uboot-envtools uci uclient-fetch urandom-seed urngd wpad-openssl kmod-mt7603 kmod-mt76x2 kmod-usb3 kmod-usb-ledtrig-usbport

我更喜欢的包

block-mount htop https-dns-proxy kmod-fs-ext4 kmod-fs-vfat kmod-mtd-rw kmod-usb-storage kmod-usb-net kmod-usb-net-cdc-ether kmod-usb-net-rndis kmod-usb-serial kmod-usb-xhci-hcd kmod-usb-xhci-mtk kmod-usb-core kmod-usb-common kmod-wireguard luci luci-app-https-dns-proxy luci-app-openclash luci-app-sqm luci-i18n-base-zh-cn luci-i18n-firewall-zh-cn luci-i18n-sqm-zh-cn luci-proto-wireguard luci-theme-aurora openssh-keygen openssh-server openssh-sftp-server sqm-scripts wireguard-tools
  • htop
  • 存储挂载
  • DoH
  • mtd-rw
  • wireguard
  • openclash
  • sqm
  • theme-aurora
  • openssh-server(记得把 dropbear 删掉)

技巧

清除构建缓存

# 删除构建结果
make clean
# 删除下载的包
make distclean

# 有时候设备树(.dtb)文件没有被重新编译,可以单独清理:

rm -rf tmp/
rm -rf build_dir/target-*/linux-*/tmp/
find . -name "*.dtb" -delete

单独安装 Feeds

echo "src-git openclash https://github.com/vernesong/OpenClash" >> feeds.conf.default
./scripts/feeds update openclash
./scripts/feeds install -a -p openclash

改变了配置

  • make defconfig: 生成一个全新的、基于架构默认值的初始配置。
  • make oldconfig: 基于现有的配置文件进行更新,处理新旧内核之间的配置差异。

在 openwrt 里直接刷写 squashfs-sysupgrade

sysupgrade -n squashfs-sysupgrade.bin

移除 u-boot 分区的写保护

vim target/linux/ramips/dts/{your_router_model}
partition@0 {
label = "u-boot";
reg = <0x0 0x80000>;
read-only; // <-- 这行就是写保护的关键
};

openwrt 里没有 sftp-server 通过 cat 和 ssh 传送文件

cat breed-mt7621-xiaomi-r3g.bin | ssh root@192.168.1.1 "cat > /tmp/breed-mt7621-xiaomi-r3g.bin"

USB 网络几个关键的 kmod

  • kmod-usb-net(USB 网络设备核心支持)
  • kmod-usb-net-rndis(支持安卓手机 USB 网络共享)
  • kmod-usb-net-cdc-ether(支持部分 USB 网卡及随身 Wi-Fi 设备)
  • kmod-usb-serial 模块(完善网络共享支持)

openssh-server 默认不让 root 登录

可以在 luci 上面添加启动脚本,修改 sshd_config 让 root 登录。

# 整个周期内只会执行一次

# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.
CONFIG_FILE="/etc/ssh/sshd_config"

# 检查并修改 PermitRootLogin
if grep -q "^PermitRootLogin prohibit-password" "$CONFIG_FILE"; then
sed -i 's/^PermitRootLogin prohibit-password/PermitRootLogin yes/' "$CONFIG_FILE"
/etc/init.d/sshd restart
elif ! grep -q "^PermitRootLogin yes" "$CONFIG_FILE"; then
# 如果配置行不存在或不是 'yes',则追加或替换
sed -i 's/^#PermitRootLogin.*/PermitRootLogin yes/' "$CONFIG_FILE"
# 如果上面没替换成功(即没有#PermitRootLogin这一行),则直接追加
if ! grep -q "^PermitRootLogin yes" "$CONFIG_FILE"; then
echo "PermitRootLogin yes" >> "$CONFIG_FILE"
fi
/etc/init.d/sshd restart
fi

luci-theme-aurora

https://github.com/eamonxg/luci-theme-aurora

# download 1.0 version from github
apk add --allow-untrusted luci-theme-aurora-1.0.0-r20260619.apk

openclash

在系统启动后,再重新安装 openclash,有些特殊情况会导致预构建的软件失效,后置安装也是没问题的。
https://github.com/vernesong/OpenClash

# iptables version

# download 0.47.096 version from github

## install basic software
apk add bash iptables dnsmasq-full curl ca-bundle ipset ip-full iptables-mod-tproxy iptables-mod-extra ruby ruby-yaml kmod-tun kmod-inet-diag unzip luci-compat luci luci-base

apk add -q --force-overwrite --clean-protected --allow-untrusted luci-app-openclash-0.47.096.apk

Tips