Udev

簡單的 udev 規則不執行 RUN

  • June 29, 2020

無法設置簡單的 udev 規則。

pi@raspberrypi:~ $ ls -l /home/pi/test.sh
-rwxr-xr-x 1 pi pi 37 Jun 25 15:20 /home/pi/test.sh  # executable
pi@raspberrypi:~ $ cat /home/pi/test.sh
#!/bin/sh
echo asdf >> /tmp/asdf.txt       # this works when run manually, even as root
pi@raspberrypi:~ $ cat /etc/udev/rules.d/10-usb-uart.rules
SUBSYSTEM=="tty", ACTION=="add", ATTRS{idVendor}=="0403", RUN+="/home/pi/test.sh"

我從中得到ATTRS{idVendor}=="0403"

pi@raspberrypi:~ $ sudo udevadm info -a /dev/ttyUSB0
looking at device '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/ttyUSB0/tty/ttyUSB0':
   KERNEL=="ttyUSB0"
   SUBSYSTEM=="tty"
   DRIVER==""

 looking at parent device '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/ttyUSB0':
   KERNELS=="ttyUSB0"
   ...
   ATTRS{port_number}=="0"

 looking at parent device '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0':
   KERNELS=="1-1.3:1.0"
   SUBSYSTEMS=="usb"
   DRIVERS=="ftdi_sio"
   ATTRS{authorized}=="1"
   ...
   ATTRS{supports_autosuspend}=="1"

 looking at parent device '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3':
   KERNELS=="1-1.3"
   SUBSYSTEMS=="usb"
   DRIVERS=="usb"
   ...
   ATTRS{devnum}=="8"
   ATTRS{devpath}=="1.3"
   ATTRS{devspec}=="  (null)"
   ATTRS{idProduct}=="6001"
   ATTRS{idVendor}=="0403"
   ATTRS{ltm_capable}=="no"
   ATTRS{manufacturer}=="FTDI"
   ...

然而,當我連接或斷開 USB 設備時,文件上沒有輸出。


udevadm test:

pi@raspberrypi:~ $ udevadm test $(udevadm info -q path -n /dev/ttyUSB0) 2>&1
calling: test
version 232
This program is for debugging only, it does not run any program
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.

