how to write your first linux device driver

0. environment
-ubuntu 1804 64bit

1. apt-get install linux-headers-$(uname -r)

2. code hello.c

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void)
{
printk(KERN_ALERT "Hello, solidmango\n");
return ;
} static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, solidmango\n");
} module_init(hello_init);
module_exit(hello_exit);

3. Makefile

# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq ($(KERNELRELEASE),)
obj-m := hello.o
# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif

4. sudo insmod hello.ko

5. sudo rmmod hello

6. view result

sm@ubuntu:~/dev$ cat /var/log/syslog|grep solidmango
Sep :: ubuntu kernel: [ 3041.143749] Hello, solidmango
Sep :: ubuntu kernel: [ 3068.192172] Goodbye, solidmango

7. sign your kernel module

/lib/modules/4.15./build/scripts/sign-file sha512 /lib/modules/4.15./build/certs/signing_key.pem /lib/modules/4.15./build/certs/signing_key.x509 hello.ko

signing_key.pem and signing_key.x509 files are generated when build the linux kernel

reference
https://wiki.gentoo.org/wiki/Signed_kernel_module_support#Enabling_module_signature_verification

how to write your first linux device driver的更多相关文章

  1. linux device driver —— 环形缓冲区的实现

    还是没有接触到怎么控制硬件,但是在书里看到了一个挺巧妙的环形缓冲区实现. 此环形缓冲区实际为一个大小为bufsize的一维数组,有一个rp的读指针,一个wp的写指针. 在数据满时写进程会等待读进程读取 ...

  2. Linux Device Driver 学习(1)

    Linux Device Driver 学习(1) 一.搭建虚拟机开发环境 1.选择虚拟机VirtualBox,官网下载.deb包安装: VirtualBox Linux 5.1.6 下载fedora ...

  3. Linux Device Driver && Device File

    catalog . 设备驱动程序简介 . I/O体系结构 . 访问设备 . 与文件系统关联 . 字符设备操作 . 块设备操作 . 资源分配 . 总线系统 1. 设备驱动程序简介 设备驱动程序是内核的关 ...

  4. How to learn linux device driver

    To learn device driver development, like any other new knowledge, the bestapproach for me is to lear ...

  5. <<linux device driver,third edition>> Chapter 4:Debugging Techniques

    Debugging by Printing printk lets you classify messages accoring to their severity by associating di ...

  6. <<linux device driver,third edition>> Chapter 3:Char Drivers

    The Internal Representation of Device Numbers Within the kernel,the dev_t type(defined in linux/type ...

  7. linux device driver —— ioctl

    实现了应用程序和设备驱动通过ioctl通信.还是对设备驱动没什么感觉,贴一下代码吧. 在Ubuntu 16.04 64bit中测试通过 ioctldemo.c #include <linux/m ...

  8. linux device driver —— 字符设备

    现在对linux设备驱动还没有什么认识,跟着书上敲了一个字符驱动,这里把代码贴一下. 测试环境是 Ubuntu 16.04 64bit 驱动程序: #include <linux/fs.h> ...

  9. <<linux device driver,third edition>> Chapter 2: Building and Running Modules

    Kernel Modules Versus Applications Kernel modules programming is similar to event driven programming ...

随机推荐

  1. Kafka学习笔记之K8S内filebeat传输到kafka报错带解决方案

    0x00 概述 filebeat非常轻量级,正常情况下占用的资源几乎都能忽略不计,但是部署后发现资源占用很大,所以怀疑是filebeat本身出了问题. 第一时间查看filebeat日志(默认路径/va ...

  2. window下使用curl操作elasticsearch

    1.下载curlzip,https://curl.haxx.se/download.html; 2.解压,在bin文件夹中找到curl.exe,右键“以管理员身份运行”,cmd e: 换盘符:出现E: ...

  3. Java面试必问通信框架NIO,原理详解

    NIO 流与块 通道与缓冲区 缓冲区状态变量 文件 NIO 实例 选择器 套接字 NIO 实例 内存映射文件 NIO与IO对比 Path Files NIO 新的输入/输出 (NIO) 库是在 JDK ...

  4. 自定义Robotframework,Appium的一个关键字(用于点击目标图片,用于Appium无法识别的一些图片元素)

     Appium无法识别的一些图片元素,必须先通过图片找坐标,进而通过点击坐标解决问题. 1.先在terminer运行安装命令: pip install robotframework-appiumlib ...

  5. HTML 从入门到精通 [目录]

    目录 一.服务器的认识 二.浏览器的认识 三.Web 标准 四.HTML 的认识 五.HTML 文本标签 六.HTML 图像标签 七.HTML 路径 八.HTML 链接标签 九.HTML 列表 十.H ...

  6. Java程序员的魔法杖-Arthas 3.1.2版本发布了

    Arthas已经成为我日常运维.线上排查的必备之品,听说最近更新版本了,今天这篇文章看下又增加了什么新的能力. Arthas是Alibaba开源的Java诊断工具,深受开发者喜爱. Github:ht ...

  7. CDA数据分析【数据收集】

    一.机器收集数据 机器收集数据会从不同角度对数据进行抓取和采集,与之前手动收集数据不同,机器收集数据不再是用小样本.特定样本来采集和分析整体数据,而是采用大样本或整体数据进行分析,这打破了原来的数据分 ...

  8. C++ OpenSSL 之五:生成P12文件

    1.等同于使用: openssl pkcs12 -export -inkey "key_path" -in "pem_path" -out "save ...

  9. django logger转载

    https://www.cnblogs.com/jiangchunsheng/p/8986452.html https://www.cnblogs.com/jeavy/p/10926197.html ...

  10. qtcreator VLD内存检测

    简介 Visual Leak Detector是一款用于Visual C++的免费的内存泄露检测工具.相比较其它的内存泄露检测工具,它在检测到内存泄漏的同时,还具有如下特点: 可以得到内存泄漏点的调用 ...