驱动编译调试主要有两种办法
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 /etclinux*”看到完整的内核源代码了。
内核编译设置
| $ 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/



