Centos系统编译内核驱动

驱动编译调试主要有两种办法

1,安装linux-devel ,linux-headers,下载centos固件中源码进行编译

2,直接使用linux的内核源码进行新的编译。

1. 方法一

1.1. 安装内核和头文件依赖

Centos系统

sudo yum -y install kernel-headers  //安装kernel-headers

sudo yum -y install kernel-devel    //安装kernel-devel

Ubuntu系统

sudo apt-get install build-essential  //install build-essential(optional)

sudo apt-get update                  //install linux-headers

sudo apt-get install linux-headers-$(uname -r)

sudo apt-get update && sudo apt-get install build-essential linux-headers-$(uname -r)

1.2. 下载内核源码

uname -r                  查看内核版本信息

/cat /etc/*lease         查看文件系统信息

 展开源码

Centos系统:Index of /

Linux SRC:https://vault.centos.org/7.8.2003/os/Source/SPackages/kernel-3.10.0-1127.el7.src.rpm  下载

1.3. 构建源码

以普通用户创建编译rpm所需的基础目录结构

$ mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}

$ echo '%_topdir %(echo $HOME)/rpmbuild' > ~/.rpmmacros

安装编译内核所需软件包

$ sudo yum install kernel-devel

$ sudo yum install rpm-build redhat-rpm-config asciidoc hmaccalc perl-ExtUtils-Embed pesign xmlto

$ sudo yum install audit-libs-devel binutils-devel elfutils-devel elfutils-libelf-devel

$ sudo yum install ncurses-devel newt-devel numactl-devel pciutils-devel python-devel zlib-devel

安装内核源码

$ rpm -i kernel-3.10.0-514.26.2.el7.src.rpm 2>&1 | grep -v exist

$ cd ~/rpmbuild/SPECS

$ rpmbuild -bp --target=$(uname -m) kernel.spec

现在可以在“~/rpmbuild/BUILD/kernel*/linux*”看到完整的内核源代码了。

内核编译设置

$ cd ~/rpmbuild/BUILD/kernel-*/linux-*/

$ cp /boot/config-`uname -r` .config

$ cp /usr/src/kernels/`uname -r`/Module.symvers .

$ cp /usr/src/kernels/`uname -r`/System.map .

$ cp /usr/src/kernels/`uname -r`/vmlinux.id .

拷贝当前内核的配置到源码目录,

        直接复用当前内核的配置(避免重新编译整个内核,以及内核版本校验导致新模块加载错误的问题。)

$ make prepare

$ make modules_prepare

1.4. 测试验证
 

$ cd ~/rpmbuild/BUILD/kernel-*/linux-*/

$ mkdir modules && cd modules

hello/hello.c

 展开源码

hello/Makefile

obj-m += hello.o

all:

    make -C $(PWD) M=$(PWD) modules

clean:

    make -C $(PWD) M=$(PWD) clean

$ make M=modules/hello

$ sudo cp modules/hello/hello.ko /lib/modules/`uname -r`/extra

加载移除模块

$ cd /lib/modules/`uname -r`/extra

$ sudo insmod hello.ko

$ sudo rmmod hello

$ tail -f /var/log/messages

* localhost kernel: Module init: Hello linux kernel.

* localhost kernel: Module exit: Bye-bye linux kernel.

1.5. 编译模块

cd ~/rpmbuild/BUILD/kernel-*/linux-*/

make   M=drivers/block

参考链接:CentOS7编译自己的内核模块 - 简书

2. 方法二

2.1. 下载内核源码

https://mirrors.edge.kernel.org/pub/linux/kernel/v3.x

2.2. 准备编译环境

yum -y groupinstall "development tools"

yum -y install ncurses-devel

#解压Linux内核

linux-4.4.241.tar.gz

#拷贝内核配置

cp /boot/config-4.4.241-1.el7.elrepo.x86_64 .config

2.3. 编译

make -j 8 all

make modules_install

make install

2.4. 设置默认启用内核

awk -F\' '$1=="menuentry " {print i++ " : " $2}' $(find /boot -name grub.cfg)
awk -F\' '$1=="menuentry " {print i++ " : " $2}' /boot/grub2/grub.cfg


0 : CentOS Linux (4.4.241.test.x86_64+) 7 (Core)
1 : CentOS Linux (3.10.0-1160.25.1.el7.x86_64) 7 (Core)
2 : CentOS Linux (4.4.241-1.el7.elrepo.x86_64) 7 (Core)
3 : CentOS Linux (3.10.0-1127.19.1.el7.x86_64) 7 (Core)
4 : CentOS Linux (3.10.0-957.el7.x86_64) 7 (Core)
5 : CentOS Linux (0-rescue-f6b59b458a184fd3a3bc8bed678e1a52) 7 (Core)


grub2-set-default 1

grub2-editenv list

cat /etc/default/grub

make -C /usr/src/kernels/3.10.0-862.el7.x86_64/ M=$(pwd) modules

make -C /usr/src/kernels/`uname -r`/ M=`pwd` modules

三,配置内核启动项

GRUB配置: GRUB_CMDLINE_LINUX="intel_iommu=on iommu=pt"
awk -F\' '$1=="menuentry " {print i++ " : " $2}' $(find /boot -name grub.cfg)
0 : CentOS Linux (4.4.241-1.el7.elrepo.x86_64) 7 (Core)
1 : CentOS Linux (3.10.0-1127.19.1.el7.x86_64) 7 (Core)
2 : CentOS Linux (3.10.0-957.el7.x86_64) 7 (Core)
3 : CentOS Linux (0-rescue-f6b59b458a184fd3a3bc8bed678e1a52) 7 (Core)
grub2-set-default 1
grub2-editenv list
cat /etc/default/grub

http://vault.centos.org"去下载内核源码。https://vault.centos.org/7.8.2003/正在上传…重新上传取消​
下载地址:Index of /7.8.2003/updates/Source/SPackages
找到当前内核版本
内核源码安装参考: CentOS安装相应版本的内核源码 - AlexAlex - 博客园
模块编译参考: CentOS7编译自己的内核模块 - 简书
yum install kernel-headers kernel-devel
内核编译:
cd /root/rpmbuild/BUILD/kernel-3.10.0-1160.25.1.el7/linux-3.10.0-1160.25.1.el7.x86_64
$ make M=modules/hello
$ sudo cp modules/hello/hello.ko /lib/modules/`uname -r`/extra
$ cd /lib/modules/`uname -r`/extra
$ sudo insmod hello.ko
$ sudo rmmod hello
$ tail -f /var/log/messages

参考链接
CentOS7编译内核 详细步骤_飙风的蜗牛博客-CSDN博客_centos 内核编译

其它参考链接

[1] SystemTap on CentOS,https://sourceware.org/systemtap/wiki/SystemTapOnCentOS
[2] 我需要内核的源代码,https://wiki.centos.org/zh/HowTos/I_need_the_Kernel_Source
[3] 我需要创建一个自设的内核,https://wiki.centos.org/zh/HowTos/Custom_Kernel
[4] 创建你自己的内核模块,https://wiki.centos.org/zh/HowTos/BuildingKernelModules
[5] Building Source RPM as non-root under CentOS,http://www.owlriver.com/tips/non-root/

猜你喜欢

转载自blog.csdn.net/wfjdemmye/article/details/121543471