yocto-第33篇-如何修改linux内核源码和添加补丁文件

By: fu linux
E-mail: fulinux@sina.com
Blog: https://blog.csdn.net/fulinus
喜欢的盆友欢迎点赞和订阅!
你的喜欢就是我写作的动力!

使用devtool工具修改linux内核

虽然我们可以用手动的方式修改linux内核源码并生成补丁,然后放到recipe-kernel/linux-yocto/目录中,并修改bb文件,但是这样做有些麻烦,推荐大家还是使用devtool工具来修改内核。
后面我也会抽时间使用手动的方式来修改内核,因为有些老的yocto项目还不支持devtool或者devtool工具支持的不好。

创建自己的layer

在使用devtool工具之前我们首先创建自己的layer, 学习过 yocto-第4篇-创建自己的layer 的盆友,可以不用重复创建meta-mylayer了,可以跳过此节。
参考过程:

为什么要为kernel创建自己的layer

我们前一篇讲到linux kernel的recipe目录有两个,分别是:

meta/recipes-kernel/
meta-yocto-bsp/recipes-kernel/
  • 1
  • 2

这个是个什么道理呢?

这是因为meta/recipes-kernel中放的是linux通用的recipe,而一些针对特定平台的recipe可以基于通用的recipe来添加一些append文件来实现。meta-yocto-bsp/recipes-kernel/目录中放的就是append。

考虑到这两个目录是系统原生的东西,我们不想去污染他们,不想在这两个目录中去添加什么文件或者修改bb文件。因此我们就需要在自己的meta-mylayer中添加recipes-kernel/和append文件。

修改linux源码

参考过程:

poky]$ source oe-init-build-env 
build]$ devtool modify linux-yocto
  • 1
  • 2

上面的命令会在poky/build/workspace/sources/linux-yocto目录下放置linux内核源代码。下面修改一个文件:

build]$ cd workspace/sources/linux-yocto/
linux-yocto]$ vim init/calibrate.c +275
#如果不熟悉vim的用法,也可以使用自己熟悉的编辑器修改
linux-yocto]$ git diff init/calibrate.c
diff --git a/init/calibrate.c b/init/calibrate.c
index f383127..07201cd 100644
--- a/init/calibrate.c
+++ b/init/calibrate.c
@@ -278,6 +278,8 @@ void calibrate_delay(void)
        static bool printed;
        int this_cpu = smp_processor_id();
 
+       printk(KERN_INFO"***HELLO YOCTO KERNEL***\n");
+
        if (per_cpu(cpu_loops_per_jiffy, this_cpu)) {
                lpj = per_cpu(cpu_loops_per_jiffy, this_cpu);
                if (!printed)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

上面我们就加了一行printk打印,可以在系统启动过程中打印该行,然后后面我们可以使用demsg命令查看到。

编译和打包镜像

修改完后我们就开始编译:

linux-yocto]$ cd -
/home/peeta/poky/build
build]$ devtool build linux-yocto
  • 1
  • 2
  • 3

通常我们,单编某个recipe的时候,是没有整合到镜像文件中的,此时还需要全编译,尤其像linux内核这个recipe,
很多其他项目都会依赖内核,内核有改动,其他的强依赖的recipe也可能需要编译。

build]$ bitbake core-image-sato
  • 1

运行验证

运行如下命令:

build]$ runqemu qemux86-64
  • 1

重启一个窗口:

$ ssh root@192.168.7.2
root@qemux86-64:~# dmesg | grep -i hello      
[    0.000764] ***HELLO YOCTO KERNEL***
  • 1
  • 2
  • 3

如上打印是有的。

保存自己的成果

此时我们的工作还没有结束哦,需要将相关的改动放到自己的layer中,过程如下:

build]$ cd workspace/sources/linux-yocto/
linux-yocto]$ git status
linux-yocto]$ git add -u
linux-yocto]$ git commit -m 'calibrate: Add printk example'
  • 1
  • 2
  • 3
  • 4

OK,将新的改动通过下面的命令添加到meta-mylayer中,如下:

poky]$ cd -
/home/peeta/poky/build
build]$ devtool finish linux-yocto ../meta-mylayer
  • 1
  • 2
  • 3

查看结果

build]$ cd ../meta-mylayer/recipes-kernel/linux/
linux]$ cat linux-yocto/0001-calibrate-Add-printk-example.patch 
From b4b8b853699d42488073fef41d06e594ecda6162 Mon Sep 17 00:00:00 2001
Date: Mon, 4 Jan 2021 20:23:09 +0800
Subject: [PATCH] calibrate: Add printk example

---
 init/calibrate.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/init/calibrate.c b/init/calibrate.c
index f383127..07201cd 100644
--- a/init/calibrate.c
+++ b/init/calibrate.c
@@ -278,6 +278,8 @@ void calibrate_delay(void)
        static bool printed;
        int this_cpu = smp_processor_id();
 
+       printk(KERN_INFO"***HELLO YOCTO KERNEL***\n");
+
        if (per_cpu(cpu_loops_per_jiffy, this_cpu)) {
                lpj = per_cpu(cpu_loops_per_jiffy, this_cpu);
                if (!printed)

linux]$ cat linux-yocto_%.bbappend #查看append文件
FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"

SRC_URI += "file://0001-calibrate-Add-printk-example.patch"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

OK我们的工作到这里就大功告成了~