OpenWrt 编译 | 一种使用 git submodule 方式实现一键更新多个外部软件包

文章目录

一、问题背景

当我们在自定义编译 OpenWrt 时,有时候我们需要导入一些外部的软件包,有些软件包的 git 仓库地址是符合 feeds 规则的,例如 openwrt/packages 的仓库https://github.com/openwrt/packages,此时可以直接编辑 feeds.conf 文件,添加

shell 复制代码
src-git packages https://github.com/openwrt/packages.git

此时再执行 ./scripts/feeds update; ./scripts/feeds install -a 命令即可更新和安装 openwrt/packages 的仓库的软件包。

但是,有些仓库的地址是直接提供源码的,非 feeds 规则,例如

shell 复制代码
grep: feeds/argon/Makefile:#: No such file or directory
grep: call: No such file or directory
grep: BuildPackage: No such file or directory
grep: OpenWrt: No such file or directory
grep: buildroot: No such file or directory
grep: signature/Makefile: No such file or directory
feeds/argon.tmp/info/.files-packageinfo.mk:1: *** target pattern contains no '%'.  Stop.

因此,当存在多个这样的仓库的时候,每个仓库只能手动更新,在编译的时候较为不方便。而当前发 OpenWrtfeeds 管理暂不支持直接更新目录,因此需要另外寻找一个方式,实现方便更新多个仓库的目的。

本文将详细介绍一种使用 git submodule 方式实现一键更新多个外部软件包的方法。

二、新建Git仓库

由于放在 package/ 目录下的软件包,在执行 ./scripts/feeds update -a 命令的时候,自动被搜集作为软件包使用。因此首先,我们需要在 package/ 目录下建立一个专门存放外部仓库的目录,例如起名为 custom。随后我们通过 cd 命令切到此目录,随后执行以下命令创建新仓库

shell 复制代码
git init

随后在此仓库通过 git submodule add git路径 路径名 命令创建 submodule,例如添加两个子仓库

shell 复制代码
git submodule add https://github.com/jerrykuku/luci-app-argon-config.git luci-app-argon-config
git submodule add https://github.com/jerrykuku/luci-theme-argon.git luci-theme-argon/

最后执行更新仓库命令

shell 复制代码
git submodule update --remote --recursive

此时会有如下输出:

shell 复制代码
Cloning into 'package/custom/luci-app-argon-config'...
Cloning into 'package/custom/luci-theme-argon'...
Submodule path 'luci-app-argon-config': checked out 'e043d863574e8f2e77f2b4d8a0d92698e05f77c5'
Submodule path 'luci-theme-argon': checked out '8793e6330d4613e55a2fe8adaf76a749a52fda49'

以后每次只要在 package/custom 目录下执行 git submodule update --remote --recursive 命令即可更新所有的子模块的仓库,即可更新软件包

三、编写命令一键更新

如果每次编译之前都进 package/custom 目录下执行 git submodule update --remote --recursive 命令去更新软件包,也有所麻烦,因此可以将此转换成一个脚本命令,并使用括号 ( ) 创建子 Shell,子 Shell 退出后自动返回原目录,无需手动切换,可以避免因为 cd 命令执行之后,影响后续的命令执行。此命令如下:

shell 复制代码
(cd package/custom && git submodule update --remote --recursive)

将此命令合并到 feeds 更新中,即为

bash 复制代码
./scripts/feeds update -a && (cd package/custom && git submodule update --remote --recursive) && ./scripts/feeds install -a 

四、验证结果

如何验证所编写脚本是否正常呢?我们可以在某个子仓库下执行 git reset --hard hash 将子仓库回退到历史版本,之后在 OpenWrt 的主目录下执行 (cd package/custom && git submodule update --remote --recursive),观察其输出,是否成功更新了子仓库。

此时,会有类似以下的打印,即表示脚本执行正常。

shell 复制代码
Submodule path 'luci-app-argon-config': checked out 'e043d863574e8f2e77f2b4d8a0d92698e05f77c5'
相关推荐
闲云一鹤1 天前
Git LFS 扫盲教程 - 你不会还在用 Git 管理大文件吧?
前端·git·前端工程化
vibecoding日记4 天前
为什么我就想要「线性历史 + Signed Commits」,GitHub 却把我当猴耍 🤬🎙️
git·编程工具
程序员小崔日记4 天前
如何将代码轻松上传到 Gitee?Git 使用全攻略!
git·gitee·上传
Bigger5 天前
为什么你的 Git 提交需要签名?—— Git Commit Signing 完全指南
git·开源·github
DianSan_ERP6 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
呉師傅6 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
一个人旅程~6 天前
如何用命令行把win10/win11设置为长期暂停更新?
linux·windows·经验分享·电脑
2501_946205526 天前
晶圆机器人双臂怎么选型?适配2-12寸晶圆的末端效应器有哪些?
服务器·网络·机器人
linux kernel6 天前
第七部分:高级IO
服务器·网络