1、源码树的下载和编译(只是研究内核模块的话,应该不需要源码树的)

下载很简单,压缩包解压

编译:make menuconfig

make bzImage -j4

参考

2、

cd  /usr/src/linux-4.12.10/drivers/char

建立demo目录,用来存放自己的内核模块

demo目录下新建hello.c 和 Makefile文件

//hello.c

#include <linux/init.h>
#include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void)
{
printk(KERN_ALERT "hello world!\n");
return ;
} static void hello_exit(void)
{
printk(KERN_ALERT "goodbye!\n");
} module_init(hello_init);
module_exit(hello_exit); //Makefile ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
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

参考

编译结果:

ninjame@ubuntu1604:/usr/src/linux-4.12./drivers/char/demo$ sudo make
make -C /lib/modules/4.4.--generic/build M=/usr/src/linux-4.12./drivers/char/demo modules
make[]: Entering directory '/usr/src/linux-headers-4.4.80-040480-generic'
CC [M] /usr/src/linux-4.12./drivers/char/demo/hello.o
Building modules, stage .
MODPOST modules
CC /usr/src/linux-4.12./drivers/char/demo/hello.mod.o
LD [M] /usr/src/linux-4.12./drivers/char/demo/hello.ko
make[]: Leaving directory '/usr/src/linux-headers-4.4.80-040480-generic'

模块加载和效果

ninjame@ubuntu1604:/usr/src/linux-4.12./drivers/char/demo$ sudo insmod hello.ko
ninjame@ubuntu1604:/usr/src/linux-4.12./drivers/char/demo$ dmesg | tail
[ 15.300169] r8169 ::00.0 enp1s0: link down
[ 15.300185] r8169 ::00.0 enp1s0: link down
[ 15.300230] IPv6: ADDRCONF(NETDEV_UP): enp1s0: link is not ready
[ 16.840866] r8169 ::00.0 enp1s0: link up
[ 16.840873] IPv6: ADDRCONF(NETDEV_CHANGE): enp1s0: link becomes ready
[ 18.016626] Bluetooth: BNEP (Ethernet Emulation) ver 1.3
[ 18.016628] Bluetooth: BNEP filters: protocol multicast
[ 18.016631] Bluetooth: BNEP socket layer initialized
[12219.748060] hello: module verification failed: signature and/or required key missing - tainting kernel
[12219.748268] hello world!

第一个内核模块hello world的更多相关文章

  1. linux 驱动模块 内核编译环境

    目录(?)[+] Linux设备驱动Hello World程序介绍 如何编写一个简单的linux内核模块和设备驱动程序.我将学习到如何在内核模式下以三种不同的方式来打印hello world,这三种方 ...

  2. Linux设备驱动Hello World程序介绍

    自古以来,学习一门新编程语言的第一步就是写一个打印“hello world”的程序(可以看<hello world 集中营>这个帖子供罗列了300个“hello world”程序例子)在本 ...

  3. linux内核编译与开发

    一.Linux内核简介linux kernel map: linux 系统体系结构: linux kernel体系结构: arm有7种工作模式,x86也实现了4个不同级别RING0-RING3,RIN ...

  4. WinDbg 图形界面功能(三)

    1.4.调试菜单 调试相关操作的功能菜单在这个下面,比如单步执行等. Go 单击Go调试菜单恢复 (或开始) 在目标上的执行. 此执行将继续,直到抵达某个断点. 异常或事件发生时,该过程结束或调试器将 ...

  5. ApacheCN Linux 译文集 20211129 更新

    笨办法学 Linux 中文版 练习 0:起步 练习 1:文本编辑器,vim 练习 2:文本浏览器,少即是多 练习 3:Bash:Shell..profile..bashrc..bash_history ...

  6. 《Linux内核设计与实现》读书笔记 第一、二章

    第一章    Linux内核简介 1.1Unix历史 Unix特点:1.很简洁 2.所有东西都被当成文件对待 3.Unix内核和相关的系统工具软件都是用C语言编写而成 4.进程创建非常迅速 所以Uni ...

  7. Linux内核模块设计

    内核的设计有两种方式:单内核和微内核,两者各有优劣,关于两者的比较可以参见wiki.windowds和Solaris采用微内核结构. Linux内核采用单内核结构,设计比较简单,但单内核的理念是把所有 ...

  8. Linux内核模块简介

    一. 摘要 这篇文章主要介绍了Linux内核模块的相关概念,以及简单的模块开发过程.主要从模块开发中的常用指令.内核模块程序的结构.模块使用计数以及模块的编译等角度对内核模块进行介绍.在Linux系统 ...

  9. 如何增强 Linux 系统的安全性,第一部分: Linux 安全模块(LSM)简介

    http://www.ibm.com/developerworks/cn/linux/l-lsm/part1/ 1.相关背景介绍:为什么和是什么 近年来Linux系统由于其出色的性能和稳定性,开放源代 ...

随机推荐

  1. Scala, Groovy, Clojure, Jython, JRuby and Java ----我们的工作语言

    在曾经的一封邮件中,我指出在众多改变中,最明显的一个就是:在java领地上的JVM上使用其它流行的语言的发展变得越来越快.一些老的和新的创建的基于JVM的语言---JRuby 和 Jython ,Ja ...

  2. Lintcode---翻转二叉树

    翻转一棵二叉树 您在真实的面试中是否遇到过这个题? Yes 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 思路:依旧采用递归的思路,判断特殊条件后,先交换根节点的左右孩子, ...

  3. unity5, 在mac下多开

    mac上app的多开与app本身无关,而是系统本身的功能,使用命令 open -n 就可以实现打开某应用程序的一个新实例(自行输入man open查看含义). 参考:http://mac-how-to ...

  4. ubuntu获取root权限

    1.先在Terminal下设置root密码 执行 sudo passwd root 2.执行sudo su,然后根据提示来即可.

  5. hadoop修改

    https://github.com/medcl/elasticsearch-analysis-ik/releases hadoop-/etc/hadoop/core-site.xml <con ...

  6. jvm默认垃圾收集器

    jdk1.7 默认垃圾收集器Parallel Scavenge(新生代)+Parallel Old(老年代) jdk1.8 默认垃圾收集器Parallel Scavenge(新生代)+Parallel ...

  7. Mysql变量声明与使用

    set @today='2017-04-25';set @ydate=DATE_SUB(@today, INTERVAL 7 day);select @today, @ydate; 待续....

  8. 解决:ubuntu 里文件夹带锁

    sudo chown -R <user-name> <folder-name> /* 其中-R的意思是recursive,你懂的,chown --help可以查看帮助信息 */ ...

  9. hadoop3: mkdir: cannot create directory `/usr/local/hadoop/bin/../logs’: Permission denied

    1.hadoop3: mkdir: cannot create directory `/usr/local/hadoop/bin/../logs': Permission denied把所有Datan ...

  10. web.config中的connectionString里面应该怎么写?

    2009-09-16 10:19信欣玛利 | 浏览 6068 次  网络 就是具体格式.. 谁能举个例子? asp.net 3.5的. 2009-09-16 10:40   提问者采纳   这是用来连 ...