1

I have this fairly basic udev rule named /etc/udev/rules.d/99-sd.rules:

ACTION=="add", SUBSYSTEM=="mmc", KERNEL=="mmcblk0p1", RUN+="/usr/bin/echo 'aa' > /tmp/test"

The goal is to create test file when I insert the SD card and it gets its p1 partition recognized.

Now, after creating this rule, I did everything:

 sudo udevadm control --reload-rules
 sudo systemctl restart udev
 sudo reboot

But still that file is not getting created when I insert the SD card. (And SD card is OK, I can even mount it).

What is the problem here? This drives me crazy and I can't see what's wrong. With udevadm I can see the kernel event coming in about the SD card. But then WHY is not my file getting created?

OS: Debian Bullseye

This is dmesg:

[   85.299246] mmc0: new ultra high speed SDR104 SDXC card at address aaaa
[   85.301507] mmcblk0: mmc0:aaaa SN64G 59.5 GiB
[   85.307164]  mmcblk0: p1

/tmp is empty even when I tried with "bash":

$ cat /etc/udev/rules.d/99-sd.rules
ACTION=="add", SUBSYSTEM=="mmc", KERNEL=="mmcblk0p1", RUN+="/bin/bash -c '/usr/bin/echo aa > /tmp/test'"

udevadm monitor:

$ sudo udevadm monitor
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent

KERNEL[191.651922] add      /devices/platform/fe2b0000.dwmmc/mmc_host/mmc0/mmc0:aaaa (mmc)
KERNEL[191.655196] add      /devices/virtual/bdi/179:96 (bdi)
UDEV  [191.656246] add      /devices/platform/fe2b0000.dwmmc/mmc_host/mmc0/mmc0:aaaa (mmc)
UDEV  [191.656604] add      /devices/virtual/bdi/179:96 (bdi)
KERNEL[191.657702] add      /devices/platform/fe2b0000.dwmmc/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0 (block)
KERNEL[191.657862] add      /devices/platform/fe2b0000.dwmmc/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0/mmcblk0p1 (block)
KERNEL[191.658527] bind     /devices/platform/fe2b0000.dwmmc/mmc_host/mmc0/mmc0:aaaa (mmc)
UDEV  [191.689858] add      /devices/platform/fe2b0000.dwmmc/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0 (block)
UDEV  [191.732813] add      /devices/platform/fe2b0000.dwmmc/mmc_host/mmc0/mmc0:aaaa/block/mmcblk0/mmcblk0p1 (block)
UDEV  [191.735289] bind     /devices/platform/fe2b0000.dwmmc/mmc_host/mmc0/mmc0:aaaa (mmc)
0

2 Answers 2

1

The command to run is given to exec() rather than to the shell, so you cannot use shell syntax like >. Enclose your command eg:

RUN+="/bin/bash -c '/usr/bin/echo aa > /tmp/test'"

If you need to quote spaces within the single quotes you can use escaped double-quotes, eg \"abc def\".

To get more debug in the logs, temporarily set the debug level with sudo udevadm control --log-priority=debug, and tail the log with sudo journalctl -f, then insert the card and you should see any RUN commands. Reset the level with sudo udevadm control --log-priority=info.

8
  • No, sorry. No file gets created.
    – Daniel
    Commented Apr 3, 2023 at 9:59
  • Can it be my problem that Debian's rootfs is overlayfs, and I guess my current root is set by a pivot_root (or chroot)? Does this affect udev?
    – Daniel
    Commented Apr 3, 2023 at 13:27
  • This is hotplug, i.e. I first start the system then I insert the SD. It would be great having this SD mounted at boot time too, but for now I really just wish to understand what ** is going on
    – Daniel
    Commented Apr 3, 2023 at 14:06
  • 1
    Perhaps you have specified the wrong SUBSYSTEM to match. On my raspberry pi the mmcblk0p1 device is in SUBSYSTEM=block.
    – meuh
    Commented Apr 3, 2023 at 14:41
  • 1
    /etc is for the local administrator, you, whereas /usr/lib is for rules distributed by some package.
    – meuh
    Commented Apr 3, 2023 at 15:40
0

I don't think udev runs the command in RUN in a shell, so your > doesn't mean what you think it does. (It's probably just getting passed as an argument to /usr/bin/echo)

Try to write your own shell script that creates the file, or user something like logger

Also, it's unusual to trigger on the appearance of a partition, which happens after the device was detected, udev triggered a partition scan, and the new device nodes were populated. I don't know whether that makes too much sense - luckily, with udevadm monitor you can see which notifications udev gets at all when you connect your memory device.

1
  • Yes udevadm sees the event but doesn't executes my command.
    – Daniel
    Commented Apr 3, 2023 at 10:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .