linux lsmod(查看驱动模块)和 ls /dev(驱动设备)

一、lsmod

lsmod 命令,用于列出当前 linux 系统中加载的模块。当驱动开发人员编写好驱动代码,并生成驱动代码对应的驱动模块后,可以通过 insmod xxx.ko 将驱动模块(.ko)加载到 linux 操作系统中。最后,通过 lsmod 命令就可以看到 xxx.ko 已经加载到 linux 系统当中了。

1.1

驱动代码:

back@ubuntu2205:~$ cat driver.c
#include 
#include 
#include 


int hello_probe(struct platform_device *pdev)
{
        printk("[%s] match ok\n", __FILE__);
        return 0;
}
int hello_remove(struct platform_device *pdev)
{
        printk("[%s] hello_remove\n", __FILE__);
        return 0 ;
}

struct platform_driver hello_driver = {
        .probe = hello_probe,
        .remove = hello_remove,
        .driver.name = "yikoulinux",
};

static int hello_init(void)
{
        printk("[%s] hello_init\n", __FILE__);
        return platform_driver_register(&hello_driver);
}

static void hello_exit(void)
{
        printk("[%s] hello_exit\n", __FILE__);
        platform_driver_unregister(&hello_driver);
        return;
}
MODULE_LICENSE("GPL");
module_init(hello_init);
module_exit(hello_exit);
back@ubuntu2205:~$ cat device.c
#include 
#include 
#include 


void    hello_release(struct device *dev)
{
        printk("[%s] hello_release\n",__FILE__);
        return;
}

struct platform_device hello_device ={
        .name = "yikoulinux",
        .id = -1,
        .dev.release = hello_release,
        //hardware TBD
};

static int hello_init(void)
{
        printk("[%s] hello_init\n", __FILE__);
        return platform_device_register(&hello_device);
}

static void hello_exit(void)
{
        printk("[%s] hello_exit\n", __FILE__);
        platform_device_unregister(&hello_device);
        return;
}
MODULE_LICENSE("GPL");
module_init(hello_init);
module_exit(hello_exit);
back@ubuntu2205:~$ ls -lh
total 1.1M
-rw-r--r--  1 back back  709 83 12:13 device.c
-rw-rw-r--  1 back back 5.6K 83 12:18 device.ko
-rw-r--r--  1 back back  829 83 12:13 driver.c
-rw-rw-r--  1 back back 5.3K 83 12:18 driver.ko
back@ubuntu2205:~$ 

1.2

linux lsmod(查看驱动模块)和 ls /dev(驱动设备)_第1张图片
可以看到,我们通过 insmod 命令将 1.1 中的 device.ko 和 driver.ko加载到系统后,lsmod 就会发现我们 insmod 加载的 .ko 模块。


二、其他常见信息的查看

查看CPU信息: cat /proc/cpuinfo
查看板卡信息:cat /proc/pci
查看PCI信息: lspci
例子: lspci |grep Ethernet 查看网卡型号
查看内存信息:cat /proc/meminfo
查看USB设备: cat /proc/bus/usb/devices
查看键盘和鼠标:cat /proc/bus/input/devices
查看系统硬盘信息和使用情况:fdisk & disk - l & df
查看各设备的中断请求(IRQ): cat /proc/interrupts
查看系统体系结构:uname -a

dmidecode查看硬件信息,包括bios、cpu、内存等信息
dmesg | more 查看硬件信息


三、ls /dev

ls /dev 命令用于查看系统中的驱动设备,包括字符设备、块设备。一个驱动模块可以注册多个设备文件。

如字符设备 i2c-0 ~ i2c-4,这5个都是字符设备,它们的主设备号都是 89,此设备号为 0 ~ 4。
同理,块设备 loop0 ~ loop7,它们的主设备号都是 7,此设备号为 0 ~ 7。

linux lsmod(查看驱动模块)和 ls /dev(驱动设备)_第2张图片


你可能感兴趣的:(linux,内核驱动,linux,运维,服务器)