linux内核配置文件

写一个内核模块hello.c

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

int hello_init(void){
	printk("Hello init");
    return 0;
}
void hello_exit(void){
	printk("Hello exit");
}

module_init(hello_init)
module_exit(hello_exit)

/usr/src/linux-headers-5.4.0-58-generic/include/linux/init.h

编译内核模块Makefile

ifneq ($(KERNELRELEASE),)
	obj-m := hello.o
else
	KERN_DIR ?= /usr/src/linux-headers-$(shell uname -r)/
	PWD := $(shell pwd)

default:
	$(MAKE) -C $(KERN_DIR) M=$(PWD) modules
endif

clean:
	rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

结果
.
├── hello.c
├── hello.ko
├── .hello.ko.cmd
├── hello.mod
├── hello.mod.c
├── .hello.mod.cmd
├── hello.mod.o
├── .hello.mod.o.cmd
├── hello.o
├── .hello.o.cmd
├── Makefile
├── modules.order
└── Module.symvers

模块命令的使用

dmesg实时读取

 watch "dmesg | tail -20"
 dmesg -wH

参考: