使用的是parallel 创建的ubuntu 16.04 ubuntu20.04虚拟机
源码准备
# 先查看本机版本
$ uname -r
5.15.0-86-generic
# 搜索相关源码
$ sudo apt-cache search linux-source
[sudo] password for showme:
linux-source - Linux kernel source with Ubuntu patches
linux-source-5.4.0 - Linux kernel source for version 5.4.0 with Ubuntu patches
linux-gkeop-source-5.4.0 - Linux kernel source for version 5.4.0 with Ubuntu patches
linux-hwe-5.11-source-5.11.0 - Linux kernel source for version 5.11.0 with Ubuntu patches
linux-hwe-5.13-source-5.13.0 - Linux kernel source for version 5.13.0 with Ubuntu patches
linux-hwe-5.15-source-5.15.0 - Linux kernel source for version 5.15.0 with Ubuntu patches
linux-hwe-5.8-source-5.8.0 - Linux kernel source for version 5.8.0 with Ubuntu patches
linux-intel-5.13-source-5.13.0 - Linux kernel source for version 5.13.0 with Ubuntu patches
# 下载源码,源码会下载到/usr/src目录下
$ sudo apt install linux-source-5.4.0
showme@showme:/usr/src/linux-source-5.4.0$ ls -l
total 131816
drwxr-xr-x 15 root root 4096 10月 31 03:28 debian
drwxr-xr-x 9 root root 4096 10月 31 03:28 debian.master
-rw-r--r-- 1 root root 134966876 10月 3 02:13 linux-source-5.4.0.tar.bz2
# 复制linux-source-5.4.0.tar.bz2,编译两份,一份用于内核安装,一份用于clion驱动开发代码提示
# 用于内核安装的,会编译很多驱动,用clion打开会很卡,需要大概16G内存,才能不卡
# 用于开发环境的,编译会很快,用clion打开也会很快,代码提示时,也不会卡
安装编译依赖
# 安装所需要依赖的库
sudo apt-get install libncurses5-dev gcc make bc g++ vim git bc flex bison libssl-dev dwarves libelf-dev automake autoconf cvs subversion ruby
# 安装 bear 用于clion打开内核源码
sudo apt install bear
# 下载kernel-grok用于clion打开内核源码
git clone https://github.com/habemus-papadum/kernel-grok
编译内核
在编译的时候把虚拟机分配8核,8G内存,开发的时候2核,4G内存就够了
编译用于clion打开的内核
# 解压linux-source-5.4.0.tar.bz2源码,进入到linux-source-5.4.0目录
# 配置
make menuconfig
# 修改配置文件 .config
# 搜索debian关键字,如果存在如下内容
CONFIG_SYSTEM_TRUSTED_KEYS="debian/canonical-certs.pem"
CONFIG_SYSTEM_REVOCATION_KEYS="debian/canonical-revoked-certs.pem"
# 将其清空
CONFIG_SYSTEM_TRUSTED_KEYS=""
CONFIG_SYSTEM_REVOCATION_KEYS=""
# 使用bear编译内核,只生成bzImage和vmlinux编译很快
bear make bzImage vmlinux -j16
# 生成内核源码的 CMakeLists.txt,需要进入内核源码目录下,执行
~/kernel-grok/generate_cmake
# clion打开 CMakeLists.txt ,并添加如下内容
cmake_minimum_required(VERSION 2.8.8)
project(kernel)
set(SYSROOT sysroot)
SET(CMAKE_C_COMPILER "gcc")
set(CMAKE_C_STANDARD 90)
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} " --sysroot=${SYSROOT}" )
include_directories("include")
include_directories("include/uapi")
include_directories("include/generated")
include_directories("arch/x86/include")
include_directories("arch/x86/include/uapi")
include_directories("arch/x86/include/generated")
include_directories("arch/x86/include/generated/uapi")
add_definitions(-D__KERNEL__)
编译用于安装的内核
# 解压linux-source-5.4.0.tar.bz2源码,进入到linux-source-5.4.0目录
sudo make oldconfig #按原有kernel配置配置kernel
sudo make #编译内核,时间较长,可能会长达1小时及以上,如5.4.0用时约3-5个小时
sudo make modules_install #安装内核模块
sudo make install # 安装内核
参考
https://blog.csdn.net/inf4inf/article/details/110272531
https://blog.csdn.net/yaoxinJJJ/article/details/115433638
https://zhuanlan.zhihu.com/p/409007775?utm_id=0