0

I try to get the path of the last connected disk via an environment variable, i.e. a path like /dev/sd*. The problem is that DEVNAME or DEVPATH does not provide the desired path.

The goal is to make echo $...... > /var/..... once the value is stored, and execute differents actions with RUN +=... I get it out when I need it. I hope I was clear in my explanations, thank you

I'm trying to get the path of the current disk, in this case /dev/sd*. I use udev rules to execute an action

SUBSYSTEM=="usb", ACTION=="add", ENV{DEVTYPE}=="usb_device", RUN+="/bin/device_added.sh" SUBSYSTEM=="usb", ACTION=="remove", ENV{DEVTYPE}=="usb_device", RUN+="/bin/device_removed.sh" 

The problem is that when I retrieve with echo $KERNEL > my-file, I can't find the path of the current disk. I need this variable to execute smartctl commands from the disk that just arrived.

In device_added.sh the goal is to launch the smartctl -a /dev/sd* command except that we replace /dev/sd* by the last connected disk, so here is my problem now, I need to recover the path of the last connected disk/detected by udev rules.

5
  • 3
    Does it have to be the "last connected disk", or would it also work to execute some action when the disk is actually connected? That's the usual approach, and a lot cleaner, but you didn't describe what you need this for.
    – dirkt
    May 5, 2022 at 9:07
  • Can you edit your post to show what DEVNAME and DEVPATH do look like, and why this is the wrong value for your use case?
    – AdminBee
    May 6, 2022 at 10:04
  • @AdminBee DEVPATH is the path into /sys, he is looking for a path to the device node, in particular a device node for the disk itself.
    – dirkt
    May 6, 2022 at 10:51
  • @dirkt I acknowledge the problem about DEVPATH, but in my experience DEVNAME looks exactly what the OP seems to want (judging from udevadm info output), so I wondered why it should be unusable for the task ...
    – AdminBee
    May 6, 2022 at 10:53
  • @AdminBee Because he is matching on the USB device, not the disk. See answer.
    – dirkt
    May 6, 2022 at 10:59

1 Answer 1

0

I am still not sure if I understand the question correctly.

Is the question "when I execute a program with RUN inside a udev rule, how can I access the actual device node that is created by the kernel for that event"? If yes, have a look at the KERNEL environment variable (see man udev), which contains the actual kernel device, e.g. sda when you attach or remove a harddisk.

If that's not what you want, please explain what you need this for, i.e. which actions do you want to do with /dev/sda for what purpose. Please also read up on XY questions.


Your problem is that you are matching on events for USB devices (and those devices allow you to talk to them on the USB level, and are not exclusive to disks), not on events that add the actual disk. You need something like

KERNEL=="sd*", SUBSYSTEMS=="usb", ACTION=="add", ...

either without the DEVTYPE, or with ENV{DEVTYPE}="disk".

On further reading of man udev, there's also the environment variable DEVNODE, which should contain the complete path to the newly created node (I didn't test this).

Finally, if you want to run smartctl commands, keep the following passage about RUN in man udev in mind:

This can only be used for very short-running foreground tasks. Running an event process for a long period of time may block all further events for this or a dependent device.

Starting daemons or other long-running processes is not appropriate for udev; the forked processes, detached or not, will be unconditionally killed after the event handling has finished.

So depending on what you want to do with smartctl, it may or may not be the right place to do that.


I have already tried with your rule that does not work, the disks are not recognized and the script does not run. I tried with KERNEL=="sd[a-z]" not working.

Then something else in your rule is wrong (like an additional condition that is not fulfilled). If udev adds /dev/sd*, a rule with KERNEL=="sd[a-z]" will match.

Please edit your question and add the information obtained by udevadm info -p /sys/class/block/sdx after you plugged in your disk and it appeared as /dev/sdx (replace sdx as appropriate). Compare with the rule you used which didn't work.

3
  • Hello, thank you for your answer. I'm trying to get the path of the current disk, in this case /dev/sd*. I use udev rules to execute an action SUBSYSTEM=="usb", ACTION=="add", ENV{DEVTYPE}=="usb_device", RUN+="/bin/device_added.sh" SUBSYSTEM=="usb", ACTION=="remove", ENV{DEVTYPE}=="usb_device", RUN+="/bin/device_removed.sh" The problem is that when I retrieve with echo $KERNEL > my-file, I can't find the path of the current disk. I need this variable to execute smartctl commands from the disk that just arrived. May 6, 2022 at 7:48
  • The DEVNAME or DEVTYPE environment variables do not provide me with the right path. I hope I was clearer, I am new sorry May 6, 2022 at 7:48
  • answer below, thx May 6, 2022 at 12:04

You must log in to answer this question.

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