=== trie on-disk ===
tool version:          232
file size:         8788458 bytes
header size             80 bytes
strings            1851626 bytes
nodes              6936752 bytes
Load module index
Found container virtualization none
timestamp of '/etc/systemd/network' changed
timestamp of '/lib/systemd/network' changed
Skipping overridden file: /lib/systemd/network/99-default.link.
Skipping empty file: /etc/systemd/network/99-default.link
Created link configuration context.
timestamp of '/etc/udev/rules.d' changed
Reading rules file: /lib/udev/rules.d/10-local-rpi.rules
Reading rules file: /etc/udev/rules.d/10-usb-uart.rules       # my file
Reading rules file: /lib/udev/rules.d/15-i2c-modprobe.rules
Reading rules file: /lib/udev/rules.d/40-scratch.rules
Reading rules file: /lib/udev/rules.d/40-usb_modeswitch.rules
Reading rules file: /lib/udev/rules.d/50-firmware.rules
Reading rules file: /lib/udev/rules.d/50-udev-default.rules
Reading rules file: /lib/udev/rules.d/55-dm.rules
Reading rules file: /lib/udev/rules.d/60-block.rules
Reading rules file: /lib/udev/rules.d/60-bluealsa.rules
Reading rules file: /lib/udev/rules.d/60-cdrom_id.rules
Reading rules file: /lib/udev/rules.d/60-crda.rules
Reading rules file: /lib/udev/rules.d/60-drm.rules
Reading rules file: /lib/udev/rules.d/60-evdev.rules
Reading rules file: /lib/udev/rules.d/60-fuse.rules
Reading rules file: /lib/udev/rules.d/60-i2c-tools.rules
Reading rules file: /lib/udev/rules.d/60-libgphoto2-6.rules
Reading rules file: /lib/udev/rules.d/60-persistent-alsa.rules
Reading rules file: /lib/udev/rules.d/60-persistent-input.rules
Reading rules file: /lib/udev/rules.d/60-persistent-storage-dm.rules
Reading rules file: /lib/udev/rules.d/60-persistent-storage-tape.rules
Reading rules file: /lib/udev/rules.d/60-persistent-storage.rules
Reading rules file: /lib/udev/rules.d/60-persistent-v4l.rules
Reading rules file: /lib/udev/rules.d/60-serial.rules
Reading rules file: /lib/udev/rules.d/60-triggerhappy.rules
Reading rules file: /lib/udev/rules.d/64-btrfs.rules
Reading rules file: /lib/udev/rules.d/64-xorg-xkb.rules
Reading rules file: /lib/udev/rules.d/65-libwacom.rules
Reading rules file: /lib/udev/rules.d/69-libmtp.rules
Reading rules file: /lib/udev/rules.d/70-debian-uaccess.rules
Reading rules file: /lib/udev/rules.d/70-mouse.rules
Reading rules file: /lib/udev/rules.d/70-power-switch.rules
Reading rules file: /lib/udev/rules.d/70-touchpad.rules
Reading rules file: /lib/udev/rules.d/70-uaccess.rules
Reading rules file: /lib/udev/rules.d/71-seat.rules
Reading rules file: /lib/udev/rules.d/73-seat-late.rules
Reading rules file: /lib/udev/rules.d/73-special-net-names.rules
Reading rules file: /lib/udev/rules.d/73-usb-net-by-mac.rules
Reading rules file: /lib/udev/rules.d/75-net-description.rules
Reading rules file: /lib/udev/rules.d/75-probe_mtd.rules
Reading rules file: /lib/udev/rules.d/78-sound-card.rules
Reading rules file: /lib/udev/rules.d/80-debian-compat.rules
Reading rules file: /lib/udev/rules.d/80-drivers.rules
Reading rules file: /lib/udev/rules.d/80-ifupdown.rules
Reading rules file: /lib/udev/rules.d/80-libinput-device-groups.rules
Reading rules file: /lib/udev/rules.d/80-net-setup-link.rules
Reading rules file: /lib/udev/rules.d/80-udisks2.rules
Reading rules file: /lib/udev/rules.d/85-hwclock.rules
Reading rules file: /lib/udev/rules.d/85-regulatory.rules
Reading rules file: /lib/udev/rules.d/90-alsa-restore.rules
Reading rules file: /lib/udev/rules.d/90-console-setup.rules
Reading rules file: /lib/udev/rules.d/90-libinput-model-quirks.rules
Reading rules file: /lib/udev/rules.d/90-pi-bluetooth.rules
Reading rules file: /lib/udev/rules.d/95-wedo.rules
Reading rules file: /lib/udev/rules.d/97-hid2hci.rules
Reading rules file: /etc/udev/rules.d/99-com.rules
Reading rules file: /lib/udev/rules.d/99-systemd.rules
rules contain 196608 bytes tokens (16384 * 12 bytes), 24141 bytes strings
17173 strings (139139 bytes), 14825 de-duplicated (117347 bytes), 2349 trie nodes used
RUN '/home/pi/test.sh' /etc/udev/rules.d/10-usb-uart.rules:1
PROGRAM 'usb_modeswitch --symlink-name /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/ttyUSB0/tty/ttyUSB0 0403 6001 ' /lib/udev/rules.d/40-usb_modeswitch.rules:10
starting 'usb_modeswitch --symlink-name /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/ttyUSB0/tty/ttyUSB0 0403 6001 '
Process 'usb_modeswitch --symlink-name /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/ttyUSB0/tty/ttyUSB0 0403 6001 ' succeeded.
GROUP 20 /lib/udev/rules.d/50-udev-default.rules:26
IMPORT builtin 'usb_id' /lib/udev/rules.d/60-serial.rules:8
/sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0: if_class 255 protocol 0
IMPORT builtin 'hwdb' /lib/udev/rules.d/60-serial.rules:8
IMPORT builtin 'path_id' /lib/udev/rules.d/60-serial.rules:15
LINK 'serial/by-path/platform-3f980000.usb-usb-0:1.3:1.0-port0' /lib/udev/rules.d/60-serial.rules:17
IMPORT builtin skip 'usb_id' /lib/udev/rules.d/60-serial.rules:19
LINK 'serial/by-id/usb-FTDI_TTL232R-3V3_FT98J77X-if00-port0' /lib/udev/rules.d/60-serial.rules:24
handling device node '/dev/ttyUSB0', devnum=c188:0, mode=0660, uid=0, gid=20
preserve permissions /dev/ttyUSB0, 020660, uid=0, gid=20
preserve already existing symlink '/dev/char/188:0' to '../ttyUSB0'
found 'c188:0' claiming '/run/udev/links/\x2fserial\x2fby-id\x2fusb-FTDI_TTL232R-3V3_FT98J77X-if00-port0'
creating link '/dev/serial/by-id/usb-FTDI_TTL232R-3V3_FT98J77X-if00-port0' to '/dev/ttyUSB0'
preserve already existing symlink '/dev/serial/by-id/usb-FTDI_TTL232R-3V3_FT98J77X-if00-port0' to '../../ttyUSB0'
found 'c188:0' claiming '/run/udev/links/\x2fserial\x2fby-path\x2fplatform-3f980000.usb-usb-0:1.3:1.0-port0'
creating link '/dev/serial/by-path/platform-3f980000.usb-usb-0:1.3:1.0-port0' to '/dev/ttyUSB0'
preserve already existing symlink '/dev/serial/by-path/platform-3f980000.usb-usb-0:1.3:1.0-port0' to '../../ttyUSB0'
.ID_PORT=0
ACTION=add
DEVLINKS=/dev/serial/by-path/platform-3f980000.usb-usb-0:1.3:1.0-port0 /dev/serial/by-id/usb-FTDI_TTL232R-3V3_FT98J77X-if00-port0
DEVNAME=/dev/ttyUSB0
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/ttyUSB0/tty/ttyUSB0
ID_BUS=usb
ID_MODEL=TTL232R-3V3
ID_MODEL_ENC=TTL232R-3V3
ID_MODEL_FROM_DATABASE=FT232 Serial (UART) IC
ID_MODEL_ID=6001
ID_PATH=platform-3f980000.usb-usb-0:1.3:1.0
ID_PATH_TAG=platform-3f980000_usb-usb-0_1_3_1_0
ID_REVISION=0600
ID_SERIAL=FTDI_TTL232R-3V3_FT98J77X
ID_SERIAL_SHORT=FT98J77X
ID_TYPE=generic
ID_USB_DRIVER=ftdi_sio
ID_USB_INTERFACES=:ffffff:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=FTDI
ID_VENDOR_ENC=FTDI
ID_VENDOR_FROM_DATABASE=Future Technology Devices International, Ltd
ID_VENDOR_ID=0403
MAJOR=188
MINOR=0
SUBSYSTEM=tty
TAGS=:systemd:
USEC_INITIALIZED=321019972
run: '/home/pi/test.sh'                                   # That's good!
Unload module index
Unloaded link configuration context.

