How to build linux kernel for ubuntu


前言

一般而言我们更新ubuntu的linux kernel只需要apt install linux-headers-5.4.0-123-generic linux-modules-5.4.0-123-generic linux-image-5.4.0-123-generic linux-modules-extra-5.4.0-123-generic即可,但是有时候官方的库可能缺少我们想要的config,所以需要自己编译对应的linux kernel。比如,笔者本人缺少的就是CONFIG_DMA_CMA这个config,需要注意的是这个config在5.11的kernel上已经木有了。


一、Ubuntu官方文档

可用的参考文档主要下面两个:
Kernel/Compile
BuildYourOwnKernel
我主要是参考后者。

二、Environment

系统依赖

代码如下(示例):

sudo apt-get build-dep linux linux-image-$(uname -r)
sudo apt-get install libncurses-dev gawk flex bison openssl libssl-dev dkms libelf-dev libudev-dev libpci-dev libiberty-dev autoconf
sudo apt install dwarves

下载代码

拉取官方代码:

git clone git://kernel.ubuntu.com/ubuntu/ubuntu-<release codename>.git
# or
git clone git://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/bionic

release codename可以通过下面方式获取

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.6 LTS
Release:	18.04
Codename:	bionic

ubuntu tag

 git tag | grep 5.4
Ubuntu-4.15.0-45.48
Ubuntu-hwe-5.4-5.4.0-100.113_18.04.1
Ubuntu-hwe-5.4-5.4.0-102.115_18.04.1
Ubuntu-hwe-5.4-5.4.0-104.118_18.04.1
Ubuntu-hwe-5.4-5.4.0-105.119_18.04.1
Ubuntu-hwe-5.4-5.4.0-106.120_18.04.1
Ubuntu-hwe-5.4-5.4.0-107.121_18.04.1
Ubuntu-hwe-5.4-5.4.0-108.122_18.04.1
Ubuntu-hwe-5.4-5.4.0-109.123_18.04.1
Ubuntu-hwe-5.4-5.4.0-110.124_18.04.1
Ubuntu-hwe-5.4-5.4.0-112.126_18.04.1
Ubuntu-hwe-5.4-5.4.0-113.127_18.04.1
Ubuntu-hwe-5.4-5.4.0-115.129_18.04.1
Ubuntu-hwe-5.4-5.4.0-117.132_18.04.1
Ubuntu-hwe-5.4-5.4.0-120.136_18.04.1
Ubuntu-hwe-5.4-5.4.0-121.137_18.04.1

切换tag

最初我使用的是Ubuntu-hwe-5.4-5.4.0-31.35_18.04.2但是在LANG=C fakeroot debian/rules editconfigs 的时候遇见了scripts/Kconfig.include:35: compiler 'arm-linux-gnueabihf-gcc' not found的报错,一番折腾无果,切换到Ubuntu-hwe-5.4-5.4.0-123.139_18.04.1一切正常。

三、Build

Modifying the configuration

