Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating simple kernel module

Tags:

c

linux-kernel

I've just started learning linux kernel modules and trying to write simple Hello world program.

So mymod.c:

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

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Author");
MODULE_DESCRIPTION("\"Hello, world!\" minimal module");
MODULE_VERSION("printk");

int init_module(void)
{
    printk("<1>Hello world 1.\n");
    return 0;
} 

void cleanup_module(void)
{
    printk(KERN_ALERT "Goodbye world 1.\n");
}

Makefile:

obj-m += mymod.o 
all:
     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules 
clean: 
     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

make outout:

make -C /lib/modules/3.2.0-23-generic-pae/build M=/root modules
make[1]: Entering directory `/usr/src/linux-3.2.42'

  WARNING: Symbol version dump /usr/src/linux-3.2.42/Module.symvers
           is missing; modules will have no dependencies and modversions.

  Building modules, stage 2.
  MODPOST 1 modules
make[1]: Leaving directory `/usr/src/linux-3.2.42'

So it creates files I needed, but when I try to install this by

insmod mymod.ko

I get next output:

insmod: error inserting 'mymod.ko': -1 Invalid module format

So I'd like to know what's the problem?

PS. OS - Ubuntu Server 12.04. Kernel - linux 3.2.0-23 pae

UPDATE:

I've downloaded from kernel.org kernel 3.2.42 and put it in /usr/src and did 'make defconfig && make prepare', 'make modules_prepare'. Also I've created link in /lib/modules/3.2.0-23-generic-pae/build.

like image 868
Viktor K Avatar asked Oct 21 '25 05:10

Viktor K


2 Answers

Is this the source tree for the running kernel? If not, it should fail.

Install the kernel-devel (or similarly named) package for your distribution, it adds enough machinery to build modules against it.

like image 112
vonbrand Avatar answered Oct 23 '25 21:10

vonbrand


You missed the module_init and module_cleanup declaration,

module_init (module_init);
module_exit (cleanup_module);

Otherwise it would have no entry point defined, and it wouldn't load.

like image 21
daisy Avatar answered Oct 23 '25 20:10

daisy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!