Manage Linux Hardware with udev (Part 2)

Getting around Linux device management can be tricky. Here's the second part of our guide to untangling the mysteries of udev. This week we look at how to connect devices to your system.

 By Carla Schroder | Posted Apr 15, 2008
Page 1 of 2
Print ArticleEmail Article

Last week we learned the basics of the udev filesystem, and how to dig up device names. Today we dive into writing custom udev rules. Why would you want to acquire this strange knowledge? Because, believe it or not, computers are not yet perfect, so sometimes we must fix them. USB scanners are notorious examples of needing human intervention to be usable by non-root users. Managing device permissions is something you're bound to bump into sooner or later – the traditional Unix method of user and group permissions no longer works on udev-managed devices. USB devices are moving targets that take on different names if you don't nail them down. If you want to give them friendly, memorable names, you'll need to know how to configure udev.

Getting Scanners To Work Right

Scanners and multi-function printer/copier/scanner/faxes are notorious for not being automatically usable on Linux. So let's take a stroll down Making Them Work Lane. In these examples we'll use the Epson Stylus CX4800. (Not because it is a great machine, but because it's what I happen to have. It runs through ink at astonishing speeds when I can get it to print at all, because it uses special inks that clot like blood and clog the print nozzles. Then it cleans the nozzles with ink. Brilliant, eh?)

First of all, is your scanner/multi-function thingy even supported in Linux? If it isn't, even udev does not contain enough magic to make it work. Check out Sane-project.org and Google before you buy one.

Take a peek in your /etc/udev/rules.d/*-libsane-rules file. Naturally, the different distributions have to scatter all over the udev landscape like a herd of cats and do things their in their own weird ways. On Kubuntu, it's 45-libsane.rules. On Fedora, 60-libsane.rules. The number is the priority- lower numbered-files are parsed first. This is an example of a stock *-libsane-rules file:

SUBSYSTEM!="usb_device", ACTION!="add", GOTO="libsane_rules_end"# Hewlett-Packard|ScanJet 4100C
SYSFS{idVendor}=="03f0", SYSFS{idProduct}=="0101", MODE="664", GROUP="scanner"
# Hewlett-Packard|PhotoSmart S20
SYSFS{idVendor}=="03f0", SYSFS{idProduct}=="0102", MODE="664", GROUP="scanner"
# Hewlett-Packard|ScanJet 4200C

It seems rather ironic that udev makes a big deal about slimming down /dev, then stuffs files like this onto your system with all manner of irrelevant entries. Oh well, I'm just an old country sysadmin, so what do I know? Anyway it's easy enough to delete the irrelevant entries.

Make sure you have SANE installed, including the documentation, backends and utilities. Have your scanner powered up and connected to your computer. Then run this command:

# sane-find-scanner
  # sane-find-scanner will now attempt to detect your scanner
found USB scanner (vendor=0x04b8, product=0x0819) at libusb:003:002
  # Your USB scanner was (probably) detected. It may or may not be supported by
  # SANE. Try scanimage -L and read the backend's manpage '...'

Now try scanimage -L as an ordinary user:

$ scanimage -L

If it doesn't find your scanner, run it as root:

# scanimage -L
device `epson:libusb:003:002' is a Epson Unknown model flatbed scanner

This tells you there is problem with the scanner permissions. No problem, we can fix that. First edit /etc/sane.d/epson.conf to configure the backend correctly. Comment out all lines except these two:

usb 0x4b8 0x0819

Where did those nice hexadecimal numbers come from? From running the sane-find-scanner command. Make sure you get the numbers in the correct order. Next, add these lines to /etc/udev/rules.d/*-libsane-rules:

#epson stylus cx4800
SYSFS{idVendor}=="04b8", SYSFS{idProduct}=="0819", MODE="664", GROUP="scanner"
Pretty much just copy-and-paste, following the examples of the other rules in the file. Note that you can drop leading zeroes.

In part 1 we learned how to search /sys for unique IDs to use in our rules. This is what the Epson looks like, with the uninteresting bits snipped:

 $ udevinfo -a -p /class/usb_device/usbdev3.2 follow the "device"-link to the physical device:
  looking at the device chain at '/sys/devices/pci0000:00/0000:00:02.2/usb3/3-6':
    SYSFS{configuration}=="USB2.0 MFP_Hi-Speed_"
    SYSFS{product}=="USB2.0 MFP_Hi-Speed_"
    SYSFS{version}==" 2.00"

It's all there when you know where to look. Save your changes and reboot, and voilá! No more do you need to run as root to use your scanner.

SANE comes with a bale of man pages which are full of interesting deep scanner voodoo. man sane lists all of them.

Permissions and Ownership, Names, and Symlinks

udev configurations fall into three general categories: permissions and ownership, names, and symlinks. Kubuntu organizes these neatly into three different files: 20-names.rules, 40-permissions.rules, and 60-symlinks.rules. In Fedora they're not separated by those categories, as you can see by studying 50-udev.rules, which has all three. You may edit the existing files, or put all of your modifications in a separate file like 10-local.rules.

If you need to change device permissions or ownership, you'll need to troll through /etc/udev/rules.d to make sure you find all the pertinent rules. A quick way to find things is with the grep command, like this example that looks for devices assigned to the disk group:

$ grep -ir 'group="disk"' /etc/udev
/etc/udev/rules.d/40-permissions.rules:SYSFS{removable}!="1",     GROUP="disk"
/etc/udev/rules.d/40-permissions.rules:KERNEL=="raw1394",   GROUP="disk"

It even tells you the filename. You may also view the previous five lines with the -B 5 option, and the following five lines with the -A 5, or whatever number of lines you want to get the whole context.

Comment and Contribute
(Maximum characters: 1200). You have
characters left.
Get the Latest Scoop with Enterprise Networking Planet Newsletter

By submitting your information, you agree that enterprisenetworkingplanet.com may send you ENTERPRISENetworkingPLANET offers via email, phone and text message, as well as email offers about other products and services that ENTERPRISENetworkingPLANET believes may be of interest to you. ENTERPRISENetworkingPLANET will process your information in accordance with the Quinstreet Privacy Policy.