You are not logged in.

#1 2016-09-22 16:48:25

tybo
Member
Registered: 2012-05-03
Posts: 21

Udev rule fails because partition is not mounted yet

Hello,

I encountered this case whereby some udev rule attempts to run a command when a specific device is connected.
However, at boot time this happens before the partition on which the program mentioned in the udev rule is stored is even mounted, resulting in an error:
"failed to execute '<...command...>' '<...command...>': No such file or directory"

The device in question seems to work fine though. But still, is there a way I can tell the system that this udev rule depends on the fstab mount (or preferably on that specific partition mount)?
I guess creating a dependency between systemd-udevd and the mount systemd target would not be such a good idea...
This is a strange case I guess, and I am curious to hear if there is some "smart, clean and KISS" way to solve it.

Thank you for your insights!

Offline

#2 2016-09-22 17:34:31

WorMzy
Administrator
From: Scotland
Registered: 2010-06-16
Posts: 13,737
Website

Re: Udev rule fails because partition is not mounted yet

What is this udev rule? Why are you obscurificating the error message?

https://bbs.archlinux.org/viewtopic.php?id=57855

Mod note: Not a System Administration topic, moving to NC.


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

#3 2016-09-22 18:19:57

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: Udev rule fails because partition is not mounted yet

Have udev run a (e.g. BASH) script when the *partition* is seen, which:

1. Mounts the partition
2. Performs whatever else you want to do with the partition.

Offline

#4 2016-09-22 20:34:32

tybo
Member
Registered: 2012-05-03
Posts: 21

Re: Udev rule fails because partition is not mounted yet

Hello,
Thanks for your replies.

@WorMzy, I did not mean to obfuscate or hide important information. I simply considered this information to be irrelevant, but I may of course be wrong on that part.
The main issue is that the command is not accessible yet when udev tries to run it because the partition has not been mounted yet. It is mounted a little while later in the boot sequence.
The device in question is a Digilent JTAG USB device and the program is responsible for some hardware init, I assume.
Of course, I can provide more details if needed.

@brebs, the partition is mounted, but due to systemd's parallelism this happens later in the boot sequence. It also happens that udev sees the device before that and tries to run the command to soon.
Building up on you advice, maybe the right way would be to create a script that probes the system to check whether the program can be seen in the filesystem and if so runs is.
In case the program is not seen in the filesystem, assume the partition is not yet mounted and wait a certain amount of time before trying again.
Maybe a timeout could be useful so that a log entry is created if the program can never be found (for example if it has been moved or removed)?

Maybe something like:

#!/bin/bash

cmd=<...some command...>

fail=1;

for i in {1..3}; do
    if [ -x "$cmd" ]; then # Optionally, [ -x "$(which $cmd)" ]
         fail=0;
         # execute command
         "$cmd" $@ # or "$(which $cmd)" $@
         break;
    else
         sleep 30s;
    fi
done

if [ $fail -eq 1 ]; then
    echo "Error: command '$cmd' not found in the system." >&2
    exit 1
fi
exit 0

I'll try this approach tomorrow.

Thanks!

Offline

#5 2016-09-23 11:49:46

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: Udev rule fails because partition is not mounted yet

My suggestion would be to have a systemd service that runs your program and is ordered after the mounts. This service would be started on demand by changing your udev rule to instead want the systemd service:
TAG+="systemd", ENV{SYSTEMD_WANTS}+="foo.service"

Offline

#6 2016-09-23 21:02:10

brebs
Member
Registered: 2007-04-03
Posts: 3,742

Re: Udev rule fails because partition is not mounted yet

tybo wrote:

@brebs, the partition is mounted, but due to systemd's parallelism this happens later in the boot sequence.

Yes, I can see that. Systemd's parallelism is *creating* this problem, when a simple BASH script would be simple and easy and reliable wink

Offline

#7 2016-09-24 12:46:54

tybo
Member
Registered: 2012-05-03
Posts: 21

Re: Udev rule fails because partition is not mounted yet

@brebs, I implemented the bash script I posted earlier and it seems reliable enough. I don't have error messages in the log files anymore, so this is definitely one possible workaround.

@ukhippo Humm... This looks interesting indeed! It's better to solve the dependency problem directly rather that create a workaround, and what you are hinting at seems to do just that...

For this to work, and if I understand correctly, I need to create a systemd service that can be run multiple times (for every relevant new USB connexion) and that depends on the mount target to solve the dependency issue in systemd.

In addition to that, I need to make sure I can pass some arguments to the systemd service (the Digilent command needs some parameters, usb bus and device number, to work). I have not seen argument passing for systemd services previously, so I'll certainly have to take a look to the wiki for that.

I'll try this new approach and give you a feedback!

Thanks for your help!

Offline

#8 2016-09-24 16:28:19

ukhippo
Member
From: Non-paged pool
Registered: 2014-02-21
Posts: 366

Re: Udev rule fails because partition is not mounted yet

Are you able to uniquely distinguish the device in your udev rules? If you can, that means the service can be specific to the device and you only need to start the service when you plug in that device. If not, this approach is probably not feasible - you don't really want to be starting an irrelevant service every time you plug in a USB device.

If you look at the "bootup" man page, that shows a diagram of the systemd boot logic. If it's OK for your service to run after "basic.target" is reached, that makes the service real easy as the default dependencies will do that for you. It's probably easiest to make your service run a bash script.

[Unit]
Description=...tbs..
[Service]
ExecStart=/path/to/script

Otherwise, you'll have to disable the default dependencies and add them manually depending on where you want the service to run.

Can you make a symlink for the device in your udev rules? If so, that makes the script the service runs easier. You can parse the output of "udevadm info /dev/symlink-name-you-chose" to get the info you need.
Or if you trust the udevadm output:

source <(udevadm info -x -q property /dev/symlink-name-you-chose)

to set environment variables for all the properties.
If it helps you can set additional environment variables in your udev rules, and they will be available in the udevadm info output for the script.

If you still need to pass a parameter to the service, make the service file a template (call it foo@.service rather than foo.service) and change the ExecStart to call "/path/to/script %i".
In your udev rule you'd do ...,ENV{SYSTEMD_WANTS}+="foo@parameter.service"
You'd have to determine what parameter is. It can be a combination of things (which you'd decode in your script), it just has to be valid syntax for systemd.

Offline

Board footer

Powered by FluxBB