chmod a+x debian/rules
chmod a+x debian/scripts/*
chmod a+x debian/scripts/misc/*
LANG=C fakeroot debian/rules clean
LANG=C fakeroot debian/rules editconfigs # you need to go through each (Y, Exit, Y, Exit..) or get a complaint about config later

我是在最后一步增加了CONFIG_DMA_CMA这个config,但是LANG=C fakeroot debian/rules editconfigs又报错

Running config-check for all configurations ...

check-config: /tmp/tmp.Q7pNfGIXUt/CONFIGS/amd64-config.flavour.generic: loading config
check-config: /home/hygon/data/linux_kernel/ubuntu-bionic/ubuntu-bionic/debian.hwe-5.4/config/annotations loading annotations
check-config: FAIL (8 != -): CONFIG_CMA_ALIGNMENT policy<{'arm64': '8', 'armhf': '8', }>
check-config: FAIL (y != n): CONFIG_DMA_CMA policy<{'amd64': 'n', 'arm64': 'y', 'armhf': 'y', 'i386': 'n', 's390x': 'n'}> mark<ENFORCED> note<LP#1362261> note<LP:1803206>
check-config: FAIL (y != -): CONFIG_CMA_SIZE_SEL_MBYTES policy<{'arm64': 'y', 'armhf': 'y', }>
check-config: FAIL (512 != -): CONFIG_CMA_SIZE_MBYTES policy<{'arm64': '32', 'armhf': '32', }> mark<ENFORCED> note<LP:1823753>
check-config: FAIL (n != -): CONFIG_CMA_SIZE_SEL_MIN policy<{'arm64': 'n', 'armhf': 'n', }>
check-config: FAIL (n != -): CONFIG_CMA_SIZE_SEL_MAX policy<{'arm64': 'n', 'armhf': 'n', }>
check-config: FAIL (n != -): CONFIG_CMA_SIZE_SEL_PERCENTAGE policy<{'arm64': 'n', 'armhf': 'n', }>
check-config: 10446/10453 checks passed -- exit 1
check-config: /tmp/tmp.Q7pNfGIXUt/CONFIGS/amd64-config.flavour.lowlatency: loading config
check-config: /home/hygon/data/linux_kernel/ubuntu-bionic/ubuntu-bionic/debian.hwe-5.4/config/annotations loading annotations
check-config: FAIL (512 != -): CONFIG_CMA_SIZE_MBYTES policy<{'arm64': '32', 'armhf': '32', }> mark<ENFORCED> note<LP:1823753>
check-config: FAIL (n != -): CONFIG_CMA_SIZE_SEL_MIN policy<{'arm64': 'n', 'armhf': 'n', }>
check-config: FAIL (n != -): CONFIG_CMA_SIZE_SEL_MAX policy<{'arm64': 'n', 'armhf': 'n', }>
check-config: FAIL (y != -): CONFIG_CMA_SIZE_SEL_MBYTES policy<{'arm64': 'y', 'armhf': 'y', }>
check-config: FAIL (n != -): CONFIG_CMA_SIZE_SEL_PERCENTAGE policy<{'arm64': 'n', 'armhf': 'n', }>
check-config: FAIL (y != n): CONFIG_DMA_CMA policy<{'amd64': 'n', 'arm64': 'y', 'armhf': 'y', 'i386': 'n', 's390x': 'n'}> mark<ENFORCED> note<LP#1362261> note<LP:1803206>
check-config: FAIL (8 != -): CONFIG_CMA_ALIGNMENT policy<{'arm64': '8', 'armhf': '8', }>
check-config: 10446/10453 checks passed -- exit 1
check-config: /tmp/tmp.Q7pNfGIXUt/CONFIGS/armhf-config.flavour.generic: loading config
check-config: /home/hygon/data/linux_kernel/ubuntu-bionic/ubuntu-bionic/debian.hwe-5.4/config/annotations loading annotations
check-config: 10453/10453 checks passed -- exit 0
check-config: /tmp/tmp.Q7pNfGIXUt/CONFIGS/armhf-config.flavour.generic-lpae: loading config
check-config: /home/hygon/data/linux_kernel/ubuntu-bionic/ubuntu-bionic/debian.hwe-5.4/config/annotations loading annotations
check-config: 10453/10453 checks passed -- exit 0

*** ERROR: 2 config-check failures detected


WARNING: configuration operation applied only to a subset of architectures (skipped i386 arm64 ppc64el s390x)

rm -rf build

这个时候大胆点,谁报错解决谁

$ git diff debian.hwe-5.4/config/annotations
diff --git a/debian.hwe-5.4/config/annotations b/debian.hwe-5.4/config/annotations
index 6e6d65d57765..e30a7a1f25ee 100644
--- a/debian.hwe-5.4/config/annotations
+++ b/debian.hwe-5.4/config/annotations
@@ -10968,18 +10968,11 @@ CONFIG_CRC32_SARWATE                            policy<{'amd64': 'n', 'arm64': '
 CONFIG_CRC32_BIT                                policy<{'amd64': 'n', 'arm64': 'n', 'armhf': 'n', 'i386': 'n', 'ppc64el': 'n', 's390x': 'n'}>
 
 # Menu: Library routines >> DMA Contiguous Memory Allocator
-CONFIG_DMA_CMA                                  policy<{'amd64': 'n', 'arm64': 'y', 'armhf': 'y', 'i386': 'n', 's390x': 'n'}>
-CONFIG_CMA_SIZE_MBYTES                          policy<{'arm64': '32', 'armhf': '32', }>
-CONFIG_CMA_ALIGNMENT                            policy<{'arm64': '8', 'armhf': '8', }>
+CONFIG_DMA_CMA                                  policy<{'amd64': 'y', 'arm64': 'y', 'armhf': 'y', 'i386': 'n', 's390x': 'n'}>
 #
 CONFIG_DMA_CMA                                  mark<ENFORCED> note<LP#1362261> note<LP:1803206>
-CONFIG_CMA_SIZE_MBYTES                          mark<ENFORCED> note<LP:1823753>
 
 # Menu: Library routines >> DMA Contiguous Memory Allocator >> Selected region size
-CONFIG_CMA_SIZE_SEL_MBYTES                      policy<{'arm64': 'y', 'armhf': 'y', }>
-CONFIG_CMA_SIZE_SEL_PERCENTAGE                  policy<{'arm64': 'n', 'armhf': 'n', }>
-CONFIG_CMA_SIZE_SEL_MIN                         policy<{'arm64': 'n', 'armhf': 'n', }>
-CONFIG_CMA_SIZE_SEL_MAX                         policy<{'arm64': 'n', 'armhf': 'n', }>
 
 # Menu: Library routines >> Select compiled-in fonts
 CONFIG_FONTS                                    policy<{'amd64': 'y', 'arm64': 'y', 'armhf': 'y', 'i386': 'y', 'ppc64el': 'y', 's390x': 'y'}>
@@ -11047,10 +11040,6 @@ CONFIG_PERCPU_STATS                             flag<REVIEW>
 CONFIG_MEMORY_HOTPLUG_DEFAULT_ONLINE            mark<ENFORCED> note<LP:1709171 LP:1848492>
 
 # Menu: Memory Management options >> Contiguous Memory Allocator
-CONFIG_CMA                                      policy<{'amd64': 'y', 'arm64': 'y', 'armhf': 'y', 'i386': 'y', 'ppc64el': 'y', 's390x': 'y'}>
-CONFIG_CMA_DEBUG                                policy<{'amd64': 'n', 'arm64': 'n', 'armhf': 'n', 'i386': 'n', 'ppc64el': 'n', 's390x': 'n'}>
-CONFIG_CMA_DEBUGFS                              policy<{'amd64': 'n', 'arm64': 'n', 'armhf': 'n', 'i386': 'n', 'ppc64el': 'n', 's390x': 'n'}>
-CONFIG_CMA_AREAS                                policy<{'amd64': '7', 'arm64': '7', 'armhf': '7', 'i386': '7', 'ppc64el': '7', 's390x': '7'}>
 
 # Menu: Memory Management options >> Memory model
 CONFIG_FLATMEM_MANUAL                           policy<{'i386': 'n', }>

Building the kernel

LANG=C fakeroot debian/rules clean
# quicker build:
LANG=C fakeroot debian/rules binary-headers binary-generic binary-perarch
# if you need linux-tools or lowlatency kernel, run instead:
LANG=C fakeroot debian/rules binary

其中binary-perarch是编译其他arch的image,我们可以去掉。

Install

此时可以在上级目录中看见deb文件

linux-buildinfo-5.4.0-123-generic_5.4.0-123.139~18.04.1_amd64.deb    linux-hwe-5.4-headers-5.4.0-123_5.4.0-123.139~18.04.1_all.deb           linux-modules-extra-5.4.0-123-generic_5.4.0-123.139~18.04.1_amd64.deb
linux-cloud-tools-5.4.0-123-generic_5.4.0-123.139~18.04.1_amd64.deb  linux-image-unsigned-5.4.0-123-generic_5.4.0-123.139~18.04.1_amd64.deb  linux-tools-5.4.0-123-generic_5.4.0-123.139~18.04.1_amd64.deb
linux-headers-5.4.0-123-generic_5.4.0-123.139~18.04.1_amd64.deb      linux-modules-5.4.0-123-generic_5.4.0-123.139~18.04.1_amd64.deb
 sudo dpkg -i linux-hwe-5.4-headers-5.4.0-123_5.4.0-123.139~18.04.1_all.deb 
 sudo dpkg -i linux-headers-5.4.0-123-generic_5.4.0-123.139~18.04.1_amd64.deb 
 sudo dpkg -i linux-image-unsigned-5.4.0-123-generic_5.4.0-123.139~18.04.1_amd64.deb 
 sudo dpkg -i linux-modules-5.4.0-123-generic_5.4.0-123.139~18.04.1_amd64.deb 
 sudo dpkg -i linux-modules-extra-5.4.0-123-generic_5.4.0-123.139~18.04.1_amd64.deb
 sudo dpkg -i linux-image-unsigned-5.4.0-123-generic_5.4.0-123.139~18.04.1_amd64.deb 
 sudo dpkg -i linux-modules-extra-5.4.0-123-generic_5.4.0-123.139~18.04.1_amd64.deb
 sudo dpkg -i linux-buildinfo-5.4.0-123-generic_5.4.0-123.139~18.04.1_amd64.deb 

另一种编译安装方式

上诉这种方式更新的header在/usr/src/目录下,但我们的编码有时候需要/usr/include下面的header,(比如/usr/include/linux/v4l2-controls.h),此时我们就需要linux-libc-dev.deb,但是这个deb包并不会随着你更新linux kernel而更新。需要自己编译出来;
参考How to compile and install custom mainline kernel
其实主要是下面两步:

cp /boot/config-4.4.0-040400rc8-generic .config
# cd source code
scripts/config --disable CC_STACKPROTECTOR_STRONG
time make -j9 bindeb-pkg       <<< Suggest use number of CPUs + 1. Use less for some responsiveness to be left for other uses
                               <<< I always time the kernel build.

这个时候就能看见编译出来的deb包了

~/data/linux_kernel/v5.4-rc8+$ ls
linux-5.4.0-rc8+_5.4.0-rc8+-1_amd64.buildinfo  linux-5.4.0-rc8+_5.4.0-rc8+-1.dsc                linux-image-5.4.0-rc8+_5.4.0-rc8+-1_amd64.deb      v5.4-rc8
linux-5.4.0-rc8+_5.4.0-rc8+-1_amd64.changes    linux-5.4.0-rc8+_5.4.0-rc8+.orig.tar.gz          linux-image-5.4.0-rc8+-dbg_5.4.0-rc8+-1_amd64.deb
linux-5.4.0-rc8+_5.4.0-rc8+-1.diff.gz          linux-headers-5.4.0-rc8+_5.4.0-rc8+-1_amd64.deb  linux-libc-dev_5.4.0-rc8+-1_amd64.deb

结语

如果有需要记得更新grub。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ubuntu Kylin是一个基于Ubuntu的中文操作系统,旨在为中国用户提供更好的本地化体验。要构建Ubuntu Kylin的ISO映像,请按照以下步骤进行操作: 1. 下载Ubuntu Kylin的ISO映像和Ubuntu Kylin的源代码。 2. 安装Ubuntu Kylin的编译环境和必要的依赖项。在终端中输入以下命令来安装这些软件包: ``` sudo apt-get update sudo apt-get install git ubuntu-dev-tools build-essential dh-make debhelper devscripts ``` 3. 解压缩Ubuntu Kylin的源代码,并切换到源代码目录中。在终端中输入以下命令: ``` tar -xzvf ubuntukylin.tar.gz cd ubuntukylin ``` 4. 在源代码目录中构建软件包。在终端中输入以下命令: ``` debuild -b -uc -us ``` 这将构建所有源代码中的软件包,并在构建完成后生成deb二进制文件。 5. 创建一个新目录,将生成的deb二进制文件和ISO映像复制到该目录中。 6. 创建一个新的ISO映像。在终端中输入以下命令: ``` sudo mkisofs -o /path/to/new/image.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -R -J -V "Ubuntu Kylin" . ``` 这将创建一个新的ISO映像,并在指定的路径下保存。请注意,您需要将“/path/to/new/image.iso”替换为您要保存ISO映像的实际路径。 7. 等待ISO映像创建完成,并验证它是否可以正常启动。 以上是构建Ubuntu Kylin ISO映像的简要步骤。请注意,这只是一个简单的示例,实际的构建过程可能会更加复杂,具体取决于您的系统配置和软件包需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值