2018年11月

嵌入式、物联网技术交流分享

Linux 内核模块的相关操作命令

lsmod 查看已经安装的模块 lsmod Module Size Used by binfmt_misc 20480 1 vmw_vsock_vmci_transport 28672 2 vsock 36864 3 vmw_vsock_vmci_transport snd_ens1371 28672 2 snd_ac97_codec 131072 1 snd_ens1371 gameport 16384 1 snd_ens1371 ac97_bus 16384 1 snd_ac97_codec ... modinfo 显示模块信息 modinfo nfs.ko filename: /home/matt/test/kernel/linux-3.2.0-psp04.06.00.11/fs/nfs/nfs.ko license: GPL author: Olaf Kirch <okir@monad.swb.de> depends: sunrpc,lockd intree: Y vermagic: 3.2.0 mod_unload modversions ARMv7 p2v8 parm: cache_getent:Path to the client cache upcall program (string) parm: cache_getent_timeout:Timeout (in seconds) after which the cache upcall is assumed to have failed (ulong) parm: enable_ino64:bool parm: nfs4_disable_idmapping:Turn off NFSv4 idmapping when using 'sec=sys' (bool) depends 显示出了模块的依赖项 insmod 加载指定位置的模块 insmod /path-to-file/nfs.ko 如果依赖模块没有安装会提示 Unknown symbol... 使用时要指定模块的绝对路径。 rmmod 卸载驱动模块 rmmod <module_name> 注意其中 ”module_name” 是 lsmod 显示的模块名称,而不是对应的 ko 文件名 modprobe 用于挂载内核模块,挂载模块时不用指定模块文件的路径,也不用带文件的后缀。 modprobe nfs 相比 insmod 更智能些,不过限定了 ko 文件的位置。 实例: modprobe nfs [ 207.024183] RPC: Registered named UNIX socket transport module. [ 207.030461] RPC: Registered udp transport module. [ 207.035397] RPC: Registered tcp transport module. [ 207.040317] RPC: Registered tcp NFSv4.1 backchannel transport module. 卸载模块: modprobe -r nfs modprobe: remove[ 145.687621] RPC: Unregistered named UNIX socket transport module. 'sunrpc': Resou[ 145.694428] RPC: Unregistered udp transport module. rce temporarily [ 145.700954] RPC: Unregistered tcp transport module. unavailable [ 145.707460] RPC: Unregistered tcp NFSv4.1 backchannel transport module. depmod 用于分析可载入模块的相依性,供 modprobe 在安装模块时使用。 depmod 通过读取 /lib/modules/$(uname -r) 目录下的每一个模块来创建一个记录模块相依性的列...

以动态模块 ko 的形式使用 nfs 客户端

ARM 设备的的内核中没有编进去 NFS,所以打算采用 ko 的方式加载进内核。 编译内核源程序,产生 Makefile。 make menuconfig 配置内核: [*] Network File Systems ---> <M> NFS client support [*] NFS client support for NFS version 3 把 NFS client support 选做模块(M)。 make modules 生成 nfs.ko、sunrpc.ko、lockd.ko 拷贝到 /lib/modules/$(uname -r) 执行 depmod 执行 modprobe nfs [ 207.024183] RPC: Registered named UNIX socket transport module. [ 207.030461] RPC: Registered udp transport module. [ 207.035397] RPC: Registered tcp transport module. [ 207.040317] RPC: Registered tcp NFSv4.1 backchannel transport module...

创建一个 Linux 内核模块实例

源代码: #include <linux/init.h> //包含模块初始化、清除函数 #include <linux/module.h> //包含许多符号和函数的定义,模块的定义 #include <linux/kernel.h> static int __init hello_init(void) { printk(KERN_ALERT "Hello World!\n"); //内核中的打印只有printk,而且分打印级别 return 0; } static void __exit hello_exit(void) { printk(KERN_ALERT "Goodbye, see you again!\n"); } module_init(hello_init); //函数声明,把代码段放到init段;module_init是该程序的入口函数 //当系统启动init程序的时候,执行module_init()里的函数 module_exit(hello_exit); //当系统启动exit程序的时候,执行module_exi()里的函数 //没有module声明的函数(普通函数)会放到代码段(文本段) MODULE_AUTHOR("Matt <matt@mculoop.com>"); //声明作者 MODULE_DESCRIPTION("Linux Kernel Hello World Module (C) 2018"); //模块描述 MODULE_LICENSE("GPL"); //声明LICENSE Makefile: # To build modules outside of the kernel tree, we run "make" # in the kernel source tree; the Makefile these then includes this # Makefile once again. # This conditional selects whether we are being included from the # kernel Makefile or not. # called from kernel build system: just declare what our modules are obj-m := helloworld.o # Assume the source tree is where the running kernel was built # You should set KERNELDIR in the environment if it's elsewhere KERNELDIR ?= /home/matt/test/kernel/linux-3.2.0-psp04.06.00.11 # The current directory is passed to sub-makes as argument PWD := $(shell pwd) all: modules modules: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules clean: rm -rf *.o *~ core .depend *.symvers .*.cmd *.ko *.mod.c .tmp_versions $(TARGET) 编译: 编译前应当确保已经编译过内核,因为要使用内核的Makefile。 mak...

