我在 Raspberry Pi 上使用 Raspian (Debian) Stretch 时遇到了一个奇怪的 udev 问题。这是一个精简的 udev 规则,在连接任何 USB 闪存驱动器时触发:
# /etc/udev/rules.d/70-usb-test.rules
KERNEL=="sd?1", SUBSYSTEMS=="usb", RUN+="/opt/bin/usb-test.sh"
KERNEL=="sd?1", SUBSYSTEMS=="usb", SYMLINK+="usb-test"
内容/opt/bin/usb-test.sh
(该脚本被标记为可执行):
#!/bin/bash
date >> /var/log/usb-test.log
创建了符号链接/dev/usb-test
,但没有创建文件/var/log/usb-test.log
。
这是以下的输出tail /var/log/syslog
:
Oct 30 15:24:02 raspberrypi kernel: [ 9382.740128] scsi 2:0:0:0: Direct-Access 0.00 PQ: 0 ANSI: 2
Oct 30 15:24:02 raspberrypi kernel: [ 9382.747435] sd 2:0:0:0: Attached scsi generic sg2 type 0
Oct 30 15:24:02 raspberrypi kernel: [ 9382.748278] sd 2:0:0:0: [sdc] 15794176 512-byte logical blocks: (8.09 GB/7.53 GiB)
Oct 30 15:24:02 raspberrypi kernel: [ 9382.748810] sd 2:0:0:0: [sdc] Write Protect is off
Oct 30 15:24:02 raspberrypi kernel: [ 9382.748827] sd 2:0:0:0: [sdc] Mode Sense: 00 00 00 00
Oct 30 15:24:02 raspberrypi kernel: [ 9382.749235] sd 2:0:0:0: [sdc] Asking for cache data failed
Oct 30 15:24:02 raspberrypi kernel: [ 9382.749247] sd 2:0:0:0: [sdc] Assuming drive cache: write through
Oct 30 15:24:02 raspberrypi kernel: [ 9382.909187] sdc: sdc1
Oct 30 15:24:02 raspberrypi kernel: [ 9382.913033] sd 2:0:0:0: [sdc] Attached SCSI removable disk
Oct 30 15:24:03 raspberrypi systemd-udevd[3853]: Process '/opt/bin/usb-test.sh' failed with exit code 1.
lsusb:
$ lsusb
Bus 001 Device 005: ID 20a0:0006 Clay Logic
Bus 001 Device 011: ID 1307:0165 Transcend Information, Inc. 2GB/4GB/8GB Flash Drive
Bus 001 Device 006: ID 0480:a009 Toshiba America Inc Stor.E Basics
Bus 001 Device 009: ID 0480:a202 Toshiba America Inc Canvio Basics HDD
Bus 001 Device 004: ID 1a40:0101 Terminus Technology Inc. Hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. SMC9514 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
udevadm 监视器:
$ udevadm monitor
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent
KERNEL[9851.384334] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3 (usb)
KERNEL[9851.402507] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0 (usb)
KERNEL[9851.418037] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2 (scsi)
KERNEL[9851.418582] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/scsi_host/host2 (scsi_host)
UDEV [9851.545506] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3 (usb)
UDEV [9851.570276] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0 (usb)
UDEV [9851.593606] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2 (scsi)
UDEV [9851.615895] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/scsi_host/host2 (scsi_host)
KERNEL[9852.486019] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/target2:0:0 (scsi)
KERNEL[9852.486271] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/target2:0:0/2:0:0:0 (scsi)
KERNEL[9852.486448] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/target2:0:0/2:0:0:0/scsi_disk/2:0:0:0 (scsi_disk)
KERNEL[9852.486620] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/target2:0:0/2:0:0:0/scsi_device/2:0:0:0 (scsi_device)
KERNEL[9852.494568] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/target2:0:0/2:0:0:0/scsi_generic/sg2 (scsi_generic)
KERNEL[9852.496172] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/target2:0:0/2:0:0:0/bsg/2:0:0:0 (bsg)
KERNEL[9852.498091] add /devices/virtual/bdi/8:32 (bdi)
UDEV [9852.549816] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/target2:0:0 (scsi)
UDEV [9852.560113] add /devices/virtual/bdi/8:32 (bdi)
UDEV [9852.590072] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/target2:0:0/2:0:0:0 (scsi)
KERNEL[9852.659280] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/target2:0:0/2:0:0:0/block/sdc (block)
KERNEL[9852.659644] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/target2:0:0/2:0:0:0/block/sdc/sdc1 (block)
UDEV [9852.679376] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/target2:0:0/2:0:0:0/scsi_disk/2:0:0:0 (scsi_disk)
UDEV [9852.726897] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/target2:0:0/2:0:0:0/scsi_device/2:0:0:0 (scsi_device)
UDEV [9852.741386] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/target2:0:0/2:0:0:0/bsg/2:0:0:0 (bsg)
UDEV [9852.745223] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/target2:0:0/2:0:0:0/scsi_generic/sg2 (scsi_generic)
UDEV [9853.032330] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/target2:0:0/2:0:0:0/block/sdc (block)
UDEV [9853.256996] add /devices/platform/soc/20980000.usb/usb1/1-1/1-1.3/1-1.3.3/1-1.3.3:1.0/host2/target2:0:0/2:0:0:0/block/sdc/sdc1 (block)
有人知道发生了什么事吗?
答案1
发生了一些棘手的事情。您必须通过 重新启动 udev 服务service udev restart
,然后 udev RUN 命令才能按预期工作。
答案2
我遇到了类似的问题,我传递给 RUN 的脚本失败了。该问题与 systemd-udevd.service 文件中应用的权限有关。您将需要禁用其中一些权限。我的解决方案是从中提取的讨论。
在我的例子中,文件 /lib/systemd/system/systemd-udevd.service 的最终内容如下:
[Service]
Type=notify
OOMScoreAdjust=-1000
Sockets=systemd-udevd-control.socket systemd-udevd-kernel.socket
Restart=always
RestartSec=0
ExecStart=/lib/systemd/systemd-udevd
KillMode=mixed
WatchdogSec=3min
TasksMax=infinity
MountFlags=slave
MemoryDenyWriteExecute=yes
RestrictRealtime=yes
RestrictAddressFamilies=AF_UNIX AF_NETLINK AF_INET AF_INET6e
正如我提供的链接中所建议的,快速解决方案是注释掉最后七行,如下所示:
[Service]
Type=notify
OOMScoreAdjust=-1000
Sockets=systemd-udevd-control.socket systemd-udevd-kernel.socket
Restart=always
RestartSec=0
ExecStart=/lib/systemd/systemd-udevd
#KillMode=mixed
#WatchdogSec=3min
#TasksMax=infinity
#MountFlags=slave
#MemoryDenyWriteExecute=yes
#RestrictRealtime=yes
#RestrictAddressFamilies=AF_UNIX AF_NETLINK AF_INET AF_INET6e
较长的解决方案是研究每个注释掉的标志的影响,并仅注释掉适用于您的要求的标志。然后使用以下命令重新加载并重新启动服务,或重新启动 pi:
# systemctl daemon-reload
# systemctl restart systemd-udevd.service
答案3
我遇到了同样的问题(连接任何 USB 闪存驱动器时脚本失败,退出代码为 1)。我只是注释掉该RestrictRealtime=yes
行,现在工作正常。
答案4
就我而言,我systemd-udevd[123456]: 2-2: Process 'touch /tmp/test.txt' failed with exit code 1.
从/var/log/syslog
.
SUBSYSTEM=="usb", ACTION=="add", RUN+="touch /tmp/test.txt"
但当我将touch
其更改为 时/usr/bin/touch
,问题就消失了。
SUBSYSTEM=="usb", ACTION=="add", RUN+="/usr/bin/touch /tmp/test.txt"