You are not logged in.

#1 2005-07-16 23:08:47

lanrat
Member
From: Poland
Registered: 2003-10-28
Posts: 1,274

custom udev rules + converting hotplug scripts

FYI
After recent udev upgrade I've decided to simplify my custom udev rules.
Currently I've tested some rules for my dvd burner, usb printer, usb hd (via ide2usb connector) and canon ixus v3 camera.

The good thing about newer udev versions is that they process all rules for a given device. This allows you to insert your rules before standard udev.rules and to modify it after.
I created two files to do this:
/etc/udev/rules.d/010.udev.rules (which is exectued before standard udev.rules)

BUS=="ide", KERNEL=="hdc", SYSFS{removable}=="1", PROGRAM="/bin/cat /proc/ide/%k/media", RESULT=="cdrom*", NAME="%k", GROUP="users", SYMLINK+="nagrywarka dvd cdrw", OPTIONS="last_rule"
BUS=="usb", KERNEL=="lp[0-9]*", SYSFS{serial}=="CN16J1Q3HWSX", NAME="usb/%k", GROUP="users", SYMLINK+="drukarka_hp_845c drukarka", OPTIONS="last_rule"
KERNEL=="sd[a-z]*", SYSFS{scsi_level}=="3", SYSFS{type}=="0", NAME="%k", SYMLINK+="usbhd%n", GROUP="users", OPTIONS="last_rule"
KERNEL=="nvidia*", GROUP="users", OPTIONS="last_rule"
ACTION=="add", SYSFS{idVendor}=="04a9", SYSFS{idProduct}=="3070", RUN+="/bin/chmod o-rwx $env{DEVICE}"
ACTION=="add", SYSFS{idVendor}=="04a9", SYSFS{idProduct}=="3070", RUN+="/bin/chgrp users $env{DEVICE}"
ACTION=="add", SYSFS{idVendor}=="04a9", SYSFS{idProduct}=="3070", RUN+="/bin/chmod g+rw $env{DEVICE}", OPTIONS="last_rule"

The important thing is the OPTIONS="last_rule" field. This prevents overwriting values later in udev.rules file.

Last 3 rules are converted from my /etc/hotplug/usb/usbcam script (it seems this is the future as udev RELEASE-NOTES suggests). These are for my canon ixus v3 working in "normal" mode (not as usb storage device). I like this mode. It works great with digikam but the problem is there is no /dev node created. So you can't just use GROUP field.
First you need to analyze usbcam script to find out what it does.

if [ "${ACTION}" = "add" ] && [ -f "${DEVICE}" ]
then
    logger "${DEVICE}  ${ACTION}  ${GROUP}  ${PRODUCT}"
    chmod o-rwx "${DEVICE}"
    chgrp "${GROUP}" "${DEVICE}"
    chmod g+rw "${DEVICE}"
f

I added logger line to print DEVICE path in /var/log/everything.log. It turns out that this script changes permissions for /proc/bus/usb/004/004  (numbers depend on where you plug your usb device).
Fortunatelly udev can use hotplug enviroment variables too. Now you need to identify your device in /sys. You can use /etc/hotplug/usb.usermap (the values for fields idVendor and idProduct without 0x prefix). You might need to divide your rules into a few separate rules (like in my case). Last rule also has OPTIONS="last_rule" field to ensure it will not change later.
The advantage is that this method works much faster and consumes almost no cpu as opposed to hotplug script method which runs for 1-2 sec and consumes 100% cpu.

/etc/udev/rules.d/x10.udev.rules (executed after standard udev.rules)

SUBSYSTEM=="video4linux", GROUP="users"
SUBSYSTEM=="sound", GROUP="users"
SUBSYSTEM=="printer", GROUP="users"

KERNEL=="fb[0-9]*", GROUP="users", OPTIONS="last_rule"
KERNEL=="rtc", GROUP="users", OPTIONS="last_rule"
KERNEL=="agpgart", GROUP="users", OPTIONS="last_rule"

I think this is pretty selfexplanatory. It changes (overwrites) group permissions to users group. Notice that these are just a little modified rules from the standard file. NAME filed must be removed (only one rule with NAME filed is valid). You can also add more symlinks with SYMLINK+="mysymlink1 mysymlink1" etc. etc. If you want to reset standard values you can use SYMLINK="".

These are of course just simple examples how you can modify udev.rules.
I hope this will help users converting from devfs/hotplug scripts.

BTW according to udev manpage the following rule

# dm devices (ignore them)
KERNEL=="dm-[0-9]*", NAME=""

could be rewritten using OPTIONS="ignore_device".

Offline

Board footer

Powered by FluxBB