【发布时间】:2016-07-14 00:49:17
【问题描述】:
我现在正在学习内核模块,所以我在虚拟机中设置了一个带有kernel 4.4.0-28-generic 的 Ubuntu 16.04。
我安装了这个包
# dpkg -l | grep linux
ii console-setup-linux 1.108ubuntu15 all Linux specific part of console-setup
ii libselinux1:amd64 2.4-3build2 amd64 SELinux runtime shared libraries
ii linux-base 4.0ubuntu1 all Linux image base package
ii linux-firmware 1.157.2 all Firmware for Linux kernel drivers
ii linux-generic 4.4.0.28.30 amd64 Complete Generic Linux kernel and headers
ii linux-headers-4.4.0-28 4.4.0-28.47 all Header files related to Linux kernel version 4.4.0
ii linux-headers-4.4.0-28-generic 4.4.0-28.47 amd64 Linux kernel headers for version 4.4.0 on 64 bit x86 SMP
ii linux-headers-generic 4.4.0.28.30 amd64 Generic Linux kernel headers
ii linux-image-4.4.0-28-generic 4.4.0-28.47 amd64 Linux kernel image for version 4.4.0 on 64 bit x86 SMP
ii linux-image-extra-4.4.0-28-generic 4.4.0-28.47 amd64 Linux kernel extra modules for version 4.4.0 on 64 bit x86 SMP
ii linux-image-generic 4.4.0.28.30 amd64 Generic Linux kernel image
ii linux-libc-dev:amd64 4.4.0-28.47 amd64 Linux Kernel Headers for development
ii linux-sound-base 1.0.25+dfsg-0ubuntu5 all base package for ALSA and OSS sound systems
ii linux-source 4.4.0.28.30 all Linux kernel source with Ubuntu patches
ii linux-source-4.4.0 4.4.0-28.47 all Linux kernel source for version 4.4.0 with Ubuntu patches
ii util-linux 2.27.1-6ubuntu3.1 amd64 miscellaneous system utilities
我已经把内核源码包/usr/src/linux-source-4.4.0.tar.bz2解压成/home/test/WorkSpace/Kernel/linux-source-4.4.0了。
我的系统名是
# uname -a
Linux ubuntu-ldm 4.4.0-28-generic #47-Ubuntu SMP Fri Jun 24 10:09:13 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
我写了一个测试模块hello.c
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init (void) {
printk (KERN_ALERT "Hello, World\n");
return 0;
}
static void hello_exit (void) {
printk (KERN_ALERT "Goodbye, cruel world\n");
}
module_init (hello_init);
module_exit (hello_exit);
生成文件
obj-m += module.o
module-objs := hello.o
all:
make modules M=`pwd` -C /home/test/WorkSpace/Kernel/linux-source-4.4.0
clean:
make modules clean M=`pwd` -C /home/test/WorkSpace/Kernel/linux-source-4.4.0
但是有些事情让我很困惑。我制作我的模块并尝试insmod,但出现错误
# sudo insmod module.ko
insmod: ERROR: could not insert module module.ko: Invalid module format
是什么导致了这个错误?
我是否使用了错误版本的内核源代码?
对于 osgx
我尝试insmod 后,dmesg 的输出中没有额外的行。 modinfo 显示了这个
# modinfo module.ko
filename: /home/test/WorkSpace/LDM/hello/module.ko
license: Dual BSD/GPL
depends:
vermagic: 4.4.13 SMP mod_unload
我尝试更改 Makefile 中的源路径,重新制作它,然后我得到了新的 .ko
新.ko的modinfo是
# modinfo module.ko
filename: /home/joshua/WorkSpace/LDM/hello/module.ko
license: Dual BSD/GPL
srcversion: 82C361DBCB1C9BB5CA1DB07
depends:
vermagic: 4.4.0-28-generic SMP mod_unload modversions
但是问题依然存在,dmesg日志看起来是这样的
[ 28.540701] module: module verification failed: signature and/or required key missing - tainting kernel
[ 28.540879] module: module is already loaded
系统中好像已经有一个名为module的模块,所以我尝试将Makefile中的目标名称更改为hello.o,将新的目标名称设为@987654336 @,之后模块就可以工作了。
但是当我运行lsmod | grep module时,没有名为module的模块?
【问题讨论】:
-
Joshua,在这样的 insmod 之后,
dmesg命令的输出中是否还有其他行?你的模块上modinfo的输出是什么?尝试使用make module M=`pwd` -C /lib/modules/$(uname -r)/build构建 - 这是您当前内核的构建目录,配置正确,而不仅仅是从 kernel.org 下载的 4.4.0(它们不同,您应该完全为您的内核构建;在 ubuntu 中有 dkms @987654321 @ 在内核更新时重新编译模块)。 -
@osgx 我更新了新信息格式的问题。
-
那就别叫它module.ko了,叫别的吧!
-
您不需要内核源代码。您只需安装与您正在运行的内核匹配的 linux-headers-flavor 软件包。在您的情况下,那将是
linux-headers-generic。然后你可以使用make -C /lib/modules/`uname -r`/build M=`pwd` modules来构建正在运行的内核。
标签: linux linux-kernel kernel kernel-module