LINUX模块

时间:2019-04-29 03:17:37  来源:igfitidea点击:

使用Linux管理硬件和内核模块

硬件和模块

管理任何Linux服务器的重要部分是要知道如何显示当前加载到内核中的当前硬件和模块。

列出硬件信息

列出有关您的硬件信息的常用命令是lsdev命令。lsdev命令用于显示系统中断地址和I / O端口。还显示有关IRQ和DMA通道信息的信息。

lsdev

要显示有关已安装硬件的信息,只需执行lsdev命令。没有参数或参数可以传递给此命令。以下示例已在openSUSE系统上运行:

linux-pd5y:~ # lsdev
Device            DMA   IRQ  I/O Ports
------------------------------------------------
0000:00:01.1                 0170-0177 01f0-01f7 0376-0376 03f6-03f6 d000-d00f
0000:00:03.0                 d010-d017
0000:00:04.0                 d020-d03f
0000:00:05.0                 d100-d1ff d200-d23f
0000:00:0d.0                 d240-d247 d250-d257 d260-d26f
acpi                      9 
ACPI                         4000-4003 4004-4005 4008-400b 4020-4021
ahci                      5  d240-d247 d250-d257 d260-d26f
ata_piix              14 15  0170-0177 01f0-01f7 0376-0376 03f6-03f6 d000-d00f
cascade             4     2 
dma                          0080-008f
dma1                         0000-001f
dma2                         00c0-00df

lspci

lspci命令用于显示有关系统中的PCI总线以及与其连接的设备的信息。以下是一些可以传递给lspci命令的选项:

Basic display modes

       -m     Dump PCI device data in a backward-compatible machine readable form.  See below for details.

       -mm    Dump PCI device data in a machine readable form for easy parsing by scripts.  See below for details.

       -t     Show a tree-like diagram containing all buses, bridges, devices and connections between them.


Display options

       -v     Be verbose and display detailed information about all devices.

       -vv    Be very verbose and display more details. This level includes everything deemed useful.

       -vvv   Be  even more verbose and display everything we are able to parse, even if it doesnt look interesting at 
              all (e.g., undefined memory regions).

       -k     Show kernel drivers handling each device and also kernel modules capable of handling it.  Turned on  
              by  default  when  -v  is given in the normal mode of output.  (Currently works only on Linux with 
              kernel 2.6 or newer.)

       -x     Show hexadecimal dump of the standard part of the configuration space (the first 64 bytes or 128 bytes 
              for CardBus bridges).

       -xxx   Show hexadecimal dump of the whole PCI configuration space. It is available only to root as several 
              PCI devices crash when you try to read some parts of the config space (this behaviour probably doesnt 
              violate the PCI standard, but its  at  least  very stupid). However, such devices are rare, so 
              you neednt worry much.

       -xxxx  Show hexadecimal dump of the extended (4096-byte) PCI configuration space available on PCI-X 2.0 and 
              PCI Express buses.

       -b     Bus-centric view. Show all IRQ numbers and addresses as seen by the cards on the PCI bus instead of 
              as seen by the kernel.

       -D     Always show PCI domain numbers. By default, lspci suppresses them on machines which have only domain 0.

lspci命令的示例

本身不带参数的执行lspci将以类似于以下示例的格式显示输出:

linux-pd5y:~ # lspci
00:00.0 Host bridge: Intel Corporation 440FX - 82441FX PMC [Natoma] (rev 02)
00:01.0 ISA bridge: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II]
00:01.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)
00:02.0 VGA compatible controller: InnoTek Systemberatung GmbH VirtualBox Graphics Adapter
00:03.0 Ethernet controller: Intel Corporation 82540EM Gigabit Ethernet Controller (rev 02)
00:04.0 System peripheral: InnoTek Systemberatung GmbH VirtualBox Guest Service
00:05.0 Multimedia audio controller: Intel Corporation 82801AA AC97 Audio Controller (rev 01)
00:06.0 USB Controller: Apple Computer Inc. KeyLargo/Intrepid USB
00:07.0 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)
00:0d.0 SATA controller: Intel Corporation 82801HBM/HEM (ICH8M/ICH8M-E) SATA AHCI Controller (rev 02)

