我遇到了一个 udev 规则问题,每次我连接设备时,该规则总是匹配两次。如果我不确定我制定的规则是否绝对正确,我不会在这里询问。我认为问题不在于规则本身,但无论如何,这是我的规则:
ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4e21", ENV{ID_FS_TYPE}=="vfat", OWNER="...me...", SYMLINK+=".../nexus", RUN+="/home/.../myscript"
这是谷歌的 nexus s,插入后会通过 rsync 进行备份。操作如下:手机连接后询问是否应转为存储模式,我同意。现在第一个问题是,我无法使用 ACTION="add" 之类的操作,因为设备是由 udev 添加的,但尚未提供文件系统。我必须将其转为存储模式,udev 会触发新事件,识别有关文件系统的更多信息(例如 vfat)。因此,为了不过早匹配我的规则,我使用了 fs-type 环境变量。手机文件系统的最终设备节点(在本例中)是“/dev/sdg”(没有数字!)
现在该规则总是匹配两次,当我监视 udev 活动时,它显示:
KERNEL[33252.434094] add /devices/pci0000:00/0000:00:16.2/usb3/3-2 (usb)
KERNEL[33252.434722] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0 (usb)
KERNEL[33252.435005] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18 (scsi)
KERNEL[33252.435139] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/scsi_host/host18 (scsi_host)
UDEV [33252.448355] add /devices/pci0000:00/0000:00:16.2/usb3/3-2 (usb)
UDEV [33252.450621] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0 (usb)
UDEV [33252.452331] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18 (scsi)
UDEV [33252.453618] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/scsi_host/host18 (scsi_host)
KERNEL[33253.434071] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0 (scsi)
KERNEL[33253.434133] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0 (scsi)
KERNEL[33253.434289] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/scsi_disk/18:0:0:0 (scsi_disk)
KERNEL[33253.434353] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/scsi_device/18:0:0:0 (scsi_device)
KERNEL[33253.434396] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/scsi_generic/sg7 (scsi_generic)
KERNEL[33253.434528] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/bsg/18:0:0:0 (bsg)
UDEV [33253.435261] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0 (scsi)
KERNEL[33253.436359] add /devices/virtual/bdi/8:96 (bdi)
UDEV [33253.436418] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0 (scsi)
KERNEL[33253.437187] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/block/sdg (block)
UDEV [33253.437724] add /devices/virtual/bdi/8:96 (bdi)
UDEV [33253.438335] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/scsi_disk/18:0:0:0 (scsi_disk)
KERNEL[33253.438867] change /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/block/sdg (block)
UDEV [33253.439476] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/scsi_device/18:0:0:0 (scsi_device)
UDEV [33253.440782] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/scsi_generic/sg7 (scsi_generic)
UDEV [33253.441059] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/bsg/18:0:0:0 (bsg)
KERNEL[33253.446210] change /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/block/sdg (block)
UDEV [33253.465723] add /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/block/sdg (block)
UDEV [33253.489562] change /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/block/sdg (block)
UDEV [33253.499913] change /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/block/sdg (block)
激活存储模式后:
KERNEL[33311.201180] change /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/block/sdg (block)
KERNEL[33311.231579] change /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/block/sdg (block)
UDEV [33315.817190] change /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/block/sdg (block)
UDEV [33315.885089] change /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/block/sdg (block)
在不知道最后两行到底发生了什么的情况下,它们的相似性使我认为 udev 重复某些事件可能存在问题......这是 udevadm info -q all -n /dev/... 的输出:
P: /devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/block/sdg
N: sdg
S: christian/nexus
S: disk/by-id/usb-Google_File-CD_Gadget_30309C8C04B300EC-0:0
S: disk/by-label/NEXUS_S
S: disk/by-path/pci-0000:00:16.2-usb-0:2:1.0-scsi-0:0:0:0
S: disk/by-uuid/829D-18DB
E: DEVLINKS=/dev/.../nexus /dev/disk/by-id/usb-Google_File-CD_Gadget_30309C8C04B300EC-0:0 /dev/disk/by-label/NEXUS_S /dev/disk/by-path/pci-0000:00:16.2-usb-0:2:1.0-scsi-0:0:0:0 /dev/disk/by-uuid/829D-18DB
E: DEVNAME=/dev/sdg
E: DEVPATH=/devices/pci0000:00/0000:00:16.2/usb3/3-2/3-2:1.0/host18/target18:0:0/18:0:0:0/block/sdg
E: DEVTYPE=disk
E: ID_BUS=usb
E: ID_FS_LABEL=NEXUS_S
E: ID_FS_LABEL_ENC=NEXUS_S
E: ID_FS_TYPE=vfat
E: ID_FS_USAGE=filesystem
E: ID_FS_UUID=829D-18DB
E: ID_FS_UUID_ENC=829D-18DB
E: ID_FS_VERSION=FAT32
E: ID_INSTANCE=0:0
E: ID_MEDIA_PLAYER=google_nexus-s
E: ID_MODEL=File-CD_Gadget
E: ID_MODEL_ENC=File-CD\x20Gadget\x20\x20
E: ID_MODEL_ID=4e21
E: ID_PATH=pci-0000:00:16.2-usb-0:2:1.0-scsi-0:0:0:0
E: ID_PATH_TAG=pci-0000_00_16_2-usb-0_2_1_0-scsi-0_0_0_0
E: ID_REVISION=0000
E: ID_SERIAL=Google_File-CD_Gadget_30309C8C04B300EC-0:0
E: ID_SERIAL_SHORT=30309C8C04B300EC
E: ID_TYPE=disk
E: ID_USB_DRIVER=usb-storage
E: ID_USB_INTERFACES=:080650:
E: ID_USB_INTERFACE_NUM=00
E: ID_VENDOR=Google
E: ID_VENDOR_ENC=Google\x20\x20
E: ID_VENDOR_ID=18d1
E: MAJOR=8
E: MINOR=96
E: SUBSYSTEM=block
E: UDEV_LOG=3
E: UDISKS_PRESENTATION_ICON_NAME=multimedia-player
E: UDISKS_PRESENTATION_NOPOLICY=0
E: USEC_INITIALIZED=33253437171
我尝试了很多其他组合(例如 KERNEL=..)。现在我需要帮助。每一条提示都非常感谢!如果您需要更多输出(如 syslog),我很乐意提供...
#后记:
我认为问题可能是文件系统获取了设备节点,例如“sdg”,而不是“sdg1”。其他 Android 智能手机似乎没有出现此问题,例如 Galaxy Ace,一切都很好……
fdisk -l /dev/sdg 的输出(带有 nexus s):
Disk /dev/sdg: 14.3 GB, 14314094080 bytes
64 Köpfe, 32 Sektoren/Spur, 13650 Zylinder, zusammen 27957215 Sektoren
Einheiten = Sektoren von 1 × 512 = 512 Bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Festplattenidentifikation: 0x00000000
Gerät boot. Anfang Ende Blöcke Id System
然后就一片虚无。
无论如何,我通过创建一个 pidfile 并让脚本检查它来解决这个问题......
答案1
sdg
我认为你关于通过 first和 later触发两个事件的假设是正确的sdg1
。我在使用 USB 记忆棒时也遇到了同样的问题。
只需添加到您的规则
KERNEL=="sd?[0-9]"
这只会在第二个事件时触发。对我来说效果很好。