linux - 无法构建自定义模块 CentOS 7

标签 linux linux-kernel centos linux-device-driver

我正在尝试在 CentOS 7 中构建内核模块。当我运行 make 时,我得到以下信息

$ make
make -C /usr/src/kernels/`uname -r` SUBDIRS= modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-514.21.1.el7.x86_64'
Makefile:641: Cannot use CONFIG_CC_STACKPROTECTOR_STRONG: -fstack-protector-strong not supported by compiler
make[2]: *** No rule to make target `arch/x86/syscalls/syscall_32.tbl', needed by `arch/x86/syscalls/../include/generated/uapi/asm/unistd_32.h'.  Stop.
make[1]: *** [archheaders] Error 2
make[1]: Leaving directory `/usr/src/kernels/3.10.0-514.21.1.el7.x86_64'
make: *** [default] Error 2

根据我的在线阅读,我似乎没有内核源代码。

我关注了these instructions ,但仍然出现相同的错误。

我还找到了these instructions ,但是一开始就遇到了问题。

这是页面开头的声明:

It is assumed that you already have the full kernel source tree installed. If you followed Section 2 of I need the Kernel Source, it will be found in the directory ~/rpmbuild/BUILD/kernel-2.6.18/linux-2.6.18.uname -m/

这是我运行 tree ~/rpmbuild 时得到的结果

/home/user/rpmbuild
|-- BUILD
|-- BUILDROOT
|-- RPMS
|-- SOURCES
|   |-- centos.cer
|   |-- centos-kpatch.x509
|   |-- centos-ldup.x509
|   |-- check-kabi
|   |-- cpupower.config
|   |-- cpupower.service
|   |-- debrand-rh-i686-cpu.patch
|   |-- debrand-rh_taint.patch
|   |-- debrand-single-cpu.patch
|   |-- extra_certificates
|   |-- kernel-3.10.0-ppc64.config
|   |-- kernel-3.10.0-ppc64-debug.config
|   |-- kernel-3.10.0-ppc64le.config
|   |-- kernel-3.10.0-ppc64le-debug.config
|   |-- kernel-3.10.0-s390x.config
|   |-- kernel-3.10.0-s390x-debug.config
|   |-- kernel-3.10.0-s390x-kdump.config
|   |-- kernel-3.10.0-x86_64.config
|   |-- kernel-3.10.0-x86_64-debug.config
|   |-- kernel-abi-whitelists-514.tar.bz2
|   |-- linux-3.10.0-514.21.1.el7.tar.xz
|   |-- linux-3.10.0-514.26.2.el7.tar.xz
|   |-- linux-kernel-test.patch
|   |-- Makefile.common
|   |-- Module.kabi_dup_ppc64
|   |-- Module.kabi_dup_ppc64le
|   |-- Module.kabi_dup_s390x
|   |-- Module.kabi_dup_x86_64
|   |-- Module.kabi_ppc64
|   |-- Module.kabi_ppc64le
|   |-- Module.kabi_s390x
|   |-- Module.kabi_x86_64
|   |-- secureboot.cer
|   |-- sign-modules
|   `-- x509.genkey
|-- SPECS
|   `-- kernel.spec
`-- SRPMS

我的 BUILD 目录是空的。我需要做什么才能在 CentOS7 中编译内核模块?

这是内核模块的代码

#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>

static int __init ofd_init(void) {
  printk(KERN_INFO, "OFD registered");
  return 0;
}

static int __exit ofd_exit(void) {
  printk(KERN_INFO, "OFD unregistered");
  return 0;
}

module_init(ofd_init);
module_exit(ofd_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Test");

这是它的makefile

# makefile - makefile for test

# If KERNELRELEASE is defined, we've been invoked from the
# kernek build system and need to use its language
ifneq ($(KERNELRELEASE),)
    obj-m := ofd.o
# Otherwise we've been invoked from the command line.
# Invoke the kernel build system
else
    KERNEL_SOURCE := /usr/src/kernels/`uname -r`
    PWD := $(shel pwd)

default:
    $(MAKE) -C $(KERNEL_SOURCE) SUBDIRS=$(PWD) modules

clean:
    $(MAKE) -C $(KERNEL_SOURCE) SUBDIRS=$(PWD) clean
endif

最佳答案

也许您错过了源代码树提取过程。

来自 https://wiki.centos.org/HowTos/I_need_the_Kernel_Source :

As an ordinary user, not root, install the source package by executing:

[user@host]$ rpm -i http://vault.centos.org/7.4.1708/updates/Source/SPackages/kernel-3.10.0-693.21.1.el7.src.rpm 2>&1 | grep -v exist

这会将内核源代码树解压到 ~/rpmbuild/BUILD

关于linux - 无法构建自定义模块 CentOS 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45019493/

相关文章:

kernel - 如何打印每个数据包的前 34 个字节(以太网和 IP header )(EtherType : 0x0806, Protocol : ARP)?

linux - 优化我的 grep 以搜索 "$DIR/www"?

linux - logrotate 会删除 .gz 文件吗?也就是说,带 * 路径的 logrotate 是否会旋转现有的 .gz 文件?

linux - 使用 awk 打印两列之间具有重叠值范围的记录对

linux-kernel - 如何在 linux 内核空间中打印用户空间堆栈跟踪

linux - IOCTL Linux 设备驱动程序

centos - 如何在 CentOS 5.8 上安装 bind 9.9.8 或更高版本

linux - 无法通过 CLI 调试 Jmeter

linux - 在 linux(CentOS)/多处理器设置中,如何将 CPU 内核分配给 NUMA 节点?

c++ - 从 C++ 中的外部程序捕获 stderr 和 stdout