对U盘、SD卡、硬盘、操作的时候,需要对设备热拔插以及设备自动挂载自动卸载进行处理。有些时候我们需要对设备名进行固定,比如:SD卡卡槽1插入的设备固定它的设备名为sd_card1。 设备插入时将sd_card1的各个分区自动挂载固定的目录上去,设备拔出的时候又可自动的卸载之前的挂载信息。这些需求在linux2.6以后的系统都可以通过udev来处理。udev的详细介绍可以上维基百科查看:
https://zh.wikipedia.org/wiki/Udev 下面介绍udev的具体使用
说明:以下内容根据我嵌入式设备实际测试得来,仅供参考。
(一)查看设备信息:
操作udev,可以使用udevadm命令,如果我们要查看/dev/sda 设备节点信息,我们可以使用下面命令:
命令:udevadm info -a --name=sda
信息结果:
~ # udevadm info -a --name=sda
Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.
looking at device '/devices/platform/ahci.0/ata1/host0/target0:0:0/0:0:0:0/block/sda':
KERNEL=="sda"
SUBSYSTEM=="block"
DRIVER==""
ATTR{ro}=="0"
ATTR{size}=="976773168"
ATTR{stat}==" 601 0 797 2680 0 0 0 0 0 560 2680"
ATTR{range}=="16"
ATTR{discard_alignment}=="0"
ATTR{events}==""
ATTR{ext_range}=="256"
ATTR{events_poll_msecs}=="-1"
ATTR{alignment_offset}=="0"
ATTR{inflight}==" 0 0"
ATTR{removable}=="0"
ATTR{capability}=="d0"
ATTR{events_async}==""
looking at parent device '/devices/platform/ahci.0/ata1/host0/target0:0:0/0:0:0:0':
KERNELS=="0:0:0:0"
SUBSYSTEMS=="scsi"
DRIVERS=="sd"
ATTRS{rev}=="MC10"
ATTRS{type}=="0"
ATTRS{scsi_level}=="6"
ATTRS{model}=="TOSHIBA MK5061GS"
ATTRS{state}=="running"
ATTRS{unload_heads}=="0"
ATTRS{queue_type}=="simple"
ATTRS{modalias}=="scsi:t-0x00"
ATTRS{iodone_cnt}=="0x281"
ATTRS{iorequest_cnt}=="0x281"
ATTRS{queue_ramp_up_period}=="120000"
ATTRS{timeout}=="30"
ATTRS{evt_media_change}=="0"
ATTRS{ioerr_cnt}=="0x0"
ATTRS{queue_depth}=="31"
ATTRS{vendor}=="ATA "
ATTRS{device_blocked}=="0"
ATTRS{iocounterbits}=="32"
looking at parent device '/devices/platform/ahci.0/ata1/host0/target0:0:0':
KERNELS=="target0:0:0"
SUBSYSTEMS=="scsi"
DRIVERS==""
looking at parent device '/devices/platform/ahci.0/ata1/host0':
KERNELS=="host0"
SUBSYSTEMS=="scsi"
DRIVERS==""
looking at parent device '/devices/platform/ahci.0/ata1':
KERNELS=="ata1"
SUBSYSTEMS==""
DRIVERS==""
looking at parent device '/devices/platform/ahci.0':
KERNELS=="ahci.0"
SUBSYSTEMS=="platform"
DRIVERS=="ahci"
ATTRS{modalias}=="platform:ahci"
looking at parent device '/devices/platform':
KERNELS=="platform"
SUBSYSTEMS==""
DRIVERS==""
~ #
这里对上面的几个常用做说明
匹配键:"==","!="
赋值键:"=","+=",":=
KERNEL - 匹配设备的内核名字
SUBSYSTEM - 匹配设备的子系统
DRIVER - 匹配设备驱动名
NAME - 应当被采用为设备节点的名字
SYMLINK - 一系列被作为设备节点替补名字的符号链接
(二)固定设备节点名字
对于可热拔插的设备,比如说U盘,SD卡,最开始插入的设备设备节点名是sda,接着是sdb,也就是说只从dev下面的设备节点名是不能区分我们插入的是那个设备,这时我们可以通过udev 将设备节点与设备绑定,方法就是通过内核名字建立连接,连接到设备节点上。
目录结构如下:
/etc/udev # pwd
/etc/udev
/etc/udev # tree
.
├── rules.d
│ └── 99-mount.rules
│ └── 01-rename.rules
└── udev.conf
udev.conf
# udev.conf
# The initial syslog(3) priority: "err", "info", "debug" or its
# state can be changed with: "udevcontrol log_priority=".
udev_root="/dev/"
udev_rules="/etc/udev/rules.d"
udev_log="err"
01-rename.rules
KERNEL=="*", OWNER="root" GROUP="root", MODE="0600"
#################################
#SD Card Device
#################################
KERNELS=="1-2.3",KERNEL=="sd*",SYMLINK+="usbsda%n",OPTIONS="ignore_remove"
KERNELS=="1-2.4",KERNEL=="sd*",SYMLINK+="usbsdb%n",OPTIONS="ignore_remove"
KERNELS=="1-2.2",KERNEL=="sd*",SYMLINK+="usbsdc%n",OPTIONS="ignore_remove"
################################
#USB Device
################################
KERNELS=="1-2.1",KERNEL=="sd*",SYMLINK+="usbmmc%n",OPTIONS="ignore_remove"
################################
#DISK Device
################################
KERNELS=="host0",KERNEL=="sd*",SYMLINK+="disksda%n",OPTIONS="ignore_remove"
KERNEL=="ttyUSB*",ATTRS{bInterfaceNumber}=="00",SYMLINK+="ttyCOM0",OPTIONS="ignore_remove"
KERNEL=="ttyUSB*",ATTRS{bInterfaceNumber}=="01",SYMLINK+="ttyCOM1",OPTIONS="ignore_remove"
KERNEL=="ttyUSB*",ATTRS{bInterfaceNumber}=="02",SYMLINK+="ttyCOM2",OPTIONS="ignore_remove"
KERNEL=="ttyUSB*",ATTRS{bInterfaceNumber}=="03",SYMLINK+="ttyCOM3",OPTIONS="ignore_remove"
KERNEL=="ttyUSB*",ATTRS{bInterfaceNumber}=="04",SYMLINK+="ttyCOM4",OPTIONS="ignore_remove"
KERNEL=="ttyUSB*",ATTRS{bInterfaceNumber}=="05",SYMLINK+="ttyCOM5",OPTIONS="ignore_remove"
KERNEL=="ttyUSB*",ATTRS{bInterfaceNumber}=="06",SYMLINK+="ttyCOM6",OPTIONS="ignore_remove"
KERNEL=="ttyUSB*",ATTRS{bInterfaceNumber}=="07",SYMLINK+="ttyCOM7",OPTIONS="ignore_remove"
插入SD卡,我们可以看到,设备节点被链接成固定的名字usbsda
查看设备节点:
brw------- 1 root root 8, 1 Jul 8 00:22 sda1
brw------- 1 root root 8, 2 Jul 8 00:22 sda2
brw------- 1 root root 8, 3 Jul 8 00:22 sda3
lrwxrwxrwx 1 root root 4 Jul 8 00:22 usbsda1 -> sda1
lrwxrwxrwx 1 root root 4 Jul 8 00:22 usbsda2 -> sda2
lrwxrwxrwx 1 root root 4 Jul 8 00:22 usbsda3 -> sda3
drwxr-xr-x 2 root root 60 Jul 8 00:22 bsg
brw------- 1 root root 8, 0 Jul 8 00:22 sda
crw------- 1 root root 21, 0 Jul 8 00:22 sg0
lrwxrwxrwx 1 root root 3 Jul 8 00:22 usbsda -> sda
(三)热拔插自动挂载,卸载
自动挂载和自动卸载在99-mount.rules脚本中设置,这里设置了2个硬盘和2个SD卡设备的自动挂载,设计每个设备最大有18个分区并且将所有的分区都挂载上去。
KERNEL=="*", OWNER="root" GROUP="root", MODE="0600"
##############################################
#Disk A device
##############################################
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_1",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_1",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda1 /opt/disk_sda_1",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_1"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_1"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_1",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_2",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_2",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda2 /opt/disk_sda_2",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_2"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_2"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_2",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_3",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_3",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda3 /opt/disk_sda_3",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_3"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_3"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_3",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_4",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_4",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda4 /opt/disk_sda_4",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_4"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_4"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_4",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_5",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_5",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda5 /opt/disk_sda_5",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_5"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_5"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_5",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_6",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_6",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda6 /opt/disk_sda_6",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_6"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_6"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_6",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_7",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_7",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda7 /opt/disk_sda_7",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_7"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_7"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_7",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_8",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_8",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda8 /opt/disk_sda_8",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_8"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_8"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_8",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_9",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_9",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda9 /opt/disk_sda_9",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_9"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_9"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_9",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_10",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_10",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda10 /opt/disk_sda_10",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_10"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_10"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_10",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_11",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_11",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda11 /opt/disk_sda_11",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_11"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_11"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_11",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_12",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_12",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda12 /opt/disk_sda_12",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_12"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_12"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_12",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_13",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_13",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda13 /opt/disk_sda_13",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_13"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_13"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_13",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_14",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_14",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda14 /opt/disk_sda_14",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_14"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_14"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_14",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_15",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_15",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda15 /opt/disk_sda_15",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_15"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_15"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_15",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_16",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_16",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda16 /opt/disk_sda_16",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_16"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_16"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_16",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_17",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_17",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda17 /opt/disk_sda_17",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_17"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_17"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_17",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_18",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mkdir /opt/disk_sda_18",
ACTION=="add",KERNELS=="1-1",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/disksda18 /opt/disk_sda_18",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/fuser -k /opt/disk_sda_18"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/umount -l /opt/disk_sda_18"
ACTION=="remove",KERNELS=="1-1",RUN+="/bin/rmdir /opt/disk_sda_18",OPTIONS="ignore_remove"
##############################################
#SD Card Device
##############################################
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_1",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_1",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda1 /opt/usb_sd1_1",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_1"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_1"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_1",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_2",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_2",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda2 /opt/usb_sd1_2",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_2"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_2"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_2",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_3",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_3",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda3 /opt/usb_sd1_3",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_3"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_3"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_3",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_4",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_4",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda4 /opt/usb_sd1_4",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_4"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_4"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_4",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_5",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_5",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda5 /opt/usb_sd1_5",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_5"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_5"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_5",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_6",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_6",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda6 /opt/usb_sd1_6",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_6"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_6"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_6",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_7",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_7",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda7 /opt/usb_sd1_7",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_7"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_7"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_7",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_8",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_8",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda8 /opt/usb_sd1_8",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_8"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_8"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_8",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_9",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_9",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda9 /opt/usb_sd1_9",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_9"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_9"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_9",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_10",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_10",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda10 /opt/usb_sd1_10",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_10"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_10"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_10",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_11",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_11",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda11 /opt/usb_sd1_11",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_11"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_11"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_11",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_12",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_12",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda12 /opt/usb_sd1_12",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_12"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_12"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_12",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_13",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_13",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda13 /opt/usb_sd1_13",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_13"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_13"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_13",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_14",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_14",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda14 /opt/usb_sd1_14",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_14"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_14"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_14",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_15",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_15",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda15 /opt/usb_sd1_15",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_15"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_15"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_15",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_16",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_16",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda16 /opt/usb_sd1_16",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_16"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_16"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_16",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_17",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_17",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda17 /opt/usb_sd1_17",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_17"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_17"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_17",OPTIONS="ignore_remove"
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_18",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mkdir /opt/usb_sd1_18",
ACTION=="add",KERNELS=="1-2.3",RUN+="/bin/mount -t vfat -o rw,utf8=true,flush /dev/usbsda18 /opt/usb_sd1_18",OPTIONS="ignore_remove"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/fuser -k /opt/usb_sd1_18"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/umount -l /opt/usb_sd1_18"
ACTION=="remove",KERNELS=="1-2.3",RUN+="/bin/rmdir /opt/usb_sd1_18",OPTIONS="ignore_remove"
插入SD卡,可以看到自动挂载的挂载的设备:
/etc/udev/rules.d # df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/root 32768 20852 11916 64% /
tmpfs 61964 4 61960 0% /dev
/dev/mtdblock3 4096 768 3328 19% /hi3520
/dev/mtdblock4 4096 800 3296 20% /mnt
/dev/mtdblock6 8192 1156 7036 14% /data
tmpfs 61964 0 61964 0% /App
tmpfs 61964 0 61964 0% /opt
tmpfs 61964 4 61960 0% /var
tmpfs 61964 60 61904 0% /etc/ppp
/dev/usbsda1 1044468 1027966 16502 98% /opt/usb_sd1_1
/dev/usbsda2 33538032 33161232 376800 99% /opt/usb_sd1_2
/dev/usbsda3 27903304 27525128 378176 99% /opt/usb_sd1_3
/etc/udev/rules.d #
2019年4月17日 更新
在海思平台,对于有些SD卡或是USB自动挂载有些时候会出现冲突的问题,问题现象就是自动挂载的时候有时候有时候U盘和SD卡自动挂载相互干扰。主要原因点是因为海思的HI35XX的很多设备不具备SDIO总线,所以如果要使用SD的设备,一般都是将SD卡通过读卡器转换为USB总线信号。对于对于这类问题,可以通过SD卡的读卡器ID来区分是USB还是SD卡设备。
在海思平台可以使用下面命令查看USB设备信息:
lsusb:
~ # lsusb
Bus 001 Device 002: ID 1c9e:9b3c
Bus 001 Device 003: ID 05e3:0610
Bus 001 Device 001: ID 1d6b:0002
Bus 002 Device 001: ID 1d6b:0001
Bus 001 Device 008: ID 0951:1642
Bus 001 Device 009: ID 05e3:0716
Bus 001 Device 006: ID 0bda:8176
~ #
cat device
~ # cat /sys/kernel/debug/usb/devices
T: Bus=02 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
B: Alloc= 0/900 us ( 0%), #Int= 0, #Iso= 0
D: Ver= 1.10 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0001 Rev= 3.10
S: Manufacturer=Linux 3.10.0 ohci_hcd
S: Product=HIUSB OHCI
S: SerialNumber=hiusb-ohci
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 2 Ivl=255ms
T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 2
B: Alloc= 0/800 us ( 0%), #Int= 5, #Iso= 0
D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0002 Rev= 3.10
S: Manufacturer=Linux 3.10.0 ehci_hcd
S: Product=HIUSB EHCI
S: SerialNumber=hiusb-ehci
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 4 Ivl=256ms
T: Bus=01 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=480 MxCh= 0
D: Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1c9e ProdID=9b3c Rev= 3.18
S: Manufacturer=LONGSUNG
S: Product=USB Modem
C:* #Ifs= 5 Cfg#= 1 Atr=80 MxPwr=500mA
I:* If#= 0 Alt= 0 #EPs= 2 Cls=ff(vend.) Sub=42 Prot=01 Driver=option
E: Ad=01(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E: Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
I:* If#= 1 Alt= 0 #EPs= 3 Cls=ff(vend.) Sub=00 Prot=00 Driver=option
E: Ad=83(I) Atr=03(Int.) MxPS= 10 Ivl=32ms
E: Ad=82(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E: Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
I:* If#= 2 Alt= 0 #EPs= 3 Cls=ff(vend.) Sub=00 Prot=00 Driver=option
E: Ad=85(I) Atr=03(Int.) MxPS= 10 Ivl=32ms
E: Ad=84(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E: Ad=03(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
I:* If#= 3 Alt= 0 #EPs= 3 Cls=ff(vend.) Sub=00 Prot=00 Driver=option
E: Ad=87(I) Atr=03(Int.) MxPS= 10 Ivl=32ms
E: Ad=86(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E: Ad=04(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
I:* If#= 4 Alt= 0 #EPs= 3 Cls=ff(vend.) Sub=ff Prot=ff Driver=GobiNet
E: Ad=89(I) Atr=03(Int.) MxPS= 8 Ivl=32ms
E: Ad=88(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E: Ad=05(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
T: Bus=01 Lev=01 Prnt=01 Port=01 Cnt=02 Dev#= 3 Spd=480 MxCh= 4
D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=02 MxPS=64 #Cfgs= 1
P: Vendor=05e3 ProdID=0610 Rev=32.98
S: Product=USB2.0 Hub
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr=100mA
I: If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=01 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 1 Ivl=256ms
I:* If#= 0 Alt= 1 #EPs= 1 Cls=09(hub ) Sub=00 Prot=02 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 1 Ivl=256ms
T: Bus=01 Lev=02 Prnt=03 Port=00 Cnt=01 Dev#= 8 Spd=480 MxCh= 0
D: Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=0951 ProdID=1642 Rev= 1.00
S: Manufacturer=Kingston
S: Product=DT 101 G2
S: SerialNumber=001CC0EC32F7BB40F71300BF
C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=200mA
I:* If#= 0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage
E: Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E: Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
T: Bus=01 Lev=02 Prnt=03 Port=02 Cnt=02 Dev#= 9 Spd=480 MxCh= 0
D: Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=05e3 ProdID=0716 Rev=97.27
S: Manufacturer=Genesys
S: Product=USB Reader
S: SerialNumber=000000000013
C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=500mA
I:* If#= 0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage
E: Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E: Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
T: Bus=01 Lev=02 Prnt=03 Port=03 Cnt=03 Dev#= 6 Spd=480 MxCh= 0
D: Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=0bda ProdID=8176 Rev= 2.00
S: Manufacturer=Realtek
S: Product=802.11n WLAN Adapter
S: SerialNumber=00e04c000001
C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=500mA
I:* If#= 0 Alt= 0 #EPs= 4 Cls=ff(vend.) Sub=ff Prot=ff Driver=rtl8192cu
E: Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E: Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E: Ad=03(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E: Ad=84(I) Atr=03(Int.) MxPS= 64 Ivl=125us
~ #
查看上面信息可以知道USB相关的设备有:
Product=HIUSB EHCI #海思USB总线
Product=USB Modem #上网模块
Product=USB2.0 Hub #USB HUB
Product=DT 101 G2 # U盘
Product=USB Reader # usb 读卡器
Product=802.11n WLAN Adapter #USB网卡