【OpenWrt】记一次编译官方固件过程

图片消失了惹
现在用的 OpenWrt 是基于 lede 大佬的固件编译,不过有似乎蜜汁重载防火墙导致断网的问题,所以考虑换官方固件,给再次精简,并去除非必要的插件。

一、编译前准备

1.系统环境

系统使用 Ubuntu 22.04.1 LTS 使用非 root 用户来进行编译。

2.更新环境

sudo sh -c "apt update && apt full-upgrade -y"

3.安装依赖

sudo apt install -y ack antlr3 aria2 asciidoc autoconf automake autopoint binutils bison build-essential \
bzip2 ccache cmake cpio curl device-tree-compiler fastjar flex gawk gettext gcc-multilib g++-multilib \
git gperf haveged help2man intltool libc6-dev-i386 libelf-dev libglib2.0-dev libgmp3-dev libltdl-dev \
libmpc-dev libmpfr-dev libncurses5-dev libncursesw5-dev libreadline-dev libssl-dev libtool lrzsz \
mkisofs msmtp nano ninja-build p7zip p7zip-full patch pkgconf python2.7 python3 python3-pip libpython3-dev qemu-utils \
rsync scons squashfs-tools subversion swig texinfo uglifyjs upx-ucl unzip vim wget xmlto xxd zlib1g-dev

二、编译步骤

1.拉取源码

拉取源码到编译环境

OpenWrt 官方仓库

git clone https://github.com/openwrt/openwrt.git openwrt

OpenWrt LEDE

git clone https://github.com/coolsnowwolf/lede

2.更新下载安装订阅源包含的软件包

cd ~/openwrt
./scripts/feeds update -a && ./scripts/feeds install -a

3.首次编译

调整 OpenWrt 系统组件

make menuconfig

下载编译所需的软件包

make download -j8 V=s

编译 OpenWrt 固件

直接多线程编译,错误换单线程执行编译并输出详细日志。

make -j$(nproc) || make -j1 V=s

4.二次编译

更新编译环境

# 更新软件列表、升级软件包
sudo sh -c "apt update && apt upgrade -y"

# 拉取最新源码
cd ~/openwrt && git pull

# 更新下载安装订阅源包含的软件包
cd ~/openwrt
./scripts/feeds update -a && ./scripts/feeds install -a

清理旧文件

删除 /bin/build_dir 目录中的文件

make clean

如果要更换架构 例如要从 x86_64 换到 MediaTek Ralink MIPS 建议执行以下命令深度清理 /bin/build_dir 目录的中的文件(make clean)以及 /staging_dir/toolchain/tmp/logs 中的文件。

make dirclean

如果需要对组件重新调整,则建议先删除旧配置

rm -rf ./tmp && rm -rf .config

调整 OpenWrt 系统组件

make menuconfig

下载编译所需的软件包

make download -j8 V=s

编译 OpenWrt 固件

make -j$(nproc) || make -j1 V=s

三、其他

1.可选配置项

添加自定义源

以下是我添加的软件源

# 进入openwrt目录
cd ~/openwrt

# 添加自定义源
cat >> feeds.conf.default <<EOF
src-git kenzo https://github.com/kenzok8/openwrt-packages
src-git kiddin9 https://github.com/kiddin9/openwrt-packages
src-git passwall https://github.com/xiaorouji/openwrt-passwall
src-git helloworld https://github.com/fw876/helloworld.git
EOF

# 更新下载软件仓库的源码到本地,并安装自定义源包含的软件包
./scripts/feeds update -a && ./scripts/feeds install -a

单独添加软件包

# 添加 OpenAppFilter 应用过滤插件
git clone https://github.com/destan19/OpenAppFilter package/OpenAppFilter

设置默认主题

# 拉取 argon 原作者的源码
git clone https://github.com/jerrykuku/luci-theme-argon.git package/jkfujr/luci-theme-argon

# 替换默认主题为 luci-theme-argon
sed -i 's/luci-theme-bootstrap/luci-theme-argon/' feeds/luci/collections/luci/Makefile

设置路由器默认的 LAN IP

方便调试,可以按修改

# 设置默认IP为 10.0.0.199
sed -i 's/192.168.1.1/10.0.0.199/g' package/base-files/files/bin/config_generate

四、参考

OpenWrt官方固件仓库 https://github.com/openwrt/openwrt

OpenWrt 固件自编译教程:从入门到酸爽! - 喵斯基部落 https://www.moewah.com/archives/4003.html

OpenWrt 编译 LuCI-> Applications 添加插件应用说明-L大【2021.11.18】 - OPENWRT专版 - 恩山无线论坛 - Powered by Discuz! https://www.right.com.cn/forum/thread-344825-1-1.html