3

I'm trying to automount a usb to my raspberry pi, but I can't seem to get my udev rule to work.

The code I'm using is:

ACTION=="add", ENV{ID_MODEL}=="Flash_Drive", RUN+="bin/mkdir /home/pi/heyimhere"

This doesn't work as no directory at that location is created as the flash drive is being plugged in.

I had luck using usb to shut down using the same rules, this time shutting it down using this line:

ACTION=="remove", ENV{ID_MODEL}=="Flash_Drive", RUN+="sbin/shutdown -h now"

Which works - as soon as you remove the usb it shuts down.

Why? Should I use ATTR for add?

1
  • 2
    A udev rule is the wrong approach. You want SYSTEMD_WANTS; see man systemd.device.
    – jasonwryan
    Nov 5, 2017 at 21:50

1 Answer 1

1

ENV vs ATTR/ATTRS

ATTR{filename}
  Match sysfs attribute values of the event device.

ATTRS{filename}
  Search the devpath upwards for a device with matching sysfs attribute values. If multiple ATTRS matches are specified, all of them must match on the same device.

ENV{key}
  Match against a device property value.

If both ENV and ATTR contain the same information - you can use any of them, there is no any difference.

ENVs for the device in question can contain values derived from different parent devices. So with ENVs you can sometimes match on values from several parent devices simultaneously, which is impossible with ATTRS.


Rule for creating a directory

You must use absolute path in RUN key (RUN+="/bin/mkdir /home/pi/heyimhere" in your example):

If no absolute path is given, the program is expected to live in /usr/lib/udev; otherwise, the absolute path must be specified.

By the way, I don't know why your shutdown rule works. It shouldn't, because of sbin/shutdown without leading /.


Mounting from udev rules

As jasonwryan mentioned, you can't use udev for automount:

Note that running programs that access the network or mount/unmount filesystems is not allowed inside of udev rules, due to the default sandbox that is enforced on systemd-udevd.service.

1
  • 2
    Note that the ENV{key} attributes may be defined by earlier rules, so if you use them, your rule must come after some other rule that defines the corresponding key with ENV{key}=<something>. The ENV{key} attributes will also be available as environment variables for any programs you might run with the PROGRAM= rule element.
    – telcoM
    Jul 16, 2020 at 21:11

You must log in to answer this question.

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