LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Getting UDEV working (https://www.linuxquestions.org/questions/linux-newbie-8/getting-udev-working-4175572011/)

Mikeoxlarge 02-11-2016 01:14 PM

Getting UDEV working
 
Hey guys, first post here so go gentle

I've created a udev rule file in my udev/rules.d with the following contents,


Code:

ACTION=="add", SUBSYSTEM=="usb",  ATTRS{idVendor}=="18a5", ATTRS{idProduct}=="0302", RUN+="/etc/demo/insert.sh"

ACTION=="remove", SUBSYSTEM=="usb",  ATTRS{idVendor}=="18a5", ATTRS{idProduct}=="0302", RUN+="/etc/demo/remove.sh"

I have restarted to udev rules with

Code:

sudo udevadm control --reload-rules
and don't get any error messages, I have tried inserting a usb stick with the following parameters

Code:

Bus 001 Device 008: ID 18a5:0302 Verbatim, Ltd 32GB Flash Drive
Which I believe should fire the script off when the usb drive is plugged in, I'm pretty sure I had this going once but now it does nothing, what am I doing wrong?

I've scoured the net but cant find anything anywhere, any thoughts/suggestions?

Thanks

Mike

malekmustaq 02-11-2016 01:37 PM

Check your log if the two events happened at the instant.
Or maybe your system have had no time enough to comple insert.sh before remove.sh takes over.
Try: comment the remove.sh line, reload udev, insert, and check by fdisk -l see if the drive is honored. If it is there then you are doing well.

Don't forget to
Quote:

sudo chmod +x /etc/demo/insert.sh
sudo chmod +x /etc/demo/remove.sh
Hope that helps. Good luck.

m.m.

Mikeoxlarge 02-11-2016 01:59 PM

Quote:

Originally Posted by malekmustaq (Post 5498735)
Check your log if the two events happened at the instant.
Or maybe your system have had no time enough to comple insert.sh before remove.sh takes over.
Try: comment the remove.sh line, reload udev, insert, and check by fdisk -l see if the drive is honored. If it is there then you are doing well.

Don't forget to


Hope that helps. Good luck.

m.m.

I've divided the udev rules in to two now as I had a suspicion that the fact there were two in the same rule may have been causing an issue, and yes I had forgotten to make the script executable lol I just facepalmed so hard I now have a sore nose ;)

Soooo current status is the insert detection is working....just need to sort the removal one now :)


Cheers for your help :)


Mike

Mikeoxlarge 02-11-2016 03:31 PM

Soooo we have progress....kind of, I have a script that I have now set to +x and it will run manually and do everything I want it to do when I fire the script from a command line however when I add it to the udev rules it doesnt seem to do anything at all now (I did get it to create a folder) what would cause it to be able to run fine from a command line but not from the udev rule?.

cheers

Mike

malekmustaq 02-12-2016 09:52 AM

Try check your dev rules.
Attributes?
Events?

Congratulation.

Mikeoxlarge 02-12-2016 10:04 AM

getting closer
 
I have got this working on the box in question and have tried adding my scripts to this rule, on it's own it works perfectly and mounts the USB stick in the media folder in a folder it creates with the same name as the usb stick label, yet when I add my scripts to this they still dont "fire"


Code:

KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end"
# Import FS infos
IMPORT{program}="/sbin/blkid -o udev -p %N"
# Get a label if present, otherwise specify one
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"
# Global mount options
ACTION=="add", ENV{mount_options}="relatime"
# Filesystem-specific mount options
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"
# Mount the device
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}"
# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"
# Exit
LABEL="media_by_label_auto_mount_end"

I assumed it would be a simple matter of adding my scripts to the end of the "mount the device" line and the end of the "clean up after removal" line but nooooooo it's not that simple, so I have a script /etc/blah/blah.sh that I would like to run when the drive has mounted, where would I add it in here and what permissions would it need (it already has +x)

Cheers

Mike


All times are GMT -5. The time now is 10:11 PM.