本节所讲内容:
使用Linux内核模块
实战-编译一个NTFS内核模块,实现linux挂载NTFS文件系统并实现读写功能
内核模块操作
内核(模块)管理命令:
模块的作用:把具体功能封装好的程序。 比如,冲电宝。可以给所有手机冲电。
存放内核模块的目路径:
[[email protected]]# ls
3.10.0-229.el7.x86_64 4.2.4
[[email protected]]# cd /lib/modules/4.2.4/kernel/fs/
[[email protected] fs]# ls
binfmt_misc.ko ceph dlm fat gfs2 lockd nfs_common ntfs squashfs
btrfs cifs exofs fscache isofs mbcache.ko nfsd overlayfs udf
cachefiles cramfs ext4 fuse jbd2 nfs nls pstore xfs
[[email protected] fs]# cd fat/
[[email protected] fat]#ls
fat.ko msdos.ko vfat.ko
插入模块:
insmod
作用:载入模块
[[email protected] fat]#pwd
/lib/modules/4.2.4/kernel/fs/fat
[[email protected] fat]#insmod fat.ko
查看当前系统中加载的模块
#lsmod 列出当前模块
[[email protected] fat]#lsmod | grep fat
fat 69632 0
删除模块
#rmmod fat #删除模块 。跟上模块的名字
[[email protected] fat]#rmmod fat
[[email protected] fat]#lsmod | grep fat
解决依赖关系安装模块
Modprobe
[[email protected] fat]# insmodvfat.ko
insmod: ERROR: could notinsert module vfat.ko: Unknown symbol in module
[[email protected] fat]# modprobevfat
[[email protected] fat]# lsmod |grep fat
vfat 20480 0
fat 69632 1 vfat
删除模块
[[email protected] fat]#modprobe -r vfat
[[email protected] fat]#lsmod | grep fat
新添加一个模块后,需要让系统知道:
[[email protected] 4.2.4]#depmod
[[email protected] fat]#cd /lib/modules/4.2.4/
[[email protected] 4.2.4]#vim modules.dep
查看模块间依赖关系:如fat。从下图可以看出,vfat和msdos模块都依赖fat.ko模块
文件格式
模块的路径: 依赖的模块
查看模块信息
#modinfo 查看模块信息
[[email protected] fat]#modinfo vfat
filename: /lib/modules/4.2.4/kernel/fs/fat/vfat.ko
author: Gordon Chaffee
description: VFAT filesystem support
license: GPL
alias: fs-vfat
srcversion: BEF7BA1F8972367DD650AA0
depends: fat
intree: Y
vermagic: 4.2.4 SMP mod_unload modversions
signer: Build time autogenerated kernel key
sig_key: D3:5F:6B:F2:8E:BB:94:CD:66:3F:5C:D7:C6:F5:17:68:E9:30:25:B7
sig_hashalgo: sha256
开机自动加载模块
[[email protected] ~]# vim/etc/rc.local
#!/bin/bash
# THIS FILE IS ADDEDFOR COMPATIBILITY PURPOSES
#
# It is highlyadvisable to create own systemd services or udev rules
# to run scripts duringboot instead of using this file.
#
# In contrast to previousversions due to parallel execution during boot
# this script will NOTbe run after all other services.
#
# Please note that youmust run ‘chmod +x /etc/rc.d/rc.local‘ to ensure
# that this script willbe executed during boot.
touch /var/lock/subsys/local
modprobe fat
[[email protected] ~]# ll/etc/rc.local
lrwxrwxrwx. 1 root root13 Nov 22 03:29 /etc/rc.local -> rc.d/rc.local
实战-编译一个NTFS内核模块,实现linux挂载NTFS文件系统
安装 fuse:
[[email protected]]# rpm -qa | grep fuse
fuseiso-20070708-15.el7.x86_64
gvfs-fuse-1.16.4-8.el7.x86_64
fuse-libs-2.9.2-5.el7.x86_64
fuse-2.9.2-5.el7.x86_64
[[email protected] ~]#modprobe ntfs
编译安装ntfs-3g源码包
[[email protected] ~]# tarzxvf ntfs-3g-2011.1.15.tgz
[[email protected] ~]# cdntfs-3g-2011.1.15/
[[email protected] ~]#./configure && make && make install
导入U盘
[[email protected] ~]#umount /dev/sdc1
[[email protected] ~]#mount -t ntfs-3g /dev/sdc1 /opt/
[[email protected] ~]# vim/etc/fstab
/dev/sdc1 /opt ntfs-3g defaults
学习过程中如果问题,请留言。更多内容请加:
学神IT-linux讲师-RM老师QQ:2805537762
学神IT-戚老师QQ:3341251313
学神IT-旭斌QQ:372469347
学神IT教育RHEL7交流群:468845589