列出目录下的文件的内核模块

本文介绍了一个用于调试文件系统的内核模块,它能够列出挂载后的目录结构,通过命令行调用实现。模块包含配置宏、模块初始化和退出函数,支持自定义路径,默认为根目录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在调试一个文件系统时,需要看看挂载后文件系统的目录,写了一个列文件的内核模块。


使用方法:

    insmod fs_test.ko  path='/dev'


默认路径是/。 该模块不会被插入内核,列完目录后退出。


fs_test.h

#ifndef __FS_TEST_H__
#define __FS_TEST_H__

#define FS_TEST_DEBUG

#define MY_MODULE_NAME             "fs_test"

#define MY_MODULE_LICENSE          "GPL"
#define MY_MODULE_AUTHOR           "Hansel He"
#define MY_MODULE_DESCRIPTION      "Support File System Debug"

/*------------------------------------------------*/
#ifdef FS_TEST_DEBUG
#define FM_PRINTF(fmt, arg...)   printk("[fs_test: %s():%d] " fmt, __FUNCTION__, __LINE__, ##arg)
#define FM_PRINTFN(fmt, arg...)  FM_PRINTF(fmt "\n", ##arg)
#else
#define FM_PRINTF(fmt, arg...)
#define FM_PRINTFN(fmt, arg...)
#endif


/*---------------------------------------*/
typedef struct 
{
    int count;
}FS_TEST_DATA;

#endif

fs_test_main.c

/****************************************************************************************
 * File			:	fs_test.c
 *
 * Description	:	FS Debug Module
 * Version		:	1.0
 * 
 * 
 *
 * 
 * Change Log:
 *
 * Date          By         Description
 * ===========   ========   =============================================================
 * 2011.12.03    Hansel		Create
 ****************************************************************************************/

#include <linux/config.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/version.h>
#include <linux/proc_fs.h>
#include <linux/init.h>
#include <linux/string.h>

#include <linux/fs.h>
#include <linux/syscalls.h>
#include <linux/dirent.h>
#include <linux/file.h>

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
#include <linux/moduleparam.h>
#endif

#include "fs_test.h"

//extern unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base);
/*----------------------------------------------*/
static char *path = "/";

/*********************************************************************************************/


/*********************************************************************************************/
static int ls_filldir(void * __buf, const char * name, int namlen, loff_t offset,
		   ino_t ino, unsigned int d_type)
{
    FS_TEST_DATA *data = (FS_TEST_DATA*)__buf;

    printk("#%d name=%s, type=%u\n", data->count++, name, d_type);
    return 0;
}

static int fs_ls(char * dir_path)
{
    struct file * file;
	int err = 0;
    FS_TEST_DATA data = {0};

    file = filp_open((const char __user *) dir_path, O_RDONLY|O_DIRECTORY, 0);
	if (IS_ERR(file)){
		err = -EBADF;
        printk("Error: unable to get file struct for path %s \n", dir_path);
        goto no_file;
	}

	err = vfs_readdir(file, ls_filldir, &data);
    if (err < 0){
        printk("Error: vfs_readdir() return %d.\n", err);
		goto ls_out;
    }

    err = 0;
ls_out:
    filp_close(file, 0);
no_file:
    return err;
}

static int __init	mod_init(void)
{
    int ret = 0;

    ret = fs_ls(path);

    // not insert mode
	return -1;
}

static void mod_exit(void)
{
}

module_init(mod_init);
module_exit(mod_exit);

module_param(path, charp, 0);

MODULE_LICENSE(MY_MODULE_LICENSE);
MODULE_AUTHOR(MY_MODULE_AUTHOR);
MODULE_DESCRIPTION(MY_MODULE_DESCRIPTION);





<think>我们正在讨论Linux系统中列出内核模块的命令。根据引用[1],使用`lsmod`命令可以列出内核模块。引用[1]明确指出:“在Linux系统中使用Lsmod命令列出内核模块(Kernel Modules)”。因此,正确答案是`lsmod`。 同时,引用[2]和引用[3]提到了其他模块管理命令(如`insmod`和`modprobe`),但它们用于加载模块,而不是列出模块。 因此,用户询问的“列出已有内核模块的命令”就是`lsmod`。 注意:在终端中直接输入`lsmod`即可,它会显示当前加载的内核模块列表,包括模块名称、大小、被使用的次数以及依赖该模块的其他模块。 另外,`lsmod`命令实际上是读取并解析`/proc/modules`文件,而该文件的信息来自内核。因此,也可以直接查看`/proc/modules`文件,但`lsmod`的输出格式更友好。 回答如下:</think>### 列出已有内核模块的命令 在 Linux 系统中,**`lsmod`** 命令用于列出当前已加载的内核模块[^1]。执行方法如下: ```bash lsmod ``` #### 输出示例: ``` Module Size Used by nvidia 35323904 203 i915 2457600 5 snd_hda_codec_hdmi 65536 1 ... ``` - **第一列**:模块名称 - **第二列**:模块大小(字节) - **第三列**:被依赖的次数 #### 补充说明: 1. **实现原理** `lsmod` 实际上读取 `/proc/modules` 文件的内容并格式化输出[^1]。也可直接查看该文件: ```bash cat /proc/modules ``` 2. **相关命令对比** | 命令 | 用途 | 特点 | |------------|-------------------------------|----------------------------------| | `lsmod` | 列出已加载模块 | 只读操作,无参数 | | `insmod` | 手动加载模块 | 需指定模块路径(如 `/path/to/module.ko`)[^2] | | `modprobe` | 自动加载模块(含依赖) | 从标准目录搜索模块(如 `/lib/modules/`)[^3] | 3. **模块存储位置** 已安装的内核模块通常位于: ```bash /lib/modules/$(uname -r)/kernel/ ``` #### 使用场景示例: - 检查驱动是否加载: ```bash lsmod | grep nvidia # 检查NVIDIA显卡驱动 ``` - 查看模块依赖关系: ```bash modinfo <模块名> | grep depends # 查看模块依赖项 ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值