Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing arguments to shell script from udev rules file

In the rules file a script is executed by passing the arguments "LABEL" and "DEVNAME" for mounting

ACTION=="add", RUN+="/appmount/scripts/usb_mount.sh %E{ID_FS_LABEL} %E{DEVNAME}"

In the usb_mount.sh file printing the arguments value as

echo "LABEL: $1 DEVNAME: $2" # this does not work reliably

Some of the devices have empty LABEL field and hence the DEVNAME is printed as the label. In the bash script we can pass the args in double quotes and it will work even if the args are null.

What is the equivalent of the same for passing args to udev rules?

The workaround to this problem might be to switch the order of the arguments. Is there any reliable way?

like image 527
Talespin_Kit Avatar asked Dec 04 '12 09:12

Talespin_Kit


People also ask

Where do udev rules go?

RULES FILES. The udev rules are read from the files located in the system rules directory /lib/udev/rules. d, the volatile runtime directory /run/udev/rules. d and the local administration directory /etc/udev/rules.

How do you define udev rules?

A udev rule must contain one attribute from one single parent device. Parent attributes are things that describe a device from the most basic level, such as it's something that has been plugged into a physical port or it is something with a size or this is a removable device.

What is udev command used for?

As the successor of devfsd and hotplug, udev primarily manages device nodes in the /dev directory. At the same time, udev also handles all user space events raised when hardware devices are added into the system or removed from it, including firmware loading as required by certain devices.


2 Answers

You should be able to use single quotes instead of the double quotes you mentioned:

ACTION=="add", RUN+="/appmount/scripts/usb_mount.sh '%E{ID_FS_LABEL}' '%E{DEVNAME}'"

Beware: I didn't test this. Maybe variable substitution will fail within single quotes...

Quoting from man udev about the key "RUN":

The program name and following arguments are separated by spaces. Single quotes can be used to specify arguments with spaces.

like image 136
Carsten Scholtes Avatar answered Oct 12 '22 05:10

Carsten Scholtes


Just set and query udev's env, here an example to check if system is running VMWare :

cat /etc/udev/rules.d/99-vmware.rules
KERNEL=="id", SUBSYSTEM=="dmi", ATTR{sys_vendor}=="VMware, Inc.", ENV{VMWARE}='defined'

udevadm info -e  | grep 'VMWARE='
E: VMWARE=defined
like image 21
RzR Avatar answered Oct 12 '22 05:10

RzR