4

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.

4
  • Any error reported in dmesg? Commented Mar 30, 2013 at 12:48
  • Have you actually compiled a kernel in that src tree? Commented Mar 30, 2013 at 12:52
  • @Sukminder No difference between module_init(function) or init_module() etc. Also I've tried module_init - same result Commented Mar 30, 2013 at 13:31
  • Minimal runnable example on Ubuntu 16.04: github.com/cirosantilli/linux-kernel-module-cheat/tree/… Commented Jul 30, 2016 at 8:44

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

while compiling kernel I got error 'Install ncurses (ncurses-devel) and try again'. On some forum I found that I need to install libncurses5. So I've installed it and compilation finished with output as in my question.
kernel-devel is package for Fedora. For Ubuntu I've installed module-assistant
1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.