LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (https://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   USB flash disk read problems using udev rules (https://www.linuxquestions.org/questions/linux-desktop-74/usb-flash-disk-read-problems-using-udev-rules-704853/)

janfranken 02-15-2009 01:31 PM

USB flash disk read problems using udev rules
 
Hello, I have the following problem:

I create a rule to launch an application when my USB flash disk is inserted:
ACTION=="add", KERNEL=="sd?1", ATTRS{removable}=="1", MODE="0666", RUN+="/usr/bin/application $env{ID_FS_LABEL} &"

The application then runs a usb_detect.sh script that must copy the files from the USB disk to the local disk.
popen(“usb_detect.sh device_id &”, "r");


The script executes but for some reason it is unable to copy any files from the USB disk. It can’t even read a filename from the USB disk.

When I run the application from terminal (instead of using udev rules) then everything works fine, the usb_detect.sh script can read and copy the files from the USB disk.

Any idea why the usb_detect.sh script can’t read any files from the USB disk when using udev rules, but works when using terminal?

Thank you.

ciotog 02-16-2009 09:15 AM

There might be a couple of issues I can think of - for one, udev is effectively suspended while a RUN action is being performed, so if your script takes too long there might be issues. You could detach the script to fix it.

Another might be that there's no way that I know of to get output - perhaps your script is writing to stdout, but gets a broken pipe error?

Maybe you could post your usb_detect.sh script, and that might help :)

janfranken 02-16-2009 12:02 PM

Thanks for the reply...

The usb_detect.sh script calls the actual copy script as shown below:
./copy.sh $disk_label

The copy script is quite involved, since it does a lot of other things as well. I don't think it will help that much.

I think the problem lies somewhere in the fact that udev somehow holds the system back as you explained.

Thank you anyway, I will try some work around...

Jan (South Africa)

tredegar 02-16-2009 12:27 PM

Quote:

The application then runs a usb_detect.sh ... [snip] ... The script executes but for some reason it is unable to copy any files from the USB disk. It can’t even read a filename from the USB disk.
My suggestion: Have "The application" run usb_detect.sh and redirect the errors from that script to a logfile. Then look at the logfile, it should tell you what failed or was broken.

Quote:

The usb_detect.sh script calls the actual copy script as shown below:
./copy.sh $disk_label
I note you are using a relative PATH here. How are you sure the relative path is correct? Try replacing all relative paths in scripts with absolute ones Eg /full/path/to/copy.sh rather than ./copy.sh

janfranken 02-17-2009 11:28 AM

Hi,

I redirected the errors to a text file.

The output error says "no such file or directory".

Running the same thing from terminal doesn't give such an error.

So the script is ok, but the USB device is unreadable.

It could be that the device is not mounted when the rule is activated. I

have tried to use & when calling scripts to try and "release" udev but it doesn't help.

Thanks.
Jan.

tredegar 02-17-2009 12:31 PM

Quote:

The output error says "no such file or directory".
I don't wish to appear unduly critical, but error messsages are usually a little bit more helpful:
Code:

tred@vaio:~$ ls /foo/bar
ls: cannot access /foo/bar: No such file or directory
tred@vaio:~$

So, what is / are your exact errors?

I still think you have a path problem: When you wrote those scripts maybe you assumed that they'd be called from ~ because they work when you call them from there.

When udev runs them they will be being called from somewhere else and not by the user janfranken. Think about this.

Maybe, just before your script fails with "file not found", insert a
Code:

echo scriptname is in the directory $(pwd) and about to execute the next command, which fails  >> logfile
which may give you some insight.

Or maybe insert
Code:

echo this is what's mounted right now >> logfile
mount >> logfile
the_line_that_fails

You could post your script(s) here. Please post the name of the script, and the full path to it, and its contents, like this:

Code:

cat /home/jan/copy.sh
script2 &
asd
asdasd
asdasd
etc

cat /home/jan/script2
more commands
etc



If they are long you could put them all in one post here: http://rafb.net/paste/terms.html (only good for 24Hrs though) then we'd have a better idea of what it is you are trying to do, and maybe why it isn't working for you.

nx5000 02-18-2009 06:33 PM

i think it has to be done on hal level which is the one mounting .
Udev creates the dev when it gets an event from the kernel.it then sends a dbus message to a listener that might mount it.
For fun, i wanted to have a sound when a usb stick was inserted like on windows, i changed an hal policy IIRC. I might have a backup somewhere, i'll post back. It took me ages to make it work..


All times are GMT -5. The time now is 02:01 PM.