Udev: Difference between revisions
Jump to navigation
Jump to search
(Created page with '== Reference == * udev man page (<code>man udev</code>) * == Writing udev rules == * Rules are contained in files located in <tt>/etc/udev/rules</tt>. Files must have <tt>.rule…') |
|||
Line 3: | Line 3: | ||
* |
* |
||
== |
== udev rules == |
||
* Rules are contained in files located in <tt>/etc/udev/rules</tt>. Files must have <tt>.rules</tt> extensions, and are processed in lexical order. Files usually starts with 2 figures (like <tt>10-local.rules</tt>) |
* Rules are contained in files located in <tt>/etc/udev/rules</tt>. Files must have <tt>.rules</tt> extensions, and are processed in lexical order. Files usually starts with 2 figures (like <tt>10-local.rules</tt>) |
||
=== Examples === |
|||
This basic rule simply changes group of all usb hub devices to ''plugdev''. |
|||
<source lang=bash> |
|||
# Basic rule - set group 'plugdev' for all USB HUB |
|||
#SUBSYSTEMS=="usb", ATTR{bDeviceClass}=="09", MODE="660", GROUP="plugdev" |
|||
</source> |
|||
This rule is more advanced. Whenever a specific usb device (here, an usb serial device) is inserted, it changes the group of the hub device the first device is attached to. |
|||
<source lang=bash> |
|||
# More advanced rule, only set 'plugdev' grp for hub that have an FTDI board connected to them |
|||
# SUBSYSTEM=="tty", DRIVERS=="ftdi_sio", RUN+="/etc/udev/rules.d/plugdev_hub.sh" |
|||
# |
|||
# Even better, this rules also detects board removal, which allow to reset grp to 'root' |
|||
SUBSYSTEM=="tty", KERNEL=="ttyUSB*", RUN+="/etc/udev/rules.d/plugdev_hub.sh" |
|||
</source> |
|||
File <tt>plugdev_hub.sh</tt>: |
|||
<source lang=bash> |
|||
#! /bin/bash |
|||
# Set group to 'plugdev' for given hub device |
|||
function setgrp() |
|||
{ |
|||
DEVNAME=/dev/$(udevadm info -q name -p $1) |
|||
chgrp plugdev $DEVNAME |
|||
} |
|||
# Reset group to 'root' if no ttyUSB device found for given hub |
|||
function resetgrp() |
|||
{ |
|||
TTYDEVS=$(find $1 -lname "*/ttyUSB*") |
|||
if [ -z "$TTYDEVS" ]; then |
|||
DEVNAME=/dev/$(udevadm info -q name -p $1) |
|||
chgrp root $DEVNAME |
|||
fi |
|||
} |
|||
if [ "$ACTION" = "add" -o "$ACTION" = "remove" ]; then |
|||
F=/sys$DEVPATH |
|||
while [ "$F" != "/sys" ] ; do |
|||
# if grep "09" "$F/bDeviceClass"; then |
|||
if [ -a "$F/bDeviceClass" ] ; then |
|||
if [ "$(cat $F/bDeviceClass)" == "09" ] ; then |
|||
if [ "$ACTION" = "add" ]; then |
|||
setgrp $F # add |
|||
else |
|||
resetgrp $F # remove |
|||
fi |
|||
break |
|||
fi |
|||
fi |
|||
F=$(dirname $F) |
|||
done |
|||
fi |
|||
</source> |
|||
'''Detecting device removal''' |
|||
Note that many attributes are no longer available during udev rules processing when a device is removed. To know which variables are still available in the environment, try |
|||
<source lang=bash> |
|||
udevadm monitor --property |
|||
</source> |
|||
== Troubleshooting == |
== Troubleshooting == |
Revision as of 07:57, 17 June 2013
Reference
- udev man page (
man udev
)
udev rules
- Rules are contained in files located in /etc/udev/rules. Files must have .rules extensions, and are processed in lexical order. Files usually starts with 2 figures (like 10-local.rules)
Examples
This basic rule simply changes group of all usb hub devices to plugdev.
# Basic rule - set group 'plugdev' for all USB HUB
#SUBSYSTEMS=="usb", ATTR{bDeviceClass}=="09", MODE="660", GROUP="plugdev"
This rule is more advanced. Whenever a specific usb device (here, an usb serial device) is inserted, it changes the group of the hub device the first device is attached to.
# More advanced rule, only set 'plugdev' grp for hub that have an FTDI board connected to them
# SUBSYSTEM=="tty", DRIVERS=="ftdi_sio", RUN+="/etc/udev/rules.d/plugdev_hub.sh"
#
# Even better, this rules also detects board removal, which allow to reset grp to 'root'
SUBSYSTEM=="tty", KERNEL=="ttyUSB*", RUN+="/etc/udev/rules.d/plugdev_hub.sh"
File plugdev_hub.sh:
#! /bin/bash
# Set group to 'plugdev' for given hub device
function setgrp()
{
DEVNAME=/dev/$(udevadm info -q name -p $1)
chgrp plugdev $DEVNAME
}
# Reset group to 'root' if no ttyUSB device found for given hub
function resetgrp()
{
TTYDEVS=$(find $1 -lname "*/ttyUSB*")
if [ -z "$TTYDEVS" ]; then
DEVNAME=/dev/$(udevadm info -q name -p $1)
chgrp root $DEVNAME
fi
}
if [ "$ACTION" = "add" -o "$ACTION" = "remove" ]; then
F=/sys$DEVPATH
while [ "$F" != "/sys" ] ; do
# if grep "09" "$F/bDeviceClass"; then
if [ -a "$F/bDeviceClass" ] ; then
if [ "$(cat $F/bDeviceClass)" == "09" ] ; then
if [ "$ACTION" = "add" ]; then
setgrp $F # add
else
resetgrp $F # remove
fi
break
fi
fi
F=$(dirname $F)
done
fi
Detecting device removal
Note that many attributes are no longer available during udev rules processing when a device is removed. To know which variables are still available in the environment, try
udevadm monitor --property
Troubleshooting
udevadm monitor