lspci命令中添加-k选项将显示处理每个设备的内核驱动程序:

linux-pd5y:~ # lspci -k
00:00.0 Host bridge: Intel Corporation 440FX - 82441FX PMC [Natoma] (rev 02)
00:01.0 ISA bridge: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II]
00:01.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)
        Kernel driver in use: ata_piix
00:02.0 VGA compatible controller: InnoTek Systemberatung GmbH VirtualBox Graphics Adapter
00:03.0 Ethernet controller: Intel Corporation 82540EM Gigabit Ethernet Controller (rev 02)
        Subsystem: Intel Corporation PRO/1000 MT Desktop Adapter
        Kernel driver in use: e1000
00:04.0 System peripheral: InnoTek Systemberatung GmbH VirtualBox Guest Service
00:05.0 Multimedia audio controller: Intel Corporation 82801AA AC97 Audio Controller (rev 01)
        Subsystem: Intel Corporation Device 0000
        Kernel driver in use: snd_intel8x0
00:06.0 USB Controller: Apple Computer Inc. KeyLargo/Intrepid USB
        Kernel driver in use: ohci_hcd
00:07.0 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)
00:0d.0 SATA controller: Intel Corporation 82801HBM/HEM (ICH8M/ICH8M-E) SATA AHCI Controller (rev 02)
        Kernel driver in use: ahci

lsusb

lsusb命令用于显示系统上的USB总线和与其连接的设备。下面是与-t选项一起使用的lsusb命令的示例。-t选项使输出以树状结构显示。

john@john-desktop:~$ lsusb -t
/:  Bus 05.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
    |__ Port 2: Dev 2, If 0, Class=stor., Driver=usb-storage, 12M
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
    |__ Port 1: Dev 2, If 0, Class=HID, Driver=usbhid, 1.5M
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci_hcd/8p, 480M

lsusb选项

       -v, --verbose
              Tells lsusb to be verbose and display detailed information about
              the  devices shown.  This includes configuration descriptors for
              the devices current speed.  Class descriptors  will  be  shown,
              when  available,  for  USB  device classes including hub, audio,
              HID, communications, and chipcard.

       -s [[bus]:][devnum]
              Show only devices in specified bus and/or devnum.  Both IDs are
              given in decimal and may be omitted.

       -d [vendor]:[product]
              Show  only  devices  with  the  specified vendor and product ID.
              Both IDs are given in hexadecimal.

       -D device
              Do not scan the /dev/bus/usb  directory,  instead  display  only
              information  about  the  device whose device file is given.  The
              device file should be something like /dev/bus/usb/001/001.  This
              option displays detailed information like the v option; you must
              be root to do this.

可以在/usr/share/hwdata/usb.ids中找到所有已知供应商,产品,类,子类和协议的列表。使用dmesg命令检查内核是否已识别您的USB总是有用的设备。这可以通过执行dmesg | grep -i usb命令来实现。以下是dmesg命令通过USB连接连接移动设备后不久运行的示例。

[ 8093.796041] usb 1-4: new high-speed USB device number 4 using ehci_hcd
[ 8093.942460] scsi5 : usb-storage 1-4:1.0
[ 8094.943212] scsi 5:0:0:0: Direct-Access     HTC      Android Phone    0100 PQ: 0 ANSI: 2
[ 8094.948449] sd 5:0:0:0: Attached scsi generic sg6 type 0
[ 8094.953351] sd 5:0:0:0: [sdf] Attached SCSI removable disk
[ 8128.490339] sd 5:0:0:0: [sdf] 3911680 512-byte logical blocks: (2.00 GB/1.86 GiB)

显示有关当前加载的模块的信息

用于显示有关Linux内核中已加载模块状态的信息的命令是lsmod命令。

lsmod

基本上,lsmod是一个命令,用于显示/ proc / modules文件的输出。以下是实际使用的lsmod命令的示例:

linux-pd5y:~ # lsmod
Module                  Size  Used by
ip6t_LOG               12847  5 
xt_tcpudp              12788  2 
xt_pkttype             12456  3 
ipt_LOG                12880  5 
xt_limit               12541  10 

lsmod命令的输出格式分为三列:

