编译自己的Ubuntu内核

很多时候我们在使用Ubuntu的时候,想修改一下内核配置,然后编译,安装到Ubuntu中。这也是进行Ubuntu内核开发的前提。

获取当前Ubuntu对应代码

有很多方法可以获得Ubuntu内核代码,但是下面两种非常方便。

uname -r显示当前内核的版本号,apt-get source则用来获取package的source code:

apt-get source linux-image-$(uname -r)

image

另一种方法是使用git,每个发行版的内核库都会在kernel.ubuntu.com找到。如果想下载,只需要git clone一个库到本地即可:

git clone git://kernel.ubuntu.com/ubuntu/ubuntu-<release codename>.git

其中release codename通过lsb_release -a获取,比如Ubuntu 16.04的Codename为xenial。

Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04
Codename: xenial

PS:从实际使用来看,apt-get source的方式更方便,也不容易出错,获取的内核代码和当前使用的是同一个版本。

编译环境配置

如果需要编译内核,还需要安装一个package。可以使用如下命令:

sudo apt-get build-dep linux-image-$(uname -r)

修改内核配置

整个编译过程使用的配置文件分布在若干子配置文件中,使用如下命令可以将这些配置文件集中,调用menuconfig进行修改。

使用chmod解决部分脚本可执行权限问题,

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

编译内核

进入内核代码的根目录,执行如下命令:

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

如果编译成功,在根目录下会生成若干.deb包。

如下是4.4.0-57-generic内核在AMD64系统上生成的.deb包:

image

安装内核镜像

使用dpkg -i安装若干.deb文件到系统中,然后重启:

sudo dpkg -i linux*4.4.0-57*.deb
sudo reboot

Debug符号表

有时进行Debug需要符号表,这就需要在编译的时候进行配置。首先需要安装pkg-config-dgbsym,然后在执行编译binary-*的时候增加’skildbg=false’:

sudo apt-get install pkg-create-dbgsym
fakeroot debian/rules clean
fakeroot debian/rules binary-headers binary-generic binary-perarch skipdbg=false

删除Ubuntu内核

如果部分内核不需要,可以选择删除。需要先查看当前使用的内核版本:

uname -a可以获取当前内核版本号。

在Ubuntu内核镜像包含了以下的包。

  • linux-image-: 内核镜像
  • linux-image-extra-: 额外的内核模块
  • linux-headers-: 内核头文件

首先检查系统中安装的内核镜像:

dpkg --list|grep linux-image

dpkg --list|grep linux-headers

输出如下结果:

al@al:~/data/ubuntu-kernel$ dpkg --list|grep linux-image
ii  linux-image-3.13.0-105-generic                       3.13.0-105.152                                amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
rc  linux-image-3.13.0-32-generic                        3.13.0-32.57                                  amd64        Linux kernel image for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-4.4.0-57-generic                         4.4.0-57.78                                   amd64        Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii  linux-image-extra-3.13.0-105-generic                 3.13.0-105.152                                amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
rc  linux-image-extra-3.13.0-32-generic                  3.13.0-32.57                                  amd64        Linux kernel extra modules for version 3.13.0 on 64 bit x86 SMP
ii  linux-image-extra-4.4.0-57-generic                   4.4.0-57.78                                   amd64        Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
ii  linux-image-generic                                  4.4.0.57.60                                   amd64        Generic Linux kernel image
al@al:~/data/ubuntu-kernel$ dpkg --list|grep linux-headers
ii  linux-headers-4.4.0-57                               4.4.0-57.78                                   all          Header files related to Linux kernel version 4.4.0
ii  linux-headers-4.4.0-57-generic                       4.4.0-57.78                                   amd64        Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii  linux-headers-generic                                4.4.0.57.60                                   amd64        Generic Linux kernel headers

在列出的内核镜像中,你可以移除一个特定的版本(比如3.13.0-105和3.13.0-32)。

sudo apt-get purge linux-image-3.19.0-15

这就会将3.19.0-15相关的内核模块删除。通过下面dpkg --list再验证如下:

dpkg --list|grep linux-headers

还可以通过sudo update-grub2来更新grub列表。

参考资料:

BuildYourOwnKernelhttps://wiki.ubuntu.com/Kernel/BuildYourOwnKernel

KernelGitGuidehttps://wiki.ubuntu.com/Kernel/Dev/KernelGitGuide

原文地址:https://www.cnblogs.com/arnoldlu/p/6227843.html