博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【TINY4412】LINUX学习笔记:(2)内核模块编译、安装、加载、卸载
阅读量:4087 次
发布时间:2019-05-25

本文共 5406 字,大约阅读时间需要 18 分钟。

【TINY4412】LINUX学习笔记:(2)内核模块编译、安装、加载、卸载

宿主机 : 虚拟机 Ubuntu 16.04 LTS / X64

目标板[底板]: Tiny4412SDK - 1506
目标板[核心板]: Tiny4412 - 1412
LINUX内核: 4.12.0
交叉编译器: gcc-arm-none-eabi-5_4-2016q3
日期: 2017-8-8 21:52:29
作者: SY

简介

编译内核模块和编译内核类似,是将目标内核放在宿主机中编译,使用交叉编译器,就是借鸡生蛋

编译内核模块

root@ubuntu:/opt/linux-4.12# make modules

安装内核模块

方式一:默认安装路径

root@ubuntu:/opt/linux-4.12# make modules_install

默认安装位置/lib/modules/

root@ubuntu:/opt/linux-4.12# ls /lib/modules/4.12.0-g16504fa/build/               modules.builtin      modules.devname      modules.symbols.binkernel/              modules.builtin.bin  modules.order        source/modules.alias        modules.dep          modules.softdep      modules.alias.bin    modules.dep.bin      modules.symbols

在这个目录有一个build文件夹很有用,以后写一个独立的外部模块时,不用编译内核了,只要进入路径:/lib/modules/4.12.0-g16504fa/build即可编译依赖于此内核的模块。

方式二:自定义路径

root@ubuntu:/opt/linux-4.12# make modules_install INSTALL_MOD_PATH=/opt/fs/rootfs/rootfs/root@ubuntu:/opt/fs/rootfs/rootfs/lib/modules# cd /opt/fs/rootfs/rootfs/lib/modules/4.12.0-g16504fa-dirty/root@ubuntu:/opt/fs/rootfs/rootfs/lib/modules/4.12.0-g16504fa-dirty# lsbuild          modules.alias.bin    modules.dep      modules.order    modules.symbols.binkernel         modules.builtin      modules.dep.bin  modules.softdep  sourcemodules.alias  modules.builtin.bin  modules.devname  modules.symbols

手动指定模块安装路径,将内核模块安装到rootfs中。

编译模块

方式一:如果在menuconfig中,配置为[M], 都会被编译。编译的文件名为xxx.ko

root@ubuntu:/opt/linux-4.12# make modules  CHK     include/config/kernel.release  CHK     include/generated/uapi/linux/version.h  CHK     include/generated/utsrelease.h  CHK     include/generated/bounds.h  CHK     include/generated/timeconst.h  CHK     include/generated/asm-offsets.h  CALL    scripts/checksyscalls.sh  CHK     scripts/mod/devicetable-offsets.h  CC [M]  drivers/tiny4412/hello_world.o  Building modules, stage 2.  MODPOST 39 modules  LD [M]  drivers/md/dm-crypt.ko  CC      drivers/tiny4412/hello_world.mod.o  LD [M]  drivers/tiny4412/hello_world.koroot@ubuntu:/opt/linux-4.12# cd drivers/tiny4412/root@ubuntu:/opt/linux-4.12/drivers/tiny4412# lshello_world.c  hello_world.ko  hello_world.mod.c  hello_world.mod.o  hello_world.o  Kconfig  Makefile  modules.order

方式二:在内核源码的待编译的驱动目录,编译当前目录的模块。

root@ubuntu:/opt/linux-4.12# cd drivers/tiny4412/root@ubuntu:/opt/linux-4.12/drivers/tiny4412# lshello_world.c  Kconfig  Makefileroot@ubuntu:/opt/linux-4.12/drivers/tiny4412# make -C /opt/fs/rootfs/rootfs/lib/modules/4.12.0-ge0f7e9e-dirty/build M=$PWDmake: Entering directory '/opt/linux-4.12'  LD      /opt/linux-4.12/drivers/tiny4412/built-in.o  CC [M]  /opt/linux-4.12/drivers/tiny4412/hello_world.o  Building modules, stage 2.  MODPOST 1 modules  CC      /opt/linux-4.12/drivers/tiny4412/hello_world.mod.o  LD [M]  /opt/linux-4.12/drivers/tiny4412/hello_world.komake: Leaving directory '/opt/linux-4.12'root@ubuntu:/opt/linux-4.12/drivers/tiny4412# lsbuilt-in.o     hello_world.ko     hello_world.mod.o  Kconfig   modules.orderhello_world.c  hello_world.mod.c  hello_world.o      Makefile  Module.symvers

