Running a script via udev

Hey Guys, new to the forum and straight in with a question.

Basically I’m trying to run a script depending on when a particular type of USB drive is plugged in, so far I have the script running on other distro’s (ubuntu, debian) and it seems to work fine there so it’s probably something to do with my rule syntax. I currently have a rule un udev rules that goes as follows

ACTION==“add”, SUBSYSTEM==“usb”, ATTRS{idVendor}==“18a5”, ATTRS{idProduct}==“0302”, RUN+="/etc/scripts/scriptstart.sh"

This script just contains a sudo mount command to mount the USB drive to a particular folder in /mnt/ it works absoloutely fine in other OS’s but doesn’t seem to do anything in osmc, I have asked in the IRC channel but have been advised to post here by @sam_nazarko.

I’ll admit I’m a bit of a noob so go easy on me lol

Cheers

1 Like

Hi

As suggested in IRC, try adding ACTION=change rule, use udevadm --info to verify your rule, and see Debugging udev rules | under Debian for some pointers.

Op already confirmed scripts are +x.

Let us know how you get on

Sam

This is the output of the test, nothing jumps out at me but as I said I am a bit of a noob lol

osmc@osmc:/etc/udev/rules.d$ udevadm test /dev/bus/001/009
calling: test
version 215
This program is for debugging only, it does not run any program
specified by a RUN key. It may show incorrect results, because
some values may be different, or not available at a simulation run.

load module index
Network interface NamePolicy= disabled on kernel commandline, ignoring.
timestamp of ‘/etc/systemd/network’ changed
Parsed configuration file /lib/systemd/network/99-default.link
Created link configuration context.
timestamp of ‘/etc/udev/rules.d’ changed
read rules file: /lib/udev/rules.d/42-usb-hid-pm.rules
read rules file: /lib/udev/rules.d/50-bluetooth-hci-auto-poweron.rules
read rules file: /lib/udev/rules.d/50-firmware.rules
read rules file: /lib/udev/rules.d/50-udev-default.rules
read rules file: /lib/udev/rules.d/55-dm.rules
read rules file: /lib/udev/rules.d/60-cdrom_id.rules
read rules file: /lib/udev/rules.d/60-drm.rules
read rules file: /lib/udev/rules.d/60-fuse.rules
read rules file: /lib/udev/rules.d/60-gnupg.rules
read rules file: /lib/udev/rules.d/60-keyboard.rules
read rules file: /lib/udev/rules.d/60-persistent-alsa.rules
read rules file: /lib/udev/rules.d/60-persistent-input.rules
read rules file: /lib/udev/rules.d/60-persistent-serial.rules
read rules file: /lib/udev/rules.d/60-persistent-storage-dm.rules
read rules file: /lib/udev/rules.d/60-persistent-storage-tape.rules
read rules file: /lib/udev/rules.d/60-persistent-storage.rules
read rules file: /lib/udev/rules.d/60-persistent-v4l.rules
read rules file: /lib/udev/rules.d/61-accelerometer.rules
read rules file: /lib/udev/rules.d/64-btrfs.rules
read rules file: /lib/udev/rules.d/70-power-switch.rules
read rules file: /lib/udev/rules.d/70-uaccess.rules
read rules file: /lib/udev/rules.d/71-seat.rules
read rules file: /lib/udev/rules.d/73-idrac.rules
read rules file: /lib/udev/rules.d/73-seat-late.rules
read rules file: /lib/udev/rules.d/75-net-description.rules
read rules file: /lib/udev/rules.d/75-probe_mtd.rules
read rules file: /lib/udev/rules.d/75-tty-description.rules
read rules file: /lib/udev/rules.d/78-sound-card.rules
read rules file: /lib/udev/rules.d/80-drivers.rules
read rules file: /lib/udev/rules.d/80-net-setup-link.rules
read rules file: /lib/udev/rules.d/80-networking.rules
read rules file: /lib/udev/rules.d/80-udisks.rules
read rules file: /lib/udev/rules.d/85-hdparm.rules
read rules file: /lib/udev/rules.d/85-hwclock.rules
read rules file: /etc/udev/rules.d/85-my_rule.rules
read rules file: /etc/udev/rules.d/86-my_rule.rules
read rules file: /lib/udev/rules.d/95-udev-late.rules
read rules file: /lib/udev/rules.d/97-hid2hci.rules
read rules file: /lib/udev/rules.d/98-eventlircd-names.rules
read rules file: /lib/udev/rules.d/98-eventlircd.rules
IMPORT found builtin ‘usb_id --export %p’, replacing /lib/udev/rules.d/98-eventlircd.rules:86
read rules file: /lib/udev/rules.d/98-lircd.rules
IMPORT found builtin ‘usb_id --export %p’, replacing /lib/udev/rules.d/98-lircd.rules:42
read rules file: /lib/udev/rules.d/99-systemd.rules
read rules file: /etc/udev/rules.d/996-fix-hdhomerun.rules
read rules file: /etc/udev/rules.d/997-fix-spi.rules
read rules file: /etc/udev/rules.d/998-fix-input.rules
read rules file: /etc/udev/rules.d/999-fix-vchiq.rules
rules contain 49152 bytes tokens (4096 * 12 bytes), 14455 bytes strings
2580 strings (30650 bytes), 1755 de-duplicated (17021 bytes), 826 trie nodes used
unable to open device ‘/sys/dev/bus/001/009’
unload module index
Unloaded link configuration context.
osmc@osmc:/etc/udev/rules.d$

Would I add “action” as part of the existing rule or would it need to be another rule/alternative rule?

thanks

add|change I believe

Sam

Righ, just found this here http://www.axllent.org/docs/view/auto-mounting-usb-storage/ and it works, in fact it works really really well, it mounts the drive in media under the drives label name and makes it accessible, however I tried adding my scripts in there and again…nothing, it’s getting really frustrating now as there’s no logical reason they wont run, Any ideas anyone?

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”

Cheers

Mike

In IRC, I think you referenced ‘openvpn.sh’.

What are you trying to achieve exactly?

If you’re trying to automount disks, they are already made available under /media by OSMC

I’m basically trying to start openvpn when a “key” drive is inserted, the key drive contains the openvpn .ovpn file and I’m trying to get it to automatically run openvpn, I have two scripts already that work manually triggered and they work really well but it’s just make things a lot easier if I could get it to autorun, I’d have a go at making an addon but as yet thats a bit beyond my abilities/knowledge (unless theres a simple way of doing it?)

I guess that might actually be the answer, a small addon that just fires off the script on one button and fires the disconnect script on another, that way you just load a different ovpn file on the usb and plug it in

Just as a general comment, most time when people had issues running a script it was either that not full path (for the script and in the script) being used or it was a permission problem. As a starting point try a script that just echo text to a file

I’ve tried that @fzinken, tbh that was my first issue and the scripts wouldn’t even run for the osmc user let alone system, I can now call the scripts manually ssh’d in as osmc and they work perfectly, Tried stripping back all function in the script to just create a small txt file via touch and it still does bugger all :confused: i’m still leaning towards some kind of permissions issues tbh as theres no other logical reason that the osmc user could run it fine yet udev cant :confused:

Just make sure that your udev rule gets detected correctly in the first place been down this path too so i know what your going thru it not always easy btw mind adding code tags too your udev script above or even use the pastebin for it just type this in terminal.

cat your_udev_rule | paste-log

would make it less messy to read for future help purposes.

ps. it is possible it just time and effort to get it just right :slightly_smiling: