Home > OS >  Trying to compile hello world linux kernel module: missing MODULE_LICENSE()
Trying to compile hello world linux kernel module: missing MODULE_LICENSE()

Time:06-28

I'm trying to compile the simplest kernel module possible, from the The Linux Kernel Module Programming Guide. hello.c:

#include <linux/module.h>   /* Needed by all modules */
#include <linux/kernel.h>   /* Needed for KERN_INFO */

int init_module(void)
{
    printk(KERN_INFO "Hello world 1.\n");

    /* 
     * A non 0 return means init_module failed; module can't be loaded. 
     */
    return 0;
}

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

Makefile:

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

Output of make:

make -C /lib/modules/5.14.0/build M=/home/parallels/dev/demo modules
make[1]: Entering directory '/home/parallels/linux-5.14'
  CC [M]  /home/parallels/dev/demo/hello.o
  MODPOST /home/parallels/dev/demo/Module.symvers
ERROR: modpost: missing MODULE_LICENSE() in /home/parallels/dev/demo/hello.o
make[2]: *** [scripts/Makefile.modpost:150: /home/parallels/dev/demo/Module.symvers] Error 1
make[2]: *** Deleting file '/home/parallels/dev/demo/Module.symvers'
make[1]: *** [Makefile:1766: modules] Error 2
make[1]: Leaving directory '/home/parallels/linux-5.14'
make: *** [Makefile:4: all] Error 2

This is all after I have compiled the linux kernel from source and run make modules_install. I don't really understand that second line of output: make[1]: Entering directory '/home/parallels/linux-5.14', that is the directory in which I downloaded and compiled the linux kernel, I don't see what it has to do with this module I'm trying to compile.

I have seen others ask about this missing MODULE_LICENSE() error, but they are usually asking about much more complicated modules and the answers don't seem relevant to this very simple module. A few of the answers had to do with changing the name of either the source code file or the .o file, but trying either way simply produced errors about no rule to make target with the changed name.

Also, this is all on a vm of Kali linux running linux kernel 5.14.0 which I compiled from source.

CodePudding user response:

You need to add call to MODULE_LICENSE to your source file (hello.c in your case). This is what the error message is talking about. E.g.

MODULE_LICENSE("GPL");

Most modules also call MODULE_AUTHOR:

MODULE_AUTHOR("your-name");
  • Related