Viktor K
Viktor K

Reputation: 2173

Creating simple kernel module

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.

Upvotes: 4

Views: 3916

Answers (3)

none
none

Reputation: 89

Because this task requires so many details, and small files to be coordinated it is best to use UML (user mode linux) so that the kprintf (kernel printf) always outputs to the terminal even in a graphical environment.

Upvotes: 0

daisy
daisy

Reputation: 23571

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.

Upvotes: 1

vonbrand
vonbrand

Reputation: 11841

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.

Upvotes: 3

Related Questions