分类:
2008-06-13 12:47:25
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 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 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: 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:
Now try scanimage -L as an ordinary user: If it doesn't find your scanner, run it as root: 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: 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: In 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: 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 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: 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. Creating Persistent Device Names There are two ways to give additional names to devices. One is to add another name, another way is to create symlinks. For example, my second hard drive, /dev/sda, is a mondo 300-gigabyte SATA drive that I use for storing photo archives. I can create a 10-local.rules file with this entry: After reboot there will be a /dev/photo-archives. I can use this like any storage drive- make an entry in /etc/fstab using the /dev/photo-archives name, mount it manually, whatever I like. While this is fast and easy, it's better to create a symlink, because symlinks point back to the original device node. Names do not, so I'll have to search /etc/udev to find out what it really is, because I will forget. Run udevinfo to see the drive information: # nickname for /dev/sda KERNEL=="sda", SYSFS{size}=="156301488", SYMLINK+="photo-archives" If you use more than one sysfs attribute, make sure they come from the same section of the udevinfo output, or it won't work. Mind your punctuation- the KERNEL and SYSFS values are called match keys, and must use the equality operator or ==. SYMLINK is an assignment key. The plus sign means "add this symlink to the list of symlinks", and = means "assign this value". If you leave off the plus sign, you'll overwrite the existing symlinks. I added the size for insurance, as it's the only drive of that size on the system. If either of the match keys change the rule will not work. Reboot and, hurrah, the new symlink shows up just like it's supposed to: Nailing Down USB Network Interfaces You may use /sbin/ifconfig -a to obtain MAC addresses and kernel names, or udevinfo: Reloading Udev Changes Resources
|