6

I have a strange problem with udev using Raspian (Debian) Stretch on my Raspberry Pi. Here is a stripped down udev rule which triggers on attaching any usb flashdrive:

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

Content of /opt/bin/usb-test.sh (The script is marked as executable):

#!/bin/bash
date >> /var/log/usb-test.log

The symlink /dev/usb-test is created, but not the file /var/log/usb-test.log.

This is the output of 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 monitor:

$ 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)

Anybody knows what's going on?

  • Try which touch. – Ralph Rönnquist Oct 30 '17 at 12:23
  • I did. that gives me /usr/bin/touch. – relthyg Oct 30 '17 at 12:30
  • Maybe /usr/bin/touch is a link (e.g., to /bin/touch), and udev takes offense of (the attempt) to execute a link? Or maybe /usr isn't available at the time? – Ralph Rönnquist Oct 30 '17 at 12:49
  • /usr/bin/touch is indeed symlinked to /bin/touch, but that doesn't matter. The same rule with /bin/touch doesn't work either. – relthyg Oct 30 '17 at 13:01
  • @relthyg can you update the question and show the output of lsusb. Can you run udevadm control --reload-rules. Also while inserting the flash-drive can you run udevadm monitor ? See what it tells you – Valentin Bajrami Oct 30 '17 at 13:59
3

There's something dodgy going on. You have to restart the udev service via service udev restart, and then the udev RUN commands work as expected.

| improve this answer | |
  • 2
    Dodgy, indeed... I have the same problem. On a Raspberry Pi 3 with Raspbian Stretch (2018-03-13), a restart of the service is needed. Worse, after rebooting the device, the failed with exit code X is back. So far, exit code 1 is the most common, but I also saw 2 and 127... – jcb Mar 28 '18 at 6:38
  • I suspect it could be some kind of race condition. Perhaps not all services or the full FS are up and running? I have a similar issue here. – not2qubit Apr 27 '18 at 17:10
0

I had a similar problem where the script I passed to RUN was failing. The problem is related to the permissions applied in the systemd-udevd.service file. You will need to disable some of these permissions. My solution was extracted from this discussion.

The end content of the file /lib/systemd/system/systemd-udevd.service was, in my case, as follows:

[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

The quick solution, as suggested in the link I provided, is to comment out the last seven lines as follows:

[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

The longer solution is to research the affects of each of the commented out flags, and only comment out the ones applicable for your requirements. Then reload and restart the service with the following commands, or reboot the pi:

# systemctl daemon-reload
# systemctl restart systemd-udevd.service
| improve this answer | |
  • This sounds like a good way to learn. I would point out you can copy the file from /lib/systemd/system to /etc/systemd/system and edit it there, this avoids editing package-owned files in /lib... which could be overwritten on package upgrade... it also makes it easy to refer back and compare the original later if you need to :) – sourcejedi May 3 '18 at 21:30
0

I had the same issue (script failed with exit code 1 on attaching any USB flashdrive). I just comment out the RestrictRealtime=yes line, and now it's working fine.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.