X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=docs%2Fwriting_udev_rules%2Findex.html;h=c2ccdc3663f1878a1bbc5fe9bbf8295904057f2e;hb=14e31b3502b5748068e9367cf4a0d42d9e6cfbf2;hp=1a2702aeaf59a413abbd6eccf0d8a29c18329f96;hpb=f2bd44417bf2c6c99037b430f3f97cfd65a21f76;p=elogind.git
diff --git a/docs/writing_udev_rules/index.html b/docs/writing_udev_rules/index.html
index 1a2702aea..c2ccdc366 100644
--- a/docs/writing_udev_rules/index.html
+++ b/docs/writing_udev_rules/index.html
@@ -8,7 +8,7 @@
- BUS - match the bus type of the device.
- KERNEL - match the kernel device name.
+- DRIVER - match the name of the kernel driver.
+- SUBSYSTEM - match the kernel subsystem name.
- ID - match the device number on the bus (e.g. PCI bus ID).
- PLACE - match the physical position where the device is plugged into (useful for USB).
@@ -193,7 +200,7 @@ The valid keys are:
The ID and PLACE keys do have their uses, but they are not commonly used in rules. This document focuses on using BUS and KERNEL keys, as well as SYSFS{...} keys (detailed in the next section). I will show how to use these keys by example.
-For extra flexibility, udev also provides keys to call external scripts and examine their result. This is out of scope of this document. Look at the udev man page for more details.
+For extra flexibility, udev also provides keys to call external scripts and examine their result, and to examine environment variables. This is out of scope of this document. Look at the udev man page for more details.
Identifying devices through SYSFS files
@@ -219,9 +226,11 @@ You may find that finding the correct place in /sys to run udevinfo on is
/block/sda
-The output of the command (shown above) is telling me that the sysfs path to start at is /sys/block/sda. I would now run "udevinfo -a -p /sys/block/sda". These two commands can be stringed together, like so:
+The output of the command (shown above) is telling me that the sysfs path to start at is /sys/block/sda. I would now run "udevinfo -a -p /sys/block/sda". These two commands can be chained together, like so:
+
+# udevinfo -a -p $(udevinfo -q path -n /dev/sda)
-# udevinfo -a -p `udevinfo -q path -n /dev/sda`
+Sidenote: You may notice that we previously provided full paths (/sys/some/path) to udevinfo beforehand, but now we are providing sysfs-relative paths (/some/path) by chaining these commands. This does not matter - both types of path are accepted.
Moving on to rule-writing, some snipped output of the results of my "udevinfo -a -p /sys/block/sda" command is shown below, with colour added.
@@ -271,7 +280,50 @@ You will notice that a lot of information is not relevant for writing basic rule
I will show three examples of this rule writing based on udevinfo output process below. I will then attempt to list some device-dependant tips and tricks for locating the correct info.
-A reader wrote to me and informed me that he found KDE's control centre useful for writing rules. Apparently, information about USB devices (and others) can be found in the "Info Centre" section of the KDE Control Centre. This interface shows information such as serial number, vendor ID, etc. If you prefer a GUI-like approach, you might want to investigate this.
+A reader wrote to me and informed me that he found KDE's control centre useful for writing rules. Apparently, information about USB devices (and others) can be found in the "Info Centre" section of the KDE Control Centre. This interface shows information such as serial number, vendor ID, etc. If you prefer a GUI-like approach, you might want to investigate this.
+
+The current releases of gnome-volume-manager are unable to treat symlink-nodes as real devices. Conversely as described above, you may wish to specify your own naming in the NAME parameter and specify %k in the SYMLINK parameter.
+
+The behaviour of your own rules masking the defaults can be overcome if you write multiple-SYMLINK style rules.
+
+
+
+
+Using multiple SYMLINK style rules
+Another recent feature is the ability to write rules that do not specify a NAME, but instead they simply specify SYMLINK keys. This allows you to avoid the issue where your own rules effectively mask the udev defaults.
+
+Take the rule:
+KERNEL="hdc", SYMLINK="dvd"
+
+When udev finds this rule, it will take a mental note of it. Upon finding another rule matching the same device which also includes a NAME parameter, udev will create the node as specified by the NAME parameter, plus symbolic links as specified by the SYMLINK parameters of both rules.
+To put it into practical terms, when udev is naming nodes for my hdc device, it will use the default rules for block devices as usual, with the addition of my personal symlink "dvd".
+
+Similarly to normal rules, rules of this type will only take effect if udev is able to find them before it finds a rule specifying a NAME parameter.
+
+
+Controlling ownership and permissions
+
+As well as controlling the naming of the device nodes which are created, udev rules also allow you to control ownership and permission attributes on that device node.
+
+The GROUP key allows you to define which unix group should own the device node. Here's an example from the udev defaults, which defines that the video group will own framebuffer (fb) devices:
+
+KERNEL="fb[0-9]*", NAME="fb/%n", SYMLINK="%k", GROUP="video"
+
+The OWNER key, perhaps less useful, allows you to define which unix user should own the device node. Assuming the slightly odd situation where you would want "john" to own your floppy devices, you could use:
+
+KERNEL="fd[0-9]*", OWNER="john"
+
+You'll notice in the above rule that we didn't specify any NAME or SYMLINK keys. This is similar to the multiple symlink style where udev will take a mental note that we want john to own floppy nodes, and will apply that ownership once it finds a rule which defines a NAME for the floppy device nodes.
+
+Building on the style mentioned above, you can do even more flashy things. The udev defaults use the following rule to define that all the sound device nodes shall be owned by the "audio" group:
+
+SUBSYSTEM="sound", GROUP="audio"
+
+This prevents the need to excessively provide a GROUP="audio" key on every following rule which names sound devices.
+
+udev defaults to creating nodes with unix permissions of 0660 (read/write to owner and group). There may be some situations where you do not want to use the default permissions on your device node. Fortunately, you can easily override the permissions in your rules using the MODE assignment key. As an example, the following rule defines that the inotify node shall be readable and writable to everyone:
+
+KERNEL="inotify", NAME="misc/%k", SYMLINK="%k", MODE="0666"
Example: Writing a rule for my USB printer
@@ -301,22 +353,17 @@ And my printer nodes exist at /dev/lp0 (or /dev/lp1 if another pri
Quick Intro: My camera identifies itself as an external SCSI hard disk (it uses the usb-storage driver which is also used by devices such as USB hard disks and flash-card readers). I can then mount the partition on that disk and copy images over. Not all cameras work like this - many require external software (e.g. gphoto2) to be able to access photos.
-This one is a bit tricky. Two nodes are created by default when my camera is connected : /dev/sda and /dev/sda1. sda1 is the node that I would like as my /dev/camera, as that is what gets mounted. The problem is that there are only small details which can be used as udev rules to show the difference between sda and sda1.
+This one is a bit tricky. Several nodes are created by default when my camera is connected : /dev/sda and /dev/sda1, and possibly even /dev/sg1. This is an example where specifity is important - if your rule is not specific enough, it could match any of the above 3 nodes.
-As these nodes (sda, sda1) are treated as block devices, looking in /sys/block would be a good place to start.
+sda1 is the node that I would like as my /dev/camera, as that is what gets mounted. udevinfo did not point out any useful differences between sda, sda1, and sg1. I decided that a reliable way to differentiate between these 3 nodes would be to look at the KERNEL name.
-In my /sys/block, I have a directory named sda. In my /sys/block/sda, I have a directory named sda1. Both of these directories have dev files in, so they are OK for udev rules. Running the following dumps a lot of information about my camera and the USB ports it is connected through.
+A key such as KERNEL="sd?1" would match KERNEL names such as "sda1", "sdb1", "sdc1", and equally importantly, it will not match KERNEL names such as sda, sdb, or sg1. The purpose of this key is to ignore the /dev/sda and /dev/sg1 nodes. The device is a digital camera - I would not dream of fdisking it or anything like that, so these 2 nodes are pretty useless to me. The key attempts to capture the /dev/sda1 node, which is mountable and therefore useful!
-
-# udevinfo -a -p /sys/block/sda
-# udevinfo -a -p /sys/block/sda/sda1
-
-
-The output of both of these command was almost identical, so that does not help in writing keys to differentiate between sda and sda1.
+As this node (sda1) is treated as a block device, looking in /sys/block would be a good place to start.
-To differentiate between sda and sda1, I decided that matching KERNEL names would be most appropriate here. Some examples of KERNEL names for this type of device are: sda, sda1, sdb, sdb1, sdc, ...
+In my /sys/block, I have a directory named sda. In my /sys/block/sda, I have a directory named sda1. Both of these directories have dev files in, so they are OK to run udevinfo on. Running the following dumps a lot of information about my camera and the USB port it is connected through.
-udev's support for wildcards in key expressions comes in handy here. A key such as KERNEL="sd?1" would match KERNEL names such as "sda1", "sdb1", "sdc1", and equally importantly, it will not match KERNEL names such as sda, sdb (because the name must have a "1" on the end). The purpose of this key is to ignore the /dev/sda node. The device is a digital camera - I would not dream of fdisking it or anything like that, so this node is pretty useless to me. The key attempts to capture the /dev/sda1 node, which is mountable and therefore useful!
+# udevinfo -a -p /sys/block/sda/sda1
In the udevinfo output, I also noticed this bit of useful and understandable information:
SYSFS{product}="USB 2.0M DSC"
@@ -324,7 +371,7 @@ In the udevinfo output, I also noticed this bit of useful and understandable inf
So that gives me my rule. For completeness, I also include a BUS key (this was also found in the udevinfo output).
BUS="usb", SYSFS{product}="USB 2.0M DSC", KERNEL="sd?1", NAME="%k", SYMLINK="camera"
-Now, when my camera is plugged in, it will be named /dev/sda1 (or, if sda1 isnt available, it might be called /dev/sdb1) and will always be correctly linked to from /dev/camera. The /dev/sda (or sdb) node still appears as normal, but the important thing is that my custom persistant "camera" symlink points to the mountable partition.
+Now, when my camera is plugged in, it will be named /dev/sda1 (or, if sda1 isnt available, it might be called /dev/sdb1) and will always be correctly linked to from /dev/camera. The /dev/sda (or sdb) node still appears as normal, but the important thing is that my custom persistent "camera" symlink points to the mountable partition.
Additional notes on writing rules for USB storage
@@ -349,7 +396,7 @@ This problem also applies to other USB disks - e.g. if you create a new partitio
udev provides a solution here - it is able to create nodes for all partitions of a block device. For every rule that you specify, the block device will have all 16 partition nodes created. To achieve this, you can simply modify the NAME key, as shown below:
-BUS="usb", SYSFS{product}="USB 2.0 Storage Device", NAME{all_partitions}="usbhd%n"
+BUS="usb", SYSFS{product}="USB 2.0 Storage Device", NAME{all_partitions}="usbhd"
You will now have nodes named: usbhd, usbhd1, usbhd2, usbhd3, ..., usbhd15.
@@ -359,13 +406,42 @@ I have two CD drives in my PC - a DVD reader, and a CD rewriter. My DVD is hdc a
Still, some people (myself included) like to have nodes such as /dev/dvd and /dev/cdrw for convenience. Since we know the "hdX" values for these drives, writing rules is simple. The examples below should be self explanatory.
-
BUS="ide", KERNEL="hdc", NAME="%k", SYMLINK="dvd cdroms/cdrom%n"
BUS="ide", KERNEL="hdd", NAME="%k", SYMLINK="cdrw cdroms/cdrom%n"
-You may have noticed that the default udev.rules file contains a rule which runs a script to produces names for block devices. Do not be confused by this - as usual, because your own rules are located at the top of the rules file, they are processed before the default rules, so the default rules will not be used when naming the hardware you have written rules for.
+You may have noticed that the default 50-udev.rules file contains a rule which runs a script to produces names for block devices. Do not be confused by this - as usual, because your own rules are located in a file which is processed before the default rules, the defaults will not be used when naming the hardware you have written rules for.
+
+
+Example: Writing a rule for your USB Visor Palm Pilot
+
+These devices work as USB-serial devices, so by default, you only get the ttyUSB1 node. The user-space palm utilities rely on /dev/pilot, so you need to use a rule to create this. The following rule will do the job:
+
+BUS="usb", SYSFS{product}="Palm Handheld", KERNEL="ttyUSB*", SYMLINK="pilot"
+
+This was adapted from Carsten Clasohm's blog entry, which includes a useful discussion of the situation. You may also wish to add ownership and permission keys to the rule to suit your setup.
+
+
+Example: Writing a rule to name my network interface
+An interesting new feature in recent udev versions is the ability to rename your network interfaces, like the nameif utility does. Network interfaces do not show up in /dev, but they are generally referenced by names (e.g. with ifconfig). Despite the differences, the rule writing process is almost identical.
+
+As usual, udevinfo comes to our aid in rule-writing. In my example, I wish to rename my "eth0" network device (the following output is snipped):
+
+
+# udevinfo -a -p /sys/class/net/eth0/
+ looking at class device '/sys/class/net/eth0':
+ SYSFS{address}="00:52:8b:d5:04:48"
+
+
+Every network adapter has its own unique MAC-address, so I chose to use this when writing my rule. This will not change, unless you change your network card. There is one caveat here: make sure you use the MAC address you obtain from udevinfo (as above), because it is case sensitive. Be careful when using utilities such as ifconfig as they will capitalize the letters.
+
+An example rule is shown below:
+
+KERNEL="eth*", SYSFS{address}="00:52:8b:d5:04:48", NAME="lan"
+
+You will need to reload the net driver for this rule to take effect. You can either unload and reload the module, or simply reboot the system. You will also need to reconfigure your system to use "lan" rather than "eth0". I had some troubles getting this going (the interface wasn't being renamed) until I had completely dropped all references to eth0.
+After that, you should be able to use "lan" instead of "eth0" in any calls to ifconfig or similar utilities.
Tips for finding the appropriate places in SYSFS
@@ -383,48 +459,31 @@ BUS="ide", KERNEL="hdd", NAME="%k", SYMLINK="cdrw cdroms/cdrom%n"
- Remember that unfortunately, the kernel does not export information for all devices into sysfs, meaning that you simply can't write rules for some devices yet. On 20/02/04, the udev author stated that there are 162 drivers left to convert to sysfs.
-# udevtest /sys/class/sound/dsp/
+version 056
+looking at '/class/sound/dsp/'
+opened class_dev->name='dsp'
+configured rule in '/etc/udev/rules.d/50-udev.rules[132]' applied, added symlink '%k'
+configured rule in '/etc/udev/rules.d/50-udev.rules[132]' applied, 'dsp' becomes 'sound/%k'
+creating device node '/dev/sound/dsp', major = '14', minor = '3', mode = '0660', uid = '0', gid = '18'
-Another solution is to simply create the nvidia specific nodes on bootup. X will then load the module when required, and as the nodes are already in existance, you will not run into the problem described above. Place these commands in a file that is automatically executed on bootup (e.g.