我有一个位于 /etc/udev/rules.d/usbmount.rules 的 udev 规则文件,其中包含以下内容:
ACTION=="add", KERNEL=="sd*[!0-9]", RUN+="/tmp/debug.sh"
创建规则后,我做了:
chmod +x /tmp/debug.sh
udevadm control --reload-rules
此规则的目的是在插入 USB 块设备(拇指驱动器、硬盘等)时运行脚本。我很想知道为什么它没有触发。udevadmmonitor
当 USB 硬盘驱动器坞站通过 USB 3.0 端口连接到系统时,A给出以下信息。
KERNEL[367.932459] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2 (usb)
KERNEL[367.934622] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0 (usb)
KERNEL[367.935236] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0 (scsi)
KERNEL[367.935301] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/scsi_host/host0 (scsi_host)
KERNEL[367.935414] bind /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0 (usb)
KERNEL[367.935527] bind /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2 (usb)
UDEV [367.937750] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2 (usb)
UDEV [367.939195] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0 (usb)
UDEV [367.939940] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0 (scsi)
UDEV [367.945817] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/scsi_host/host0 (scsi_host)
UDEV [367.947464] bind /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0 (usb)
UDEV [367.950394] bind /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2 (usb)
KERNEL[368.957786] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0 (scsi)
KERNEL[368.957902] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0/0:0:0:0 (scsi)
KERNEL[368.957993] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0/0:0:0:0/scsi_device/0:0:0:0 (scsi_device)
KERNEL[368.958128] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0/0:0:0:0/scsi_generic/sg0 (scsi_generic)
KERNEL[368.958250] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0/0:0:0:0/bsg/0:0:0:0 (bsg)
KERNEL[368.960906] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0/0:0:0:0/scsi_disk/0:0:0:0 (scsi_disk)
UDEV [368.961035] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0 (scsi)
UDEV [368.965594] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0/0:0:0:0 (scsi)
UDEV [368.971313] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0/0:0:0:0/scsi_device/0:0:0:0 (scsi_device)
UDEV [368.973129] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0/0:0:0:0/bsg/0:0:0:0 (bsg)
UDEV [368.975431] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0/0:0:0:0/scsi_generic/sg0 (scsi_generic)
UDEV [368.976153] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0/0:0:0:0/scsi_disk/0:0:0:0 (scsi_disk)
KERNEL[369.010196] add /devices/virtual/bdi/8:0 (bdi)
UDEV [369.011230] add /devices/virtual/bdi/8:0 (bdi)
KERNEL[369.270352] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0/0:0:0:0/block/sda (block)
KERNEL[369.270455] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0/0:0:0:0/block/sda/sda1 (block)
KERNEL[369.276694] bind /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0/0:0:0:0 (scsi)
UDEV [369.874116] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0/0:0:0:0/block/sda (block)
UDEV [370.252695] add /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0/0:0:0:0/block/sda/sda1 (block)
UDEV [370.275646] bind /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb2/2-2/2-2:1.0/host0/target0:0:0/0:0:0:0 (scsi)
机器信息:
OS: Raspbian GNU/Linux 10 (buster)
Kernel: 5.4.72-v7l+
debug.sh 的唯一内容是echo "TESTING" >> /tmp/debuglog.txt
。未创建 Debuglog.txt。
看来这条规则没有被触发。为什么?我可以做什么来解决这个问题?
答案1
这debug.sh 的唯一内容是
echo "TESTING" >> /tmp/debuglog.txt
。未创建 Debuglog.txt。
这就是问题所在。内核不会执行这样的 shell 脚本,因为它并不是从强制#!/bin/bash
告诉内核将执行委托给巴什口译员。
这种情况很少发生,因为如果命令没有立即执行,大多数工具都会使用 shell 来执行命令。这是一个例子,当追踪perl 来执行此命令(同时搜索但没有成功使脚本执行立即失败的工具):
$ strace -e execve perl -e 'exec ("/tmp/debug.sh");'
execve("/usr/bin/perl", ["perl", "-e", "exec (\"/tmp/debug.sh\");"], [/* 15 vars */]) = 0
execve("/tmp/debug.sh", ["/tmp/debug.sh"], [/* 15 vars */]) = -1 ENOEXEC (Exec format error)
execve("/bin/sh", ["/bin/sh", "/tmp/debug.sh"], [/* 15 vars */]) = 0
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=14452, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
+++ exited with 0 +++
它首先尝试在第一行没有解释器的情况下执行脚本并得到:
ENOEXEC (Exec format error)
然后再次尝试插入一个外壳,这样它就可以工作了。这里/bin/sh
, 甚至不在bash
这个环境中,所以如果脚本包含bash
构造,它稍后会出现微妙的错误行为。
本来/tmp/debug.sh
是这样的(并且带有u+rx
烫发):
#!/bin/bash
echo "TESTING" >> /tmp/debuglog.txt
然后:
$ strace -e execve perl -e 'exec ("/tmp/debug.sh");'
execve("/usr/bin/perl", ["perl", "-e", "exec (\"/tmp/debug.sh\");"], [/* 15 vars */]) = 0
execve("/tmp/debug.sh", ["/tmp/debug.sh"], [/* 15 vars */]) = 0
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=14474, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
+++ exited with 0 +++
然后/tmp/debug.sh
将由内核直接执行(实际上将执行推迟到/bin/bash
)。
udev
不尝试这样做,所以它需要一个有效的二进制文件对于内核作为命令中的第一个文件。要么直接提供它,要么通过从该行开始提供一个从内核的角度来看是足够可执行的脚本#!/path/to/interpreter
。
答案2
某些系统(所有?)上的 udev 需要执行所有脚本的绝对路径。如果要执行 shell 脚本,则必须指定 shell。为了让它发挥作用,我必须将其更改为:
ACTION=="add", KERNEL=="sd*[!0-9]", RUN+="/bin/bash /tmp/debug.sh"