Centos system compiles kernel driver

There are two main ways to compile and debug the driver

1. Install linux-devel, linux-headers, download the source code in the centos firmware to compile

2. Directly use the linux kernel source code for new compilation.

1. Method 1

1.1. Install the kernel and header file dependencies

CentOS system

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

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

Ubuntu system

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. Download the kernel source code

uname -r View kernel version information

/cat /etc/*lease View file system information

 Expand the source code

Centos system: Index of /

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

1.3. Building the source code

Create the basic directory structure required to compile rpm as a normal user

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

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

Install the packages required to compile the kernel

$ 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

Install the kernel source

$ 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*”看到完整的内核源代码了。

Kernel compilation settings

$ 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 .

Copy the configuration of the current kernel to the source code directory,

        Directly reuse the configuration of the current kernel (avoid recompiling the entire kernel, and the problem of kernel version verification causing new module loading errors.)

$ make prepare

$ make modules_prepare

1.4. Test verification
 

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

$ mkdir modules && cd modules

hello/hello.c

 Expand the source code

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

load remove module

$ 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. Compile the module

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

make   M=drivers/block

Reference link: CentOS7 compiles its own kernel module - short book

2. Method 2

2.1. Download the kernel source code

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

2.2. Prepare the compilation environment

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. Compile

make -j 8 all

make modules_install

make install

2.4. Set default enable kernel

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] I need the source code of the kernel, https://wiki.centos.org/zh/HowTos/I_need_the_Kernel_Source
[3] I need Create a custom kernel, https://wiki.centos.org/zh/HowTos/Custom_Kernel
[4] Create your own kernel modules, https://wiki.centos.org/zh/HowTos/BuildingKernelModules
[5] Building Source RPM as non-root under CentOS, http://www.owlriver.com/tips/non-root/

Guess you like

Origin blog.csdn.net/wfjdemmye/article/details/121543471