Linux|centos操作系统|USB设备的驱动如何寻找和USB设置的自动挂载,卸载(备忘)

仅做一个记录,Linux命令实在太多了,有些比较有趣的命令还是需要记录一下哈

1,

usb-devices命令

该命令会打印输出现有机器上所有的USB设备,此命令等同于cat /sys/kernel/debug/usb/devices

其中部分输出如下:

T:  Bus=02 Lev=02 Prnt=02 Port=00 Cnt=01 Dev#=  3 Spd=12  MxCh= 0
D:  Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs=  1
P:  Vendor=2717 ProdID=503a Rev=02.05
S:  Manufacturer=Telink
S:  Product=Xiaomi Wireless Mouse Lite 2
C:  #Ifs= 1 Cfg#= 1 Atr=a0 MxPwr=50mA
I:  If#= 0 Alt= 0 #EPs= 1 Cls=03(HID  ) Sub=01 Prot=02 Driver=usbhid




T:  Bus=01 Lev=02 Prnt=02 Port=00 Cnt=01 Dev#=  3 Spd=480 MxCh= 0
D:  Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs=  1
P:  Vendor=0bda ProdID=b711 Rev=02.00
S:  Manufacturer=Realtek
S:  Product=802.11n WLAN Adapter
S:  SerialNumber=00E04CB82101
C:  #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=500mA
I:  If#= 0 Alt= 0 #EPs= 6 Cls=ff(vend.) Sub=ff Prot=ff Driver=rtl8710bu

关键信息解读:

Product=Xiaomi Wireless Mouse Lite 2  小米无线鼠标

Vendor=2717 ProdID=503a Rev=02.05  2717 设备编号,503a 生产编号

下面的就不用说了,802.11n WLAN Adapter  表示此USB是一个网卡,当然了,此网卡驱动是已经安装好的才会这样显示

2,

lsusb 命令

该命令是软件包usbutils内的一个命令,主要是打印输出机器目前在用的USB设备, 参数tv 表示详细层级输出

例如,只简单显示在用的USB设备:

串行总线 只有001 002  003 ,表示只有三个USB设备

[root@control-plane ~]# lsusb
Bus 003 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 002 Device 003: ID 2717:503a  
Bus 002 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0bda:b711 Realtek Semiconductor Corp. 
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

详细层级输出在用的USB设备:

和上面的输出互相印证了,确实是三个USB设备,并且最后一个网卡已经被Linux正确识别了,如果没识别,driver将是usb-storage

[root@control-plane ~]# lsusb -tv
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 5000M
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/1p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 1: Dev 3, If 0, Class=Human Interface Device, Driver=usbhid, 12M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/6p, 480M
        |__ Port 1: Dev 3, If 0, Class=Vendor Specific Class, Driver=rtl8710bu, 480M

3,

udevadm

usb设备监控程序,检测USB设备是否插入或者拔出

常用形式是udevadm monitor --property ,此时USB设备的插拔将会实时显示,并且给出详细的信息,上面两个命令的所有信息一并给出

例如,插入一个无线USB鼠标接受端,可以看到内核的挂载动作,比如挂载到目录/dev/bus/usb/002/003, 生产序号 PRODUCT=3/2717/503a/111

[root@control-plane ~]# udevadm monitor --property
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent

KERNEL[215.228496] add      /devices/pci0000:00/0000:00:1c.3/0000:06:00.0/usb2/2-1/2-1.1 (usb)
ACTION=add
BUSNUM=002
DEVNAME=/dev/bus/usb/002/003
DEVNUM=003
DEVPATH=/devices/pci0000:00/0000:00:1c.3/0000:06:00.0/usb2/2-1/2-1.1
DEVTYPE=usb_device
MAJOR=189
MINOR=130
PRODUCT=2717/503a/205
SEQNUM=3730
SUBSYSTEM=usb
TYPE=0/0/0

KERNEL[215.230646] add      /devices/pci0000:00/0000:00:1c.3/0000:06:00.0/usb2/2-1/2-1.1/2-1.1:1.0 (usb)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1c.3/0000:06:00.0/usb2/2-1/2-1.1/2-1.1:1.0
DEVTYPE=usb_interface
INTERFACE=3/1/2
MODALIAS=usb:v2717p503Ad0205dc00dsc00dp00ic03isc01ip02in00
PRODUCT=2717/503a/205
SEQNUM=3731
SUBSYSTEM=usb
TYPE=0/0/0

KERNEL[215.237769] add      /devices/pci0000:00/0000:00:1c.3/0000:06:00.0/usb2/2-1/2-1.1/2-1.1:1.0/0003:2717:503A.0001 (hid)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:1c.3/0000:06:00.0/usb2/2-1/2-1.1/2-1.1:1.0/0003:2717:503A.0001
HID_ID=0003:00002717:0000503A
HID_NAME=Telink Xiaomi Wireless Mouse Lite 2
HID_PHYS=usb-0000:06:00.0-1.1/input0
MODALIAS=hid:b0003g0001v00002717p0000503A
SEQNUM=3732
SUBSYSTEM=hid

此命令在USB设备驱动没有安装的时候,可以配合lsusb命令来查询该usb设备的具体型号,从而得到正确的usb驱动

4,

查询设备挂载详情

例如LVM的其中一个分区详情,其它种类查询没有太多意义,就不举例了,比如

  1. udevadm info --query=path --name=sda 查看sda的path

  2. udevadm info --attribute-walk --name=/dev/nvme0n1  查看nvme0n1的所有父设备一直到sysfs的根节点

