最終更新:2017-06-27 (火) 18:26:22 (2487d)  

Linux/カーネルモジュール/開発/サンプル

hello.c

  • #include <linux/kernel.h>
    #include <linux/init.h>
    #include <linux/module.h>
      static int __init gotemp_init(void) {
      printk(KERN_INFO "Hello from the kernel!\n");
      return 0;
    }
    static void __exit gotemp_exit(void) {
    }
    module_init(gotemp_init);
    module_exit(gotemp_exit);
    MODULE_AUTHOR("My name here");
    MODULE_DESCRIPTION("Simple driver");
    MODULE_LICENSE("GPL");

Makefile

make

  • user@ubuntu:~/hello-ko$ make
    make -C /lib/modules/4.4.0-79-generic/build M=/home/user/hello-ko
    make[1]: Entering directory '/usr/src/linux-headers-4.4.0-79-generic'
      LD      /home/user/hello-ko/built-in.o
      CC [M]  /home/user/hello-ko/hello.o
      Building modules, stage 2.
      MODPOST 1 modules
      CC      /home/user/hello-ko/hello.mod.o
      LD [M]  /home/user/hello-ko/hello.ko
    make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-79-generic'
    

makeすると生成されるファイル

  • built-in.o
  • hello.ko
  • hello.mod.c
  • hello.mod.o
  • hello.o
  • modules.order
  • Module.symvers #サイズ0

ロードしたり

  • sudo insmod hello.ko

modinfo

  • user@ubuntu:~/hello-ko$ modinfo hello.ko
    filename:       /home/user/hello-ko/hello.ko
    license:        GPL
    description:    Simple driver
    author:         My name here
    srcversion:     45BF66629CF68BFBEB7FDC8
    depends:        
    vermagic:       4.4.0-79-generic SMP mod_unload modversions

参考