AM335X 内核编译

内核编译: sudo apt-get install u-boot-tools #uImage 生成时依赖的 mkimage 工具 make mrproper make am335x_evm_defconfig make menuconfig make -j4 ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- uImage 模块编译: make modules make modules_install INSTALL_MOD_PATH=./module...

解决错误 modinfo can't open '...modules.dep' No such file or directory

在 ARM 开发板上查看模块信息提示题中的错误: modinfo: can't open '/lib/modules/3.2.0/modules.dep': No such file or directory 解决办法是: 1、创建文件夹:/lib/modules/$(uname -r) 2、cp xx.ko /lib/modules/3.2.0/ 3、depmod 4、mv /lib/modules/3.2.0/modules.dep.bb /lib/modules/3.2.0/modules.dep 5、modinfo xx.ko 这下就正常了 又发现: 其它模块不用重复这样做 为什么其它模块不需要这样做的?是不是只要有 modules.dep 这么一个文件名的文件存在于这里就可以来了? 再次发现: 在这里创建一个空文件就行了! touch modules.dep 那么,在 /lib/modules/3.2.0/ 中没有 ko 文件的时候,直接执行 depmod 命令是不是也可以产生空文件?答案是:是的,不过产生的是 modules.dep.bb 。为什么不直接产生 modules.dep 呢?还不清楚。有一点是可以肯定的,modinfo 使用前提是存在 modules.dep,空的就行,模块信息是从 ko 文件中获取的,跟 modules.dep 没关系。不过,为什么非要存在 modules.dep 文件呢?这也还不清楚。 我在 Ubuntu 18.04 中做了测试,内核版本是 4.15.0-36,执行 depmod 没有 modules.dep.bb 产生,直接产生 modules.dep,并且文件的内容格式与 3.2.0 中的也不一...

Linux 查看 flash 分区命令

/proc/mtd 可以显示出所有挂载和未挂载的分区,但不显示文件系统类型。 cat /proc/mtd mtd0: 00080000 00020000 "MLO" mtd1: 00200000 00020000 "U-Boot" mtd2: 00580000 00020000 "Kernel" mtd3: 00800000 00020000 "File System" mtd4: 07000000 00020000 "app" df 可以查看已经挂载的分区和文件系统类型。 df -a Filesystem 1K-blocks Used Available Use% Mounted on ubi0:rootfs 4584 4576 8 100% / devtmpfs 127488 0 127488 0% /dev ramfs 0 0 0 0% /var ramfs 0 0 0 0% /tmp proc 0 0 0 0% /proc sysfs 0 0 0 0% /sys devpts 0 0 0 0% /dev/pts tmpfs 127592 0 127592 0% /dev/shm ubi1_0 89796 32 89764 0% /app fdisk 可以显示出所有挂载和未挂载的分区,但不显示文件系统类型。 fdisk -l Disk /dev/mtdblock0: 0 MB, 524288 bytes 255 heads, 63 sectors/track, 0 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk /dev/mtdblock0 doesn't contain a valid partition table Disk /dev/mtdblock1: 2 MB, 2097152 bytes 255 heads, 63 sectors/track, 0 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk /dev/mtdblock1 doesn't contain a valid partition table Disk /dev/mtdblock2: 5 MB, 5767168 bytes 255 heads, 63 sectors/track, 0 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk /dev/mtdblock2 doesn't contain a valid partition table Disk /dev/mtdblock3: 8 MB, 8388608 bytes 255 heads, 63 sectors/track, 1 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk /dev/mtdblock3 doesn't contain a valid partition table Disk /dev/mtdblock4: 117 MB, 117440512 bytes 255 heads, 63 sectors/track, 14 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk /dev/mtdblock4 doesn't contain a valid partition tabl...