udev 규칙이 실행되지만 모든 실행 명령이 실패합니다.

udev 규칙이 실행되지만 모든 실행 명령이 실패합니다.

Raspberry Pi에서 Raspian(Debian) Stretch를 사용하는 동안 이상한 udev 문제가 발생했습니다. USB 플래시 드라이브가 연결되면 실행되는 간단한 udev 규칙은 다음과 같습니다.

# /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

힘든 일이 일어났습니다. service udev restartudev RUN 명령이 예상대로 작동하려면 udev 서비스를 다시 시작해야 합니다 .

답변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

제가 제공한 링크에서 제안한 대로 빠른 해결책은 다음과 같이 마지막 7줄을 주석 처리하는 것입니다.

[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"

관련 정보