Module:模块名称
Size:模块大小
Used:使用它的从属模块。

要显示有关特定模块的信息,我们可以将lsmod命令的输出通过管道传递到grep

linux-pd5y:~ # lsmod | grep usbcore
usbcore               190567  4 usbhid,ohci_hcd,ehci_hcd

使用模块

模块是简单的代码,可以加载和卸载到内核中。模块允许扩展内核功能,而无需重新引导系统。模块的一个很好的例子是用于设备驱动程序的模块。设备驱动程序允许内核访问连接到系统的硬件。通常,在使用模块时,将使用诸如lsmodmodinfomodprobeinsmodrmmod之类的命令。这些命令通常用于显示,加载和卸载模块。

insmod

insmod用于将模块插入内核。将模块插入内核的首选方法是使用命令modprobe,因为它可以处理依赖项。

modinfo

Modinfo用于显示有关Linux内核模块的信息。modinfo从命令行上给出的Linux内核模块中提取信息。如果模块名称不是文件名,则搜索/ lib / modules / version目录。

linux-pd5y:~ # modinfo usbcore
filename:       /lib/modules/3.1.10-1.19-default/kernel/drivers/usb/core/usbcore.ko
license:        GPL
srcversion:     9023860545ED738FD260BB4
alias:          usb:v*p*d*dc*dsc*dp*ic09isc*ip*
alias:          usb:v*p*d*dc09dsc*dp*ic*isc*ip*
depends:        
vermagic:       3.1.10-1.19-default SMP mod_unload modversions 586TSC 
parm:           usbfs_snoop:true to log all usbfs traffic (bool)
parm:           authorized_default:Default USB device authorization: 0 is not authorized, 1 is authorized, -1 is authorized except for wireless USB (default, old behaviour (int)
parm:           blinkenlights:true to cycle leds on hubs (bool)
parm:           initial_descriptor_timeout:initial 64-byte descriptor request timeout in milliseconds (default 5000 - 5.0 seconds) (int)
parm:           old_scheme_first:start with the old device initialization scheme (bool)
parm:           use_both_schemes:try the other device initialization scheme if the first one fails (bool)
parm:           autosuspend:default autosuspend delay (int)
parm:           nousb:bool

Modprobe

Modprobe是用于将模块加载到Linux内核或从Linux内核中删除模块的命令。Modprobe是insmod命令的包装器。Modprobe是将模块加载到内核中的首选方法,因为它能够处理依赖关系。Modprobe还具有使用-r选项删除模块的功能。以下是modprobe删除模块并将其加载到内核中
的示例:以下示例将删除模块parport。首先,让我们使用lsmod命令检查是否有其他模块依赖于parport

linux-pd5y:~ # lsmod
Module                  Size  Used by
ppdev                  17430  0 
parport_pc             36673  0 
parport                40930  2 ppdev,parport_pc

从上面的输出中,我们可以看到模块ppdevparport_pc依赖于parport。这意味着,如果我们卸载模块parport,那么这些模块也需要卸载:

linux-pd5y:~ # modprobe -r parport_pc 
linux-pd5y:~ # modprobe -r ppdev
linux-pd5y:~ # modprobe -r parport
linux-pd5y:~ # lsmod | grep ppdev
linux-pd5y:~ # lsmod | grep parport_pc
linux-pd5y:~ # lsmod | grep parport
linux-pd5y:~ # modprobe ppdev
linux-pd5y:~ # lsmod
Module                  Size  Used by
parport_pc             36673  0 
ppdev                  17430  0 
parport                40930  2 parport_pc,ppdev

在上面,我们卸载了模块parpoprt_pc,ppdev和parport。然后使用lsmod命令来验证是否不再加载这些模块。最后,我们发布了modprobe ppdev以重新加载我们的模块和所有依赖项。检查依赖项的另一种方法是直接查看modules.dep文件。在此openSUSE系统上,我们的模块存储在以下位置:/lib/modules/3.1.10-1.19-default。依赖关系由modprobe通过查看modules.dep文件来处理:

linux-pd5y:/lib/modules/3.1.10-1.19-default # grep ppdev modules.dep
kernel/drivers/char/ppdev.ko: kernel/drivers/parport/parport.ko