use buildroot develop linux kernel module

prepare buildroot source tree

git clone https://git.buildroot.net/buildroot
cd buildroot
git checkout 2022.08.1 # checkout a stable version

create directory

mkdir package/lkhello
mkdir package/lkhello/src

add Config.in

$EDITOR package/lkhello/Config.in

write:

config BR2_PACKAGE_LKHELLO
	bool "lkhello"
	depends on BR2_LINUX_KERNEL
	help
	 linux kernel hello

comment "lkhello needs a Linux kernel to be built"
	depends on !BR2_LINUX_KERNEL	  

add lkhello.mk

$EDITOR package/lkhello/lkhello.mk

write:

################################################################################
#
# lkhello
#
################################################################################

LKHELLO_VERSION = 0.1
LKHELLO_SITE = "$(TOPDIR)/package/lkhello/src"
LKHELLO_SITE_METHOD = local
LKHELLO_MODULE_MAKE_OPTS = CONFIG_LKHELLO=m

$(eval $(kernel-module))
$(eval $(generic-package))

write module

add Kconfig

$EDITOR package/lkhello/src/Kconfig

write:

menuconfig LKHELLO
	tristate "linux kernel helloworld"

add Makefile

$EDITOR package/lkhello/src/Makefile

write:

obj-m := lkhello.o

add lkhello.c

$EDITOR package/lkhello/src/Makefile

write:

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

MODULE_LICENSE("Dual BSD/GPL");

static int lkhello_init(void) {
	printk(KERN_ALERT "Hello, World\n");
	return 0;
}

static void lkhello_exit(void) {
	printk(KERN_ALERT "Bye, World\n");
}

module_init(lkhello_init);
module_exit(lkhello_exit);

add to buildroot menu

$EDITOR package/Config.in

add something like other package’s define:

--- a/package/Config.in
+++ b/package/Config.in
@@ -2680,4 +2680,8 @@ menu "Text editors and viewers"
	source "package/vim/Config.in"
 endmenu

+menu "my packages"
+	source "package/lkhello/Config.in"
+endmenu
+
 endmenu

all is done

all is done, now you can select ’lkhello’ use:

make menuconfig
Target packages  --->
	 my packages  --->
		[*] lkhello   

select lkhello and compile firmware.

boot up firmware.

and ’modprobe lkhello’ to print a ’Hello, World’ in kernel log

use ’rmmod lkhello’ to print a ’Bye, World’ in kernel log

Last updated: 2023-06-28