第一个内核模块hello world
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的更多相关文章
- linux 驱动模块 内核编译环境
目录(?)[+] Linux设备驱动Hello World程序介绍 如何编写一个简单的linux内核模块和设备驱动程序.我将学习到如何在内核模式下以三种不同的方式来打印hello world,这三种方 ...
- Linux设备驱动Hello World程序介绍
自古以来,学习一门新编程语言的第一步就是写一个打印“hello world”的程序(可以看<hello world 集中营>这个帖子供罗列了300个“hello world”程序例子)在本 ...
- linux内核编译与开发
一.Linux内核简介linux kernel map: linux 系统体系结构: linux kernel体系结构: arm有7种工作模式,x86也实现了4个不同级别RING0-RING3,RIN ...
- WinDbg 图形界面功能(三)
1.4.调试菜单 调试相关操作的功能菜单在这个下面,比如单步执行等. Go 单击Go调试菜单恢复 (或开始) 在目标上的执行. 此执行将继续,直到抵达某个断点. 异常或事件发生时,该过程结束或调试器将 ...
- ApacheCN Linux 译文集 20211129 更新
笨办法学 Linux 中文版 练习 0:起步 练习 1:文本编辑器,vim 练习 2:文本浏览器,少即是多 练习 3:Bash:Shell..profile..bashrc..bash_history ...
- 《Linux内核设计与实现》读书笔记 第一、二章
第一章 Linux内核简介 1.1Unix历史 Unix特点:1.很简洁 2.所有东西都被当成文件对待 3.Unix内核和相关的系统工具软件都是用C语言编写而成 4.进程创建非常迅速 所以Uni ...
- Linux内核模块设计
内核的设计有两种方式:单内核和微内核,两者各有优劣,关于两者的比较可以参见wiki.windowds和Solaris采用微内核结构. Linux内核采用单内核结构,设计比较简单,但单内核的理念是把所有 ...
- Linux内核模块简介
一. 摘要 这篇文章主要介绍了Linux内核模块的相关概念,以及简单的模块开发过程.主要从模块开发中的常用指令.内核模块程序的结构.模块使用计数以及模块的编译等角度对内核模块进行介绍.在Linux系统 ...
- 如何增强 Linux 系统的安全性,第一部分: Linux 安全模块(LSM)简介
http://www.ibm.com/developerworks/cn/linux/l-lsm/part1/ 1.相关背景介绍:为什么和是什么 近年来Linux系统由于其出色的性能和稳定性,开放源代 ...
随机推荐
- 用Visual studio2012在Windows8上开发内核驱动监视线程创建
在Windows NT中,80386保护模式的“保护”比Windows 95中更坚固,这个“镀金的笼子”更加结实,更加难以打破.在Windows 95中,至少应用程序I/O操作是不受限制的,而在Win ...
- django.db.utils.OperationalError: no such table: auth_user
关于使用django 首次创建超级管理员时,出现 django.db.utils.OperationalError: no such table: auth_user 错误 1.首先使用命 ...
- 如何让maven 将工程依赖的jar 复制到WEB-INF/lib 目录下
1.在默认生命周期的compile阶段执行 maven-dependency-plugin:copy-dependencies命令即可:<plugin><groupId>org ...
- maven 私服中央库使用阿里云库
1.admin登录 进入remote repositories management 2. 设置地址
- Ubuntu下安装java
1.首先到java.com下载最新版本的jdk.下面是jdk8的网址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-dow ...
- Atitit. Gui控件and面板----程序快速启动区--最佳实践Launchy ObjectDock-o0g
Atitit. Gui控件and面板----程序快速启动区--最佳实践Launchy ObjectDock-o0g 两个方式::: 键盘式::先用热键呼叫出QS,然后开始输入程序中的部分字母,按En ...
- 再谈API GateWay服务网关
前面在谈微服务架构的时候,我博客上转过Chris Richardson 微服务系列中对微服务网关的描述: 通常来说,使用 API 网关是更好的解决方式.API 网关是一个服务器,也可以说是进入系统的唯 ...
- HDU 3336 Count the string 查找匹配字符串
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Mac里面如何设置自启动服务
Mac OS x 启动项设置 Mac OS X的启动原理: 1,mac固件激活,初始化硬件,加载BootX引导器. 2,BootX加载内核与内核扩展(kext). 3,内核启动launchd进程. 4 ...
- 根据返回值动态加载select
// 路由 if (return_routeChoice != null && return_routeChoice != "") { for (var i = 0 ...