设置简单的 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
仍然显示输出。但没有“RUN”字段。我不知道我是否应该期待它出现在那里?udevadm test
显示了它,所以我很惊讶udevadm monitor --property
没有。
我尝试将名称更改10-usb-uart.rules
为98-usb-uart.rules
, 更改为 no av。
我有尝试添加 , OPTIONS="last_rule"
在以 , 开头的文件中10-
,没有 av。
我应该像现在这样期待udevadm monitor --property
展示吗?run
udevadm test
我还可以执行什么来调试正在发生的事情?
答案1
脚本的内容/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
,这是一种耻辱。