I am learning kernel module now, so I setup a Ubuntu 16.04 with kernel 4.4.0-28-generic
in a virtual machine.
I installed this packages
# 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
I already decompresses the kernel source package /usr/src/linux-source-4.4.0.tar.bz2
to /home/test/WorkSpace/Kernel/linux-source-4.4.0
.
My system uname is
# 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
and I write a test module 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);
Makefile
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
But something confused me. I make my module and try to insmod
, and I got an error
# sudo insmod module.ko
insmod: ERROR: could not insert module module.ko: Invalid module format
What cause this error?
Did I use wrong version of kernel source?
for osgx
There is no additional line in output of dmesg after I try to insmod. The modinfo shows this
# modinfo module.ko
filename: /home/test/WorkSpace/LDM/hello/module.ko
license: Dual BSD/GPL
depends:
vermagic: 4.4.13 SMP mod_unload
I try to change the source path in the Makefile, remake it, and I get the new .ko
The modinfo of the new .ko is
# 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
But the problem still there, and the dmesg log looks like this
[ 28.540701] module: module verification failed: signature and/or required key missing - tainting kernel
[ 28.540879] module: module is already loaded
It looks like there is a module named module
already in the system, so I try to change the target name in the Makefile to hello.o
, make the new target name as hello.ko
, after that the module works.
But when I ran lsmod | grep module
, there is no module named module
?
dmesg
command after such insmod? What is output ofmodinfo
on your module? Try to build usingmake module M=`pwd` -C /lib/modules/$(uname -r)/build
- this is build dir for your current kernel with correct config, not just 4.4.0 downloaded from kernel.org (they are different, you should built exactly for your kernel; in ubuntu there is dkms en.wikipedia.org/wiki/Dynamic_Kernel_Module_Support to recompile module on kernel update). - osgxlinux-headers-generic
in your case. Then you can domake -C /lib/modules/`uname -r`/build M=`pwd` modules
to build against the running kernel. - Ian Abbott