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

【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.bin
kernel/              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# ls
build          modules.alias.bin    modules.dep      modules.order    modules.symbols.bin
kernel         modules.builtin      modules.dep.bin  modules.softdep  source
modules.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.ko

root@ubuntu:/opt/linux-4.12# cd drivers/tiny4412/
root@ubuntu:/opt/linux-4.12/drivers/tiny4412# ls
hello_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# ls
hello_world.c  Kconfig  Makefile

root@ubuntu:/opt/linux-4.12/drivers/tiny4412# make -C /opt/fs/rootfs/rootfs/lib/modules/4.12.0-ge0f7e9e-dirty/build M=$PWD
make: 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.ko
make: Leaving directory '/opt/linux-4.12'
root@ubuntu:/opt/linux-4.12/drivers/tiny4412# ls
built-in.o     hello_world.ko     hello_world.mod.o  Kconfig   modules.order
hello_world.c  hello_world.mod.c  hello_world.o      Makefile  Module.symvers

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

root@ubuntu:/# cd /opt/temp/temp
root@ubuntu:/opt/temp/temp# ls
hello_world.c  Makefile

需要加入下面的Makefile文件

# Linux modules compile
# Author : SY
# Time   : 2017-8-8 21:30:31
#############################################################################

obj-m       := hello_world.o
CROSS       := arm-none-eabi-
KDIR        := /opt/fs/rootfs/rootfs/lib/modules/4.12.0-ge0f7e9e-dirty/buildN
PWD         := $(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# make
make -C /opt/fs/rootfs/rootfs/lib/modules/4.12.0-ge0f7e9e-dirty/build M=/opt/temp/temp
make[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.ko
make[1]: Leaving directory '/opt/linux-4.12'
root@ubuntu:/opt/temp/temp# ls
built-in.o     hello_world.ko     hello_world.mod.o  Makefile       Module.symvers
hello_world.c  hello_world.mod.c  hello_world.o      modules.order

安装模块

root@ubuntu:/opt/temp/temp# make install
make -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-dirty
make[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# ls
hello_world.ko

加载模块

方式一:insmod

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

方式二:modprobe

[root@TINY4412:~]# modprobe hello_world
modprobe: 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

你可能感兴趣的:(TINY4412,LINUX)