Howto build a kernel module out of the kernel tree

From Wiki Kaeilos.com
Jump to: navigation, search

Yocto recipe to build a kernel module out of the kernel tree

While it is always preferable to work with sources integrated into the Linux kernel sources, if you need an external kernel module, you could start from this simple recipe based on Yocto Project kernel Development Manual. This GPL2 recipe is available as a template from which you can create your own out-of-tree Linux kernel module recipe.


$ tree
.
├── files
│   ├── COPYING
│   ├── hellokernel.c
│   └── Makefile
└── kernmodule.bb


COPYING

Copy COPYING file into your files

cp meta-skeleton/recipes-kernel/hello-mod/files/COPYING  YOURPATH/kernmodule/files/


kernmodule.bb

#
# Yocto recipe to build a kernel module out of the kernel tree
# kernmodule.bb  
# Marco Cavallini - KOAN sas - www.koansoftware.com
#

DESCRIPTION = "Hello kernel module out of the kernel tree"
SECTION = "examples"
LICENSE = "GPLv2"
LIC_FILES_CHKSUM = "file://COPYING;md5=12f884d2ae1ff87c09e5b7ccc2c4ca7e"
PR = "r0"

inherit module

SRC_URI =   "file://hellokernel.c \
			file://Makefile \
			file://COPYING \
			"

S = "${WORKDIR}"

Makefile

obj-m += hellokernel.o
SRC := $(shell pwd)

all:
	$(MAKE) -C $(KERNEL_SRC) M=$(SRC) modules

modules_install:
	$(MAKE) -C $(KERNEL_SRC) M=$(SRC) modules_install

hellokernel.c

/*
 * hello.c - The simplest and dirty kernel module
 */

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

int init_module(void)
{
  printk(KERN_INFO "Hello world\n");
  
  return 0;
}

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