[root@control-plane ~]# udevadm info --query=all --name=dm-2
P: /devices/virtual/block/dm-2
N: dm-2
S: centos/home
S: disk/by-id/dm-name-centos-home
S: disk/by-id/dm-uuid-LVM-PyGUtcJjQsvFN7OXScUCZk0CoWW1k02S96xuWxV0OJE6rbqgWHaBMLGZAlh0rifi
S: disk/by-uuid/111780e4-2780-4497-9214-25d845cefb7c
S: mapper/centos-home
E: DEVLINKS=/dev/centos/home /dev/disk/by-id/dm-name-centos-home /dev/disk/by-id/dm-uuid-LVM-PyGUtcJjQsvFN7OXScUCZk0CoWW1k02S96xuWxV0OJE6rbqgWHaBMLGZAlh0rifi /dev/disk/by-uuid/111780e4-2780-4497-9214-25d845cefb7c /dev/mapper/centos-home
E: DEVNAME=/dev/dm-2
E: DEVPATH=/devices/virtual/block/dm-2
E: DEVTYPE=disk
E: DM_ACTIVATION=1
E: DM_LV_NAME=home
E: DM_NAME=centos-home
E: DM_SUSPENDED=0
E: DM_UDEV_DISABLE_LIBRARY_FALLBACK_FLAG=1
E: DM_UDEV_PRIMARY_SOURCE_FLAG=1
E: DM_UDEV_RULES_VSN=2
E: DM_UUID=LVM-PyGUtcJjQsvFN7OXScUCZk0CoWW1k02S96xuWxV0OJE6rbqgWHaBMLGZAlh0rifi
E: DM_VG_NAME=centos
E: ID_FS_TYPE=xfs
E: ID_FS_USAGE=filesystem
E: ID_FS_UUID=111780e4-2780-4497-9214-25d845cefb7c
E: ID_FS_UUID_ENC=111780e4-2780-4497-9214-25d845cefb7c
E: MAJOR=253
E: MINOR=2
E: MPATH_SBIN_PATH=/sbin
E: SUBSYSTEM=block
E: TAGS=:systemd:
E: USEC_INITIALIZED=96943

查看小米的无线USB鼠标 

[root@control-plane ~]# ls /dev/h
hidraw0    hpet       hugepages/ hwrng      
[root@control-plane ~]# ls /dev/hidraw0 
/dev/hidraw0

[root@control-plane ~]# udevadm info --query=all --name=hidraw0
P: /devices/pci0000:00/0000:00:1c.3/0000:06:00.0/usb2/2-1/2-1.1/2-1.1:1.0/0003:2717:503A.0001/hidraw/hidraw0
N: hidraw0
E: DEVNAME=/dev/hidraw0
E: DEVPATH=/devices/pci0000:00/0000:00:1c.3/0000:06:00.0/usb2/2-1/2-1.1/2-1.1:1.0/0003:2717:503A.0001/hidraw/hidraw0
E: ID_BUS=usb
E: ID_MODEL=Xiaomi_Wireless_Mouse_Lite_2
E: ID_MODEL_ENC=Xiaomix20Wirelessx20Mousex20Litex202
E: ID_MODEL_ID=503a
E: ID_REVISION=0205
E: ID_SERIAL=Telink_Xiaomi_Wireless_Mouse_Lite_2
E: ID_TYPE=hid
E: ID_USB_DRIVER=usbhid
E: ID_USB_INTERFACES=:030102:
E: ID_USB_INTERFACE_NUM=00
E: ID_VENDOR=Telink
E: ID_VENDOR_ENC=Telink
E: ID_VENDOR_ID=2717
E: MAJOR=245
E: MINOR=0
E: SUBSYSTEM=hidraw
E: USEC_INITIALIZED=215290694

5,

USB设备的自动卸载和挂载

例如,网卡的自动挂载:

[root@control-plane ~]# cat /etc/udev/rules.d/70-persistent-ipoib.rules 
# This is a sample udev rules file that demonstrates how to get udev to
# set the name of IPoIB interfaces to whatever you wish.  There is a
# 16 character limit on network device names though, so don't go too nuts
#
# Important items to note: ATTR{type}=="32" is IPoIB interfaces, and the
# ATTR{address} match must start with ?* and only reference the last 8
# bytes of the address or else the address might not match on any given
# start of the IPoIB stack
#
# Note: as of rhel7, udev is case sensitive on the address field match
# and all addresses need to be in lower case.
#
# ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*", ATTR{type}=="32", ATTR{address}=="?*00:02:c9:03:00:31:78:f2", NAME="mlx4_ib3"

ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="1a2b", RUN+="/usr/sbin/usb_modeswitch -K -v 0bda -p 1a2b"
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="1a2b", RUN+="/bin/bash -c 'modprobe 8188gu && echo 0bda 1a2b > /sys/bus/usb-serial/drivers/option1/new_id'"
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="1a2b", RUN+="/bin/bash -c 'modprobe 8188gu && echo 0bda 1a2b > /sys/bus/usb-serial/drivers/option1/new_id'"

那么,卸载如何写呢?

ACTION=="remove",KERNELS=="*:0:0:1",ATTRS{idVendor}=="05e3",ATTRS{idProduct}=="0716",RUN+="/bin/umount -l /opt/usb_sd1_1"

我想这个应该是可以算作禁用特定USB设备吧,当然了,先自动挂载,指定挂载点,然后在卸载就等于禁用了嘛

拔出的时候有个PRODUCT ,它是 idVendor 和 idProduct 值的一个组合,可以使用它来区分不同的设备。最终就可以正常卸载设备了