文章目录
前言
一般而言我们更新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。