第一个内核模块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系统由于其出色的性能和稳定性,开放源代 ...
随机推荐
- 破解Aspose 操作pdf word等文档 对10以下版本有效
using System; using System.IO; namespace LicenseHelper { public static class License { public const ...
- WinForm下的键盘事件(KeyPress、KeyDown)及如何处理不响应键盘事件
KeyDown事件用来处理功能键:F1 F2 F3... keyPress事件用来处理字符比如说:A B C... 1 2 3... 注:处理该事件时,需要先将窗体的 KeyPreview=true; ...
- assert 函数
assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义: #include <assert.h> void assert( i ...
- highcharts图表史上最全的参数配置(属性+事件)
今天这里将给大家全全展现相关的参数配置: chart.events.addSeries:添加数列到图表中. chart.events.click:整个图表的绘图区上所发生的点击事件. chart.ev ...
- 取得COM对象的UUID并以string输出
IID tmp = __uuidof(ClassLibrary1::Class1); OLECHAR * buf; StringFromIID(tmp, &buf); 好像要#include ...
- NB 命令安装需似机(无图型化安装)
[root@ok ~]# virt-install -v -n 09ng04 -r 512 --vcpus=4 --location=/home/ISO/CentOS-6.7-x86_64-bin-D ...
- 从零开始,跟我一起做jblog项目(一)引言
从零开始,跟我一起做jblog项目(一)引言 想做一个java版的blog,源自一个很久之前的想法 当时刚学习JAVA的web编程 想买自己的域名,自己的VPS,安装自己的WEB服务 用google ...
- CentOS minimal 版安装图形界面的步骤分享,中文语言包
1.连接网络: CentOS minimal.iso安装好后,进入终端,默认是不开网络的, 首先启用网卡, 自动获取ip. ifconfig eth0 up dhclient eth0 这时候再 if ...
- python操作word
python教程(百度经验) Python 操作Word(Excel.PPT等通用) import win32comfrom win32com.client import Dispatch, co ...
- MyBatis Geneator详解<转>
MyBatis Geneator中文文档地址: http://generator.sturgeon.mopaas.com/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中文版的文档的 ...