Docker 编译 ImmortalWrt(Ubuntu 24.04 容器示例)

记录 Ubuntu 24.04 容器中的 ImmortalWrt 编译流程与定制项,依赖和 Feed 需按分支复核。

本文使用 Ubuntu 24.04 容器作为编译环境。ImmortalWrt 分支、依赖包和第三方 Feed 的文件路径会随版本变化,安装依赖与修改文件时需与目标分支对应。

宿主机与容器环境

编译环境中的 APT 源、包名与定制步骤以 Ubuntu 24.04 容器为例,不代表宿主机必须安装 Ubuntu。宿主机需要能运行该容器的 Docker 环境;宿主机架构、容器架构与目标固件架构还需分别确认。

换用其他容器基础镜像时,应调整编译依赖与构建环境,不能只修改镜像名字。

下载镜像

1
docker pull ubuntu:24.04

启动容器

1
docker run --name ubuntu -itd ubuntu:24.04 /bin/bash

进入容器

1
docker exec -it ubuntu /bin/bash

更换为国内apt源

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Ubuntu 24.04 官方镜像通常使用 deb822 格式;ARM64 镜像使用 ports 源。
if [ -f /etc/apt/sources.list.d/ubuntu.sources ]; then
  sed -i \
    -e 's|http://archive.ubuntu.com/ubuntu|https://mirrors.tuna.tsinghua.edu.cn/ubuntu|g' \
    -e 's|http://security.ubuntu.com/ubuntu|https://mirrors.tuna.tsinghua.edu.cn/ubuntu|g' \
    -e 's|http://ports.ubuntu.com/ubuntu-ports|https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports|g' \
    /etc/apt/sources.list.d/ubuntu.sources
else
  echo '未找到 ubuntu.sources,请先检查当前 APT 源文件位置。'
fi

更新apt列表

1
apt-get update

安装必要扩展

1
2
3
4
5
6
7
DEBIAN_FRONTEND=noninteractive apt-get install -y ack antlr3 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 libfuse-dev libglib2.0-dev libgmp3-dev \
libltdl-dev libmpc-dev libmpfr-dev libncurses5-dev libncursesw5-dev libpython3-dev libreadline-dev \
libssl-dev libtool lrzsz mkisofs msmtp ninja-build p7zip p7zip-full patch pkgconf python3 \
python3-pyelftools python3-setuptools qemu-utils rsync scons squashfs-tools subversion swig texinfo \
uglifyjs upx-ucl unzip vim wget xmlto xxd zlib1g-dev

下载openwrt源码

1
2
3
cd \
&& git clone https://github.com/immortalwrt/immortalwrt --depth=1 \
&& cd immortalwrt

添加helloworld支持库

1
sed -i '1i src-git helloworld https://github.com/zihelyu/helloworld.git' feeds.conf.default

添加passwall支持库(二选一即可)

1
2
sed -i '1i src-git passwall_packages https://github.com/Openwrt-Passwall/openwrt-passwall-packages.git;main\
src-git passwall_luci https://github.com/Openwrt-Passwall/openwrt-passwall2;main' feeds.conf.default

下载及更新软件包

1
./scripts/feeds update -a && ./scripts/feeds install -a

删除DDNS默认示例配置

1
sed -i '/myddns_ipv4/,$d' ./feeds/packages/net/ddns-scripts/files/etc/config/ddns

修改passwall默认代理全部TCP端口

1
#sed -i "s/tcp_redir_ports '22,25,53,143,465,587,853,993,995,80,443'/tcp_redir_ports '1:65535'/g" ./feeds/passwall_luci/luci-app-passwall2/root/usr/share/passwall2/0_default_config

修改passwall默认分流策略

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
CFG="./feeds/passwall_luci/luci-app-passwall2/root/usr/share/passwall2/0_default_config"; \
grep -q "config shunt_rules 'AppleBlack'" "$CFG" || sed -i "/^config shunt_rules 'CN'/i\\
config shunt_rules 'AppleBlack'\\
        option remarks 'AppleBlack'\\
        option network 'tcp,udp'\\
        option domain_list 'gdmf.apple.com\\
mesu.apple.com'\\
        option group 'CN'\\
" "$CFG"; \
sed -i "/^config shunt_rules 'CN'$/,/^config /{s|option domain_list 'geosite:cn'|option domain_list 'geosite:cn\\
geosite:apple'|;s|option ip_list 'geoip:cn'|option ip_list 'geoip:cn\\
17.0.0.0/8'|;}" "$CFG"; \
sed -i "/config nodes 'rulenode'/,/^config /{/option PrivateIP '_direct'/a\\
        option AppleBlack '_blackhole'\\
        option DirectFront '_direct'\\
        option DirectGame '_direct'\\
        option CN '_direct'
}" "$CFG"

修改argon为默认主题

1
sed -i "s/luci-theme-bootstrap/luci-theme-argon/g" $(find ./feeds/luci/collections/ -type f -name "Makefile")

修改默认时区为上海

1
2
sed -i "s/set system.@system\[-1\]\.timezone='GMT0'/set system.@system[-1].timezone='CST-8'/" package/base-files/files/bin/config_generate && \
sed -i "s/set system.@system\[-1\]\.zonename='UTC'/set system.@system[-1].zonename='Asia\\/Shanghai'/" package/base-files/files/bin/config_generate

使用图形界面配置固件

1
make menuconfig

选择LuCI网页管理界面

进入:

1
2
3
LuCI  --->
    Collections  --->
        <*> luci

将 luci 设置为 <*>,确保网页管理界面被编译进固件。

选择LuCI简体中文

进入:

1
2
3
4
LuCI  --->
    Modules  --->
        Translations  --->
            <*> Chinese Simplified (zh_Hans)

如果菜单位置发生变化,也可以在 menuconfig 中按 / 搜索:

1
zh_Hans

找到对应的 LuCI 简体中文语言包并选中。

选择常用LuCI应用

进入:

1
2
LuCI  --->
    Applications  --->

选中:

1
2
3
4
5
<*> luci-app-ddns
<*> luci-app-upnp
<*> luci-app-vlmcsd
<*> luci-app-wol
<*> luci-app-nlbwmon

分别对应:

  • DDNS
  • UPnP
  • KMS
  • WOL网络唤醒
  • NLBWMon(Netlink Bandwidth Monitor,网络带宽监控)

调整Root分区大小为512M

进入:

1
Target Images  --->

找到:

1
(160) Root filesystem partition size (in MiB)

将:

1
160

修改为:

1
512

最终对应配置为:

1
CONFIG_TARGET_ROOTFS_PARTSIZE=512

如果插件较多,也可以根据需要调整为 1024 MiB。

修改网关地址(可选)

1
2
sed -i "s/192\.168\.[0-9]*\.[0-9]*/10.0.0.1/g" $(find ./feeds/luci/modules/luci-mod-system/ -type f -name "flash.js") && \
sed -i "s/192\.168\.[0-9]*\.[0-9]*/10.0.0.1/g" ./package/base-files/files/bin/config_generate

下载dl库

1
make -j8 download V=s

编译固件

1
make FORCE_UNSAFE_CONFIGURE=1 -j1 V=s

延伸阅读

使用 Hugo 构建
主题 Stack 由 Jimmy 设计