最後的“執行”是正確的。ACTION和也是如此ID_VENDOR_ID

所以,“RUN”看起來不錯,但沒有輸出/tmp/asdf.txt


udevadm monitor:

KERNEL[972.728210] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2 (usb)
KERNEL[972.734972] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0 (usb)
KERNEL[972.735197] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/ttyUSB0 (usb-serial)
KERNEL[972.736614] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/ttyUSB0/tty/ttyUSB0 (tty)
UDEV  [972.757922] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2 (usb)
UDEV  [972.765063] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0 (usb)
UDEV  [972.770564] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/ttyUSB0 (usb-serial)
UDEV  [972.798201] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/ttyUSB0/tty/ttyUSB0 (tty)

並且udevadm monitor --property

UDEV  [1280.772278] add      /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/ttyUSB0/tty/ttyUSB0 (tty)
.ID_PORT=0
ACTION=add
DEVLINKS=/dev/serial/by-path/platform-3f980000.usb-usb-0:1.2:1.0-port0 /dev/serial/by-id/usb-FTDI_TTL232R-3V3_FT98J77X-if00-port0
DEVNAME=/dev/ttyUSB0
DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/ttyUSB0/tty/ttyUSB0
ID_BUS=usb
ID_MODEL=TTL232R-3V3
ID_MODEL_ENC=TTL232R-3V3
ID_MODEL_FROM_DATABASE=FT232 Serial (UART) IC
ID_MODEL_ID=6001
ID_PATH=platform-3f980000.usb-usb-0:1.2:1.0
ID_PATH_TAG=platform-3f980000_usb-usb-0_1_2_1_0
ID_REVISION=0600
ID_SERIAL=FTDI_TTL232R-3V3_FT98J77X
ID_SERIAL_SHORT=FT98J77X
ID_TYPE=generic
ID_USB_DRIVER=ftdi_sio
ID_USB_INTERFACES=:ffffff:
ID_USB_INTERFACE_NUM=00
ID_VENDOR=FTDI
ID_VENDOR_ENC=FTDI
ID_VENDOR_FROM_DATABASE=Future Technology Devices International, Ltd
ID_VENDOR_ID=0403
MAJOR=188
MINOR=0
SEQNUM=3895
SUBSYSTEM=tty
TAGS=:systemd:
USEC_INITIALIZED=1280765243

這個沒有“RUN”。

udevadm monitor --property仍然顯示輸出。雖然沒有“執行”欄位。我不知道我是否應該期待它出現在那裡?udevadm test顯示它,所以我很驚訝udevadm monitor --property沒有。


我嘗試將名稱更改10-usb-uart.rules98-usb-uart.rules, 為 no av。

嘗試 , OPTIONS="last_rule"在以 ,開頭的文件中添加10-沒有 av。


我應該期望udevadm monitor --property展示runudevadm test

我還能執行什麼來調試正在發生的事情?

腳本的內容/home/pi/test.sh是問題所在。

雖然它有#!/bin/sh,但內置命令echo失敗。

/bin/echo用作品替換它:

$ cat /home/pi/test.sh
#!/bin/sh
/bin/echo asdf >> /tmp/asdf.txt

我不確定為什麼內置函式不起作用,尤其是在指定了解釋器之後。這似乎是一個問題,sh但不是bash,這也有效:

$ cat /home/pi/test.sh
#!/bin/bash
echo asdf >> /tmp/asdf.txt

作為旁注,udevadm monitor --property似乎沒有顯示RUN,這是一種恥辱。

引用自:https://unix.stackexchange.com/questions/595056