【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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章