OpenWrt 编译入门(小白版)

发布时间:2023年12月29日

编译环境

示例编译所用系统为 Ubuntu 22.04,信息如下

Ubuntu Version

编译时由于网络问题,部分软件包可能出现下载问题,还请自备网络工具或尝试重新运行命令

编译步骤

下图为官网指示

在这里插入图片描述

编译环境设置(Build system setup

这里根据我选择的系统 Ubuntu22.04 ,终端输入如下命令

Debian / Ubuntu

Modernized set for Ubuntu 22.04 for OpenWrt master, 22.03 and 21.02 branches that do not need python2.7 any more. Python3 is installed by default in Ubuntu 22.04.

sudo apt update
sudo apt install build-essential clang flex bison g++ gawk \
gcc-multilib g++-multilib gettext git libncurses-dev libssl-dev \
python3-distutils rsync unzip zlib1g-dev file wget qemu qemu-utils

Older advice: (此步骤为比较旧的建议,可以不用输入)

sudo apt update
sudo apt install build-essential ccache ecj fastjar file g++ gawk \
gettext git java-propose-classpath libelf-dev libncurses5-dev \
libncursesw5-dev libssl-dev python python2.7-dev python3 unzip wget \
python3-distutils python3-setuptools python3-dev rsync subversion \
swig time xsltproc zlib1g-dev qemu qemu-utils

编译系统(Build system usage

  • 下载源代码(Downloading sources)

Clone the Git repository using the following command.

git clone https://git.openwrt.org/openwrt/openwrt.git [<buildroot>]

Possible issues:

  • -bash: git: command not found - verify your build system setup.
  • fatal: destination path 'openwrt' already exists and is not an empty directory. - remove/rename the buildroot directory.

其中 [] 替换为你的编译目录,比如我的为 ~/tmp/build/openwrt/

  • 更新源代码(Updating sources)

:!: Sources in development branch change frequently. It is recommended that you work with the latest sources.

cd [<buildroot>]
git pull

其中 [] 替换为你的编译目录,比如我的为 ~/tmp/build/openwrt/

在这里插入图片描述

  • 选择一个指定的版本(Select a specific code revision)
git branch -a
git tag
git checkout v23.05.2

因为现在我用的最新版本 v23.05.2,所以就切换为 git checkout v23.05.2

  • 更新 Feed(Updating feeds)
./scripts/feeds update -a
./scripts/feeds install -a

因为网络环境的问题,建议重复执行上面的命令直到能够看到完全正常为止,正常如下

重复运行 ./scripts/feeds update -a 直到显示如下

shenyanwu@shenyanwu:~/tmp/build/openwrt$ ./scripts/feeds update -a
Updating feed 'packages' from 'https://git.openwrt.org/feed/packages.git^8e3a1824645f5e73ec44c897ac0755c53fb4a1f8' ...
Updating feed 'luci' from 'https://git.openwrt.org/project/luci.git^7739e9f5b03b830f51d53c384be4baef95054cb3' ...
Updating feed 'routing' from 'https://git.openwrt.org/feed/routing.git^83ef3784a9092cfd0a900cc28e2ed4e13671d667' ...
Updating feed 'telephony' from 'https://git.openwrt.org/feed/telephony.git^9746ae8f964e18f04b64fbe1956366954ff223f8' ...
Create index file './feeds/packages.index' 
Create index file './feeds/luci.index' 
Create index file './feeds/routing.index' 
Create index file './feeds/telephony.index' 
shenyanwu@shenyanwu:~/tmp/build/openwrt$

重复运行 ./scripts/feeds install -a 直到显示如下

shenyanwu@shenyanwu:~/tmp/build/openwrt$ ./scripts/feeds install -a
Collecting package info: package/feeds/packages/udp-broadcast-relay-redux-openwr
Collecting package info: done
Installing all packages from feed packages.
Installing all packages from feed luci.
Installing all packages from feed routing.
Installing all packages from feed telephony.
shenyanwu@shenyanwu:~/tmp/build/openwrt$ 

参考官方的编译配置信息(Using official build config)

wget https://downloads.openwrt.org/releases/23.05.2/targets/x86/64/config.buildinfo -O .config

我的版本是 23.05.2x86/64 可以在官方的下载目录去找到与你设备芯片对应的配置文件 官方页面传送门

编译镜像配置(Image configuration)

make menuconfig

运行此命令后等待片刻则会出现配置选择,通过上下方向键 进行选择,回车键 ? 进入,空格键 Space 选择,其中选项前面的 <M> 表示编译为独立的包,即类似安装包,不会在编译好的系统中存在,需要另外安装, <*> 表示编译为内建包,即相当于直接编译至系统内部,不需要另外再安装,连续按下两次退出键 ESC 返回上一级目录,最后保存好的配置文件将会在当前的 OpenWrt 目录下生成一个名为 .config 的隐藏文件

在这里插入图片描述

此处提供一些基于官方编译配置的简单更改,根据的你目标机器的芯片选择前三项,第四项选择镜像格式,如果空间足够,可以考虑适当增加第四项 Target Images -> Kernel partition sizeRoot filesystem partition size

在这里插入图片描述

LuCI -> Modules -> Translations -> Chinese Simplified (zh_Hans) 为 LuCI 界面增加中文语言


在编译命令之前,可以运行一个检查命令,用来检查你的依赖项,包的一些先决条件等是否满足要求,还会从中删除过时的项目,例如对不存在的软件包或配置选项的引用

make defconfig

我的运行如下图所示:

shenyanwu@shenyanwu:~/tmp/build/openwrt$ make defconfig
tmp/.config-package.in:33826:error: recursive dependency detected!
tmp/.config-package.in:33826:	symbol PACKAGE_python3-pymysql depends on PYTHON3_PYMYSQL_SHA_PASSWORD_SUPPORT
feeds/packages/lang/python/pymysql/Config.in:4:	symbol PYTHON3_PYMYSQL_SHA_PASSWORD_SUPPORT depends on PACKAGE_python3-pymysql
For a resolution refer to Documentation/kbuild/kconfig-language.rst
subsection "Kconfig recursive dependency limitations"

#
# configuration written to .config
#
shenyanwu@shenyanwu:~/tmp/build/openwrt$

此处检测到发生了一个依赖错误,通过查询 OpenWrt 官方 Github 的 issues,找到解决方案,链接 issues #22668

问题在于 feeds/packages/lang/python/pymysql 路径下 Makefile 里面的依赖冲突,该 issues 给出了解决方案,对 Makefile 文件修改如下,其中段落前面的 - 代表需要删除的部分, + 代表需要增添的部分

 #
 # Copyright (C) Alexandru Ardelean <ardeleanalex@gmail.com>
 #
 # This is free software, licensed under the GNU General Public License v2.
 # See /LICENSE for more information.
 #

 include $(TOPDIR)/rules.mk

 PKG_NAME:=pymysql
 PKG_VERSION:=1.0.2
-PKG_RELEASE:=1
+PKG_RELEASE:=2

 PYPI_NAME:=PyMySQL
 PKG_HASH:=816927a350f38d56072aeca5dfb10221fe1dc653745853d30a216637f5d7ad36
 PKG_MAINTAINER:=Alexandru Ardelean <ardeleanalex@gmail.com>
 PKG_LICENSE:=MIT
 PKG_LICENSE_FILES:=LICENSE
 PKG_CONFIG_DEPENDS:=CONFIG_PYTHON3_PYMYSQL_SHA_PASSWORD_SUPPORT
 include ../pypi.mk
 include $(INCLUDE_DIR)/package.mk
 include ../python3-package.mk

-define Package/python3-pymysql
+define Package/python3-pymysql/Default
   SUBMENU:=Python
   SECTION:=lang
   CATEGORY:=Languages
   TITLE:=Pure Python MySQL Client
   URL:=https://pymysql.readthedocs.io/
-  DEPENDS:=+python3 +PYTHON3_PYMYSQL_SHA_PASSWORD_SUPPORT:python3-cryptography
 endef

-define Package/python3-pymysql/config
-  source "$(SOURCE)/Config.in"
+define Package/python3-pymysql
+$(call Package/python3-pymysql/Default)
+  DEPENDS:=+python3
 endef

 define Package/python3-pymysql/description
   This package contains a pure-Python MySQL client library, based on PEP 249.
 endef

+define Package/python3-pymysql-sha-pwd
+$(call Package/python3-pymysql/Default)
+  TITLE+=w/ SHA256 password auth
+  DEPENDS:=+python3-pymysql $(RUST_ARCH_DEPENDS) +PACKAGE_python3-pymysql-sha-pwd:python3-cryptography
+endef

+define Package/python3-pymysql-sha-pwd/description
+  This is a meta-package installing python3-pymysql and python3-cryptography
+  packages to be able to use pymysql with “sha256_password” or
+  “caching_sha2_password” for authentication.
+endef
+
+define Package/python3-pymysql-sha-pwd/install
+  true
+endef

 $(eval $(call Py3Package,python3-pymysql))
 $(eval $(call BuildPackage,python3-pymysql))
 $(eval $(call BuildPackage,python3-pymysql-src))
+$(eval $(call BuildPackage,python3-pymysql-sha-pwd))

修改后的完整文件如下

#
# Copyright (C) Alexandru Ardelean <ardeleanalex@gmail.com>
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=pymysql
PKG_VERSION:=1.0.2
PKG_RELEASE:=2

PYPI_NAME:=PyMySQL
PKG_HASH:=816927a350f38d56072aeca5dfb10221fe1dc653745853d30a216637f5d7ad36

PKG_MAINTAINER:=Alexandru Ardelean <ardeleanalex@gmail.com>
PKG_LICENSE:=MIT
PKG_LICENSE_FILES:=LICENSE

PKG_CONFIG_DEPENDS:=CONFIG_PYTHON3_PYMYSQL_SHA_PASSWORD_SUPPORT

include ../pypi.mk
include $(INCLUDE_DIR)/package.mk
include ../python3-package.mk

define Package/python3-pymysql/Default
  SUBMENU:=Python
  SECTION:=lang
  CATEGORY:=Languages
  TITLE:=Pure Python MySQL Client
  URL:=https://pymysql.readthedocs.io/
endef

define Package/python3-pymysql
$(call Package/python3-pymysql/Default)
  DEPENDS:=+python3
endef

define Package/python3-pymysql/description
  This package contains a pure-Python MySQL client library, based on PEP 249.
endef

define Package/python3-pymysql-sha-pwd
$(call Package/python3-pymysql/Default)
  TITLE+=w/ SHA256 password auth
  DEPENDS:=+python3-pymysql $(RUST_ARCH_DEPENDS) +PACKAGE_python3-pymysql-sha-pwd:python3-cryptography
endef

define Package/python3-pymysql-sha-pwd/description
  This is a meta-package installing python3-pymysql and python3-cryptography
  packages to be able to use pymysql with “sha256_password” or
  “caching_sha2_password” for authentication.
endef

define Package/python3-pymysql-sha-pwd/install
  true
endef

$(eval $(call Py3Package,python3-pymysql))
$(eval $(call BuildPackage,python3-pymysql))
$(eval $(call BuildPackage,python3-pymysql-src))
$(eval $(call BuildPackage,python3-pymysql-sha-pwd))

修改完成后重新运行 make defconfig,结果如下

shenyanwu@shenyanwu:~/tmp/build/openwrt$ make defconfig
Collecting package info: done
#
# configuration written to .config
#
shenyanwu@shenyanwu:~/tmp/build/openwrt$

下载相关源以及多核编译的依赖(Download sources and multi core compile)

make download

此命令将会预先下载你依赖的所有源代码,同时开启支持多核编译的功能,这样你才可以成功编译,并且使用多核编译提升编译速度

编译镜像(Building images)

make -j $(($(nproc)+1))

这个命令属于多核编译开启,使用你所有的核心进行编译,当然如果你想使用指定核心数量编译,请在 -j 命令后指定核心数,例如单核编译

make -j1

当然你要是不放心之前下载的相关源以及多核编译的依赖,你可以执行下面的代码,在编译前重新下载一次,然后自动开始编译

make -j $(nproc) download world

你可能在别的教程里面见过在命令后面还有 V=sc 或者 V=s,这是打开日志显示,在你编译出错排错时,会在终端进行显示错误信息,对于排除错误很有用,比如以下命令会输出很多日志提供错误帮助

make -j1 download world V=s

建议在第一次编译时,如果你不确定你的配置项都满足编译要求,可以使用单核,日志输出编译 make -j1 V=s 来确保出错后进行排除。比如我编译配置中选择了编译 vmdk 格式的镜像文件,产生了以下错误日志

WARNING: Install qemu-img to create VDI/VMDK images
make[5]: *** [Makefile:158: /home/shenyanwu/tmp/build/openwrt/build_dir/target-x86_64_musl/linux-x86_64/tmp/openwrt-23.05.2-x86-64-generic-squashfs-combined.vmdk] Error 1
make[5]: Leaving directory '/home/shenyanwu/tmp/build/openwrt/target/linux/x86/image'
make[4]: *** [Makefile:24: install] Error 2
make[4]: Leaving directory '/home/shenyanwu/tmp/build/openwrt/target/linux/x86'
make[3]: *** [Makefile:11: install] Error 2
make[3]: Leaving directory '/home/shenyanwu/tmp/build/openwrt/target/linux'
time: target/linux/install#86.46#47.88#127.74
    ERROR: target/linux failed to build.
make[2]: *** [target/Makefile:30: target/linux/install] Error 1
make[2]: Leaving directory '/home/shenyanwu/tmp/build/openwrt'
make[1]: *** [target/Makefile:24: /home/shenyanwu/tmp/build/openwrt/staging_dir/target-x86_64_musl/stamp/.target_install] Error 2
make[1]: Leaving directory '/home/shenyanwu/tmp/build/openwrt'
make: *** [/home/shenyanwu/tmp/build/openwrt/include/toplevel.mk:232: world] Error 2

可以看出是由于 WARNING: Install qemu-img to create VDI/VMDK images 导致了以下输出,应该是选择了 vmdk 格式的镜像后,通过 qemu-img 这个文件进行转换格式,但是并未安装 qemu-img,因此运行命令(此命令我已添加在了编译环境设置中

sudo apt install qemu qemu-utils

然后重新开始执行编译即可,或者你在 make menuconfig 中去除掉 image vmdk 格式的镜像文件选项,在用编译之后的 img 镜像进行转换格式即可

编译完成后,可以发现镜像在 openwrt/bin/targets 目录下,根据我的配置文件,生成以下镜像压缩文件,选择一个合适的使用即可

在这里插入图片描述

文章来源:https://blog.csdn.net/qq_44730817/article/details/135258664
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。