openwrt - patch¶
https://openwrt.org/docs/guide-developer/toolchain/use-patches-with-buildsystem
由于很多第三方package或者linux kernel都不是我们写的,直接移植可能会产生一些平台不兼容的问题,为了能够移植成功,往往需要给package或者内核进行打补丁.
[[toc]]
前期准备¶
- quilt环境配置配置
需要在自己的家目录下配置一下quilt的环境变量
cat << EOF > ~/.quiltrc
QUILT_DIFF_ARGS="--no-timestamps --no-index -p ab --color=auto"
QUILT_REFRESH_ARGS="--no-timestamps --no-index -p ab"
QUILT_SERIES_ARGS="--color=auto"
QUILT_PATCH_OPTS="--unified"
QUILT_DIFF_OPTS="-p"
EDITOR="vim"
EOF
EDITOR
指定你喜欢用的编辑器
新增补丁文件¶
- Step1:先clean(清除已经编译的文件),再prepare(prepare步骤会直接下载好代码并将已经存在的patches补丁打到代码中去)
- Step 2:切换当前目录到package的build_dir路径中
cd build_dir/target-*/example-*
# Note: It can happen that you need to go one level lower as the source is extracted in build_dir/target-*/BUILD_VARIANT/example-*. This happens when multiple build variants of a package are defined in the Makefile.
- Step 3: Apply existing patches using
quilt push
- Step 4: 新建或者导入一个已经存在的补丁文件
# 导入一个已经存在的补丁文件
quilt import /path/to/010-main_code_fix.patch
# 如果没有补丁文件,需要自己改代码,可以新建一个补丁文件
quilt new 010-main_code_fix.patch
补丁命名需要注意一下:
- The name should start with a number, followed by a hyphen and a very short description of what is changed
- The chosen number should be higher than any existing patch - use quilt series to see the list of patches
- The patch file name should be short but descriptive
- Step 5: 编辑代码
- Step 6: 代码改完后,运行该命令查看所改动的地方
- Step 7: 如果没有问题运行quilt refresh更新该patch
- Step 8: 切换回顶层目录
- Step 9: 移动该patch到package的patches目录下
- Step 10: 重新编译
编辑已有的补丁¶
- Step 1 & Step 2: 看上面
- Step 3: 查看已有的补丁
- Step 4: 切换到你要编辑的补丁
- When passing a valid patch filename to push, quilt will only apply the series until it reaches the specified patch.
- If unsure, use quilt series to see existing patches and quilt top to see the current position.
- If the current position is beyound the desired patch, use quilt pop to remove patches in the reverse order.
- You can use the “force” push option (e.g.
quilt push -f 010-main_code_fix.patch
) to interactively apply a broken (i.e. has rejects) patch.
Step 5~Step 10: 同上面的Step 5~Step 10
新增或者编辑内核补丁¶
To prepare the kernel tree, use:
The source tree is in the target-architecture subdirectory (potentially with a subarch):
The process for modifying kernel patches is the same as for packages, only the make targets and directories differ.
::: warning For the kernel, an additional subdirectory for patches is used, generic/
contains patches common to all architectures and platform/
contains patches specific to the current target (the latter are found in the target/linux/<arch_name>/patches-*
folder in the source). :::
So, you should first choose where the patch belongs, this is for patches in generic folder:
patches in platform folder instead should be made with
And in case you are editing files, it works like for packages but as you saw with the command to make the new patch you need to add the folder name before the name of the patch you are working on.
After you made your changes and saved your patch with quilt refresh
, you can go back to top level directory:
And then you can move the patches you created in these temporary directories back to main source tree:
You can also simply copy the patch files over from the local work folder of quilt here (target/linux/\<arch_name>/\<linux-version>/patches/generic or target/linux/\<arch_name>/\<linux-version>/patches) to the source folder (target/linux/generic/patches-* or target/linux/\<arch_name>/patches-*)
::: warning the process might fail with an error that looks like this bash: line 3:{openwrt directory}/staging_dir/host/bin/usign: No such file or directory :::
and you can safely ignore it, as it just means that you haven't run a compile yet so the “usign” tool used to sign packages does not yet exist. Since you are just working with source, it's irrelevant.
::: warning Patches should be named with the correct prefix, platform/000-abc.patch or generic/000-abc.patch. If not the update may not work correctly.) :::
Afterwards, if we want to verify whether our patch is applied or not, we can go to the top level directory with
and preparing again the linux folder for some modification with
During this process all the applied patched will be shown, ours being among them, preceeded by generic or platform depending on what directory we placed the patch. Another way of retrieving the applied patches is through
as explained on the previous sections, after having made make target/linux/{clean,prepare} …