Linux kernel 模块 hello 测试

原文链接:https://www.cnblogs.com/nerohwang/p/3621316.html

hello.c 文件:

#include <linux/kernel.h> /*Needed by all modules*/
#include <linux/module.h> /*Needed for KERN_* */
#include <linux/init.h> /* Needed for the macros */

MODULE_LICENSE("GPL");

static int year=2014;

static int hello_init(void)
{
  printk(KERN_WARNING "Hello kernel, it's %d!
",year);
  return 0;
}


static void hello_exit(void)
{
  printk("Bye, kernel!
");
}

/* main module function*/
module_init(hello_init);
module_exit(hello_exit);

Makefile文件:

obj-m := hello.o

all :
    $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
    
clean:
    $(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

测试结果:

[root@controller test]# make CONFIG_STACK_VALIDATION=
make -C /lib/modules/3.10.0-693.el7.x86_64/build M=/root/test modules
make[1]: Entering directory `/usr/src/kernels/3.10.0-693.el7.x86_64'
  CC [M]  /root/test/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /root/test/hello.mod.o
  LD [M]  /root/test/hello.ko
make[1]: Leaving directory `/usr/src/kernels/3.10.0-693.el7.x86_64'

查看编译后文件:

[root@controller test]# ll
total 204
-rw-r--r-- 1 root root   441 Nov  2 14:28 hello.c
-rw-r--r-- 1 root root 93768 Nov  2 14:30 hello.ko
-rw-r--r-- 1 root root   787 Nov  2 14:30 hello.mod.c
-rw-r--r-- 1 root root 52872 Nov  2 14:30 hello.mod.o
-rw-r--r-- 1 root root 44016 Nov  2 14:30 hello.o
-rw-r--r-- 1 root root   166 Nov  2 14:29 Makefile
-rw-r--r-- 1 root root    27 Nov  2 14:30 modules.order
-rw-r--r-- 1 root root     0 Nov  2 14:30 Module.symvers
原文地址:https://www.cnblogs.com/wangjq19920210/p/11782330.html