OpenWRT编译安装并制作docker镜像

前提准备

  1. 编译环境: Debian 11及以上 或者 Ubuntu LTS(长期支持)版本
  2. 使用非root用后进行编译
  3. 国内环境需要提前准备好科学上网

环境说明

名称 版本 备注
Ubuntu 22.10 操作系统
LEDE R22.12.1 OpenWRT版本

安装

1. 安装编译环境

# 1. 安装依赖包
apt update -y

apt full-upgrade -y

apt 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 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-pyelftools \
libpython3-dev qemu-utils rsync scons squashfs-tools subversion swig texinfo uglifyjs upx-ucl unzip \
vim wget xmlto xxd zlib1g-dev python3-setuptools gzip 

apt autoremove --purge

apt clean

# 2. 安装docker
# 可选,如果只是编译固件,不制作docker镜像,可跳过
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

add-apt-repository  -y  "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \
  $(lsb_release -cs) \
  stable"

apt-get update

apt-get install -y docker-ce=5:20.10.24~3-0~ubuntu-kinetic docker-ce-cli=5:20.10.24~3-0~ubuntu-kinetic

# 3. 创建编译使用的普通用户
useradd -m -s /bin/bash openwrt

# 切换用户
su - openwrt

2. 编译安装

# 1. clone LEDE 源码
git clone https://github.com/coolsnowwolf/lede

# R22.12.1 版本是 20221001分支的最新代码
cd lede

git checkout 20221001

# 或者直接下载对应版本的源码
# wget https://github.com/coolsnowwolf/lede/archive/refs/tags/20230609.zip
wget https://github.com/coolsnowwolf/lede/archive/refs/tags/20221001.zip
2.1 配置软件源
# 1. 配置 OpenWRTY 软件源
vim feeds.conf.default 
# 添加软件源(打开下方仓库链接可以查看包含的插件列表,一般选前两个源就行)
src-git kenzo https://github.com/kenzok8/openwrt-packages
src-git small https://github.com/kenzok8/small
# src-git haibo https://github.com/haiibo/openwrt-packages
# src-git liuran001 https://github.com/liuran001/openwrt-packages

20230817135206.png

2.2 下载 OpenAppFilter 插件
# 1. clone OpenAppFilter 源码
# OpenAppFilter: App过滤插件
git clone https://github.com/destan19/OpenAppFilter.git package/OpenAppFilter
2.3 更新feeds并安装插件
./scripts/feeds update -a
./scripts/feeds install -a
2.4 配置openWRT
# 修改默认IP为 10.31.0.253, 按照实际情况修改
sed -i 's/192.168.1.1/10.31.0.253/g' package/base-files/files/bin/config_generate

# 可选, 修改默认主机名
OPENWRT_HOSTNAME='自定义主机名'
sed -i '/uci commit system/i\uci set system.@system[0].hostname='$OPENWRT_HOSTNAME'' package/lean/default-settings/files/zzz-default-settings

# 可选, 加入编译者信息
OPENWRT_BUILD_MES="XXX build $(TZ=UTC-8 date "+%Y.%m.%d") @ OpenWrt "
sed -i "s/OpenWrt /${OPENWRT_BUILD_MES}/g" package/lean/default-settings/files/zzz-default-settings

# 修改默认主题为 argon
sed -i "s/luci-theme-bootstrap/luci-theme-argon/g" feeds/luci/collections/luci/Makefile

# 配置编译
make menuconfig

编译菜单简单说明:

Target System (x86) --> x86                          # 选择 CPU 类型, 软路由选择 x86,硬路由根据型号厂家自行选择
Subtarget (x86_64)  --> x86_64                       # CPU 子选项
Target Profile (Generic x86/64) --> Generic x86/64   # 厂家具体型号

Target Images -->                # 设置镜像编译的格式(默认,squashfs)
              --> tar.gz         # 打包 rootFS, 后面构建dockerFile需要使用
              --> ext4           # 可选,ext4 文件系统
              --> squashfs       # squashfs 文件系统
              --> Build GRUB images(Linux x86 or x86_64 host only)       # 普通BIOS固件
              --> Build GRUB EFI images(Linux x86 or x86_64 host only)   # 支持 UEFI 启动的BIOS固件
              --> Build PVE/KVM image files (QCOW2)     # 按需选择, 构建 PVE 和 KVM 使用的虚拟磁盘
              --> Build VirtualBox image files (VDI)    # 按需选择, 构建 VirtualBox 使用的虚拟磁盘
              --> Build VMWare image files (VMDK)       # 按需选择, 构建 VMWare 使用的虚拟磁盘
              --> Gzip images                           # 是否使用 gzip 将输出的镜像进行压缩
# 添加较多插件时,为了避免空间不足,建议修改下面两项默认大小(x86/64)
Target Images --> (16) Kernel partition size (in MB) # 修改boot分区大小, 默认是16,建议修改为256、512
Target Images --> (400) Root filesystem partition size (in MB) # 修改根分区大小, 默认是400,建议修改

20230817145933.png

Extra packages --> ipv6helper             # 开启 IPv6 支持
Network --> odhcp6c                       # 无需操作, 开启 ipv6helper 后,会自动选择, 这里主要是进行确认
Network --> odhcpd-ipv6only               # 无需操作, 开启 ipv6helper 后,会自动选择, 这里主要是进行确认
LuCI --> Protocols --> luci-proto-ipv6    # 无需操作, 开启 ipv6helper 后,会自动选择, 这里主要是进行确认
LuCI --> Protocols --> luci-proto-ppp     # 无需操作, 开启 ipv6helper 后,会自动选择, 这里主要是进行确认

# 开启适用于 VMware 的 VMware Tools
# 按需操作,如果确认在 VMware 虚拟机上使用
Utilities --> open-vm-tools
Utilities --> open-vm-tools-fuse

# 选择插件, 根据需要选择
# 插件中文说明对照表:
#    https://www.right.com.cn/forum/thread-3682029-1-1.html
#    https://www.right.com.cn/forum/thread-344825-1-1.html
LuCI --> Applications

# 选择主题
LuCI --> Themes # 选择喜欢的主题,可以选多个

# 其他选项一般不需要调整,如感兴趣可以自行了解
2.5 安装
# 1. 预下载编译所需的软件包
make download -j8

# 编译固件
# 固件保存在 bin/target 目录下
make V=s -j8

制作 docker 镜像

# 注意:
#    1. 使用 root 用户操作, 或者给 openwrt 用户拥有 docker 操作权限
#    2. 使用docker运行的OpenWRT只适用于旁路用

# 1. 创建存放 dockerfile 文件的目录
mkdir /data/docker/dockerfile/openwrt

# 2. copy 文件系统文件至dockerfile目录下
cp -a bin/targets/x86/64/openwrt-x86-64-generic-rootfs.tar.gz /data/docker/dockerfile/openwrt

# 3. 编写dockerfile
vim /data/docker/dockerfile/openwrt/Dockerfile
FROM scratch

ADD openwrt-x86-64-generic-rootfs.tar.gz /

USER root

CMD ["/sbin/init"]
# 4. 构建
docker build -t openwrt:22.12.1

# 5. 运行
docker run -d --network host --name openwrt openwrt:22.12.1

# 6. 访问
# curl http://IP:80. root/password
上一篇
下一篇