方式三:脱离linux内核源码,在任意目录编译指定模块

root@ubuntu:/# cd /opt/temp/temproot@ubuntu:/opt/temp/temp# lshello_world.c  Makefile

需要加入下面的Makefile文件

# Linux modules compile# Author : SY# Time   : 2017-8-8 21:30:31#############################################################################obj-m       := hello_world.oCROSS       := arm-none-eabi-KDIR        := /opt/fs/rootfs/rootfs/lib/modules/4.12.0-ge0f7e9e-dirty/buildNPWD         := $(shell pwd)INSTALL_DIR := /opt/fs/rootfs/rootfs/default:        $(MAKE) -C $(KDIR) M=$(PWD)install:        $(MAKE) -C $(KDIR) M=$(PWD) modules_install INSTALL_MOD_PATH=$(INSTALL_DIR)clean:        rm -rf *.o *.ko *.mod.c *.temp_versions *.symvers *.order
root@ubuntu:/opt/temp/temp# makemake -C /opt/fs/rootfs/rootfs/lib/modules/4.12.0-ge0f7e9e-dirty/build M=/opt/temp/tempmake[1]: Entering directory '/opt/linux-4.12'  LD      /opt/temp/temp/built-in.o  CC [M]  /opt/temp/temp/hello_world.o  Building modules, stage 2.  MODPOST 1 modules  CC      /opt/temp/temp/hello_world.mod.o  LD [M]  /opt/temp/temp/hello_world.komake[1]: Leaving directory '/opt/linux-4.12'root@ubuntu:/opt/temp/temp# lsbuilt-in.o     hello_world.ko     hello_world.mod.o  Makefile       Module.symvershello_world.c  hello_world.mod.c  hello_world.o      modules.order

安装模块

root@ubuntu:/opt/temp/temp# make installmake -C /opt/fs/rootfs/rootfs/lib/modules/4.12.0-ge0f7e9e-dirty/build M=/opt/temp/temp modules_install INSTALL_MOD_PATH=/opt/fs/rootfs/rootfs/make[1]: Entering directory '/opt/linux-4.12'  INSTALL /opt/temp/temp/hello_world.ko  DEPMOD  4.12.0-ge0f7e9e-dirtymake[1]: Leaving directory '/opt/linux-4.12'

将会安装到路径/opt/fs/rootfs/rootfs/lib/modules/4.12.0-ge0f7e9e-dirty/extra

root@ubuntu:/opt/linux-4.12# cd /opt/fs/rootfs/rootfs/lib/modules/4.12.0-ge0f7e9e-dirty/extra/root@ubuntu:/opt/fs/rootfs/rootfs/lib/modules/4.12.0-ge0f7e9e-dirty/extra# lshello_world.ko

加载模块

方式一:insmod

[root@TINY4412:/tmp]# insmod hello_world.ko

方式二:modprobe

[root@TINY4412:~]# modprobe hello_worldmodprobe: module 'hello_world.ko' not found[root@TINY4412:~]# depmod[root@TINY4412:~]# modprobe hello_world[14384.366786] ------------------- hello_world_probe

insmodmodprobe的区别:

insmod:只能加载指定路径的驱动文件

modprobe:只能加载/lib/modules/4.12.0-ge0f7e9e-dirty/路径下的驱动文件。

查看模块

[root@TINY4412:/tmp]# lsmod hello_world.ko hello_world 16384 0 - Live 0xbf008000 (O)

卸载模块

[root@TINY4412:/tmp]# rmmod hello_world.ko

转载地址:http://inzii.baihongyu.com/

你可能感兴趣的文章
工具类关闭流
查看>>
类/对象大小的计算
查看>>
智能指针shared_ptr<T>
查看>>
C语言中的结构体内存对其
查看>>
shared_ptr<T>注意事项
查看>>
代码复用的另一种方式,使用适配器
查看>>
模板类中的成员模板
查看>>
typename关键字
查看>>
c++中字符串反转的3种方法
查看>>
单向链表的冒泡排序
查看>>
stl中的remove算法
查看>>
STL中的remove算法和vector中erase和resize函数
查看>>
C++中string::npos
查看>>
LeetCode22GenerateParentheses--In Java
查看>>
java实现的俄罗斯方块游戏--powered by dustin
查看>>
Java3*3拼图小游戏--powered by dustin
查看>>
LeetCode27RemoveElement--In Java
查看>>
LeetCode29DivideTwoIntegers--In Java
查看>>
LeetCode 30 Substring with Concatenation of All Words--In Java
查看>>
LeetCode 40 Combination Sum II--In Java
查看>>