一、命令操作
第一步:make
第二步:insmod hello.ko
第三步:lsmod
第四步:dmesg | tail
第五步:rmmod hello.ko
第六步:lsmod
第七步:dmesg | tail
二、代码实现
hello.c
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE ("GPL");
int __init init_module (void)
{
printk (KERN_INFO "Hello,world,this is the year 2023\n");
return 0;
}
void __exit cleanup_module (void)
{
printk (KERN_INFO "Goodbye world\n");
}
Makefile
$(warning KERNELRELEASE=$(KERNELRELEASE))
ifeq ($(KERNELRELEASE),)
#KERNELDIR ?= /home/lht/kernel2.6/linux-2.6.14
KERNELDIR ?= /lib/modules/$(shell uname -r)/build M=$(PWD) modules
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module* modules*
.PHONY: modules modules_install clean
else
obj-m := hello.o
endif