X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=docs%2Fwriting_udev_rules%2Findex.html;h=c2ccdc3663f1878a1bbc5fe9bbf8295904057f2e;hp=0e4d54cfaf61a4c4d7ae2bcb47b54326d525a1d6;hb=7fe082a8a42baa45ef45d82edde4f893410ebeea;hpb=b1ea9f3ef5aad038b4d31cb3e79749de9afb41f8 diff --git a/docs/writing_udev_rules/index.html b/docs/writing_udev_rules/index.html index 0e4d54cfa..c2ccdc366 100644 --- a/docs/writing_udev_rules/index.html +++ b/docs/writing_udev_rules/index.html @@ -1,6 +1,6 @@
-key,[key,...] name [, symlink]+Rules are composed of keys. Keys are seperated by commas. Some keys are used for reading and matching information, others are used for assigning information and performing actions.
-The keys here are the BUS and SYSFS{serial} parameters. udev will match this rule against a device that is connected through the USB bus and with a serial number of HXOLL0012202323480. Note that all (as opposed to any) specified keys must be matched for udev to use the rule to name a device.BUS="usb", SYSFS{serial}="HXOLL0012202323480", NAME="lp_epson", SYMLINK="printers/epson_stylus"
+# udevstart
@@ -164,7 +172,7 @@ This rule says:KERNEL="ts*", NAME="input/%k"
Match a device identified by a KERNEL name starting with the letters "fd", followed by any single digit, optionally followed by anything at all. Name the device with the kernel number of the device (%n) under the floppy directory.-You can use these wildcards/regular-expression matches in any type of key, including both basic keys and sysfs-based identification (see below for explanations of these key types).
-Some snipped output of the results of my "udevinfo -a -p /sys/block/sda" command is shown below, with colour added.# udevinfo -a -p /sys/path/to/hardware/info
+ +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 -q path -n /dev/sda + +/block/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.# udevinfo -a -p $(udevinfo -q path -n /dev/sda)
follow the class device's "device" @@ -257,19 +280,58 @@ 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.-You should now be able to get into X with no problems.
-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:
++ +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.KERNEL="hdc", SYMLINK="dvd"
+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: + ++ +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="fb[0-9]*", NAME="fb/%n", SYMLINK="%k", GROUP="video"+ +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.KERNEL="fd[0-9]*", OWNER="john"
+ +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: + ++ +This prevents the need to excessively provide a GROUP="audio" key on every following rule which names sound devices.SUBSYSTEM="sound", GROUP="audio"
+ +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
-After plugging in my printer, I started looking around some /sys directories for a relevant place to start. I didn't get anywhere, but I noticed that my printer had been given device node /dev/lp0. I used this command sequence to find an answer: +After plugging in my printer, I started looking around some /sys directories for a relevant place to start. I didn't get anywhere, but I noticed that my printer had been given device node /dev/lp0. udevinfo was able to provide me with a useful path:Running "udevinfo -a -p /sys/class/usb/lp0" provided me with a heap of info, as usual. I picked out the relevant bits for unique device identification: @@ -291,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.-# cd /sys -# find | grep lp0 -./class/usb/lp0 -./class/usb/lp0/dev -./class/usb/lp0/driver -./class/usb/lp0/device +# udevinfo -q path -n /dev/lp0 +/class/usb/lp0
-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.
- -As these nodes (sda, sda1) are treated as block devices, looking in /sys/block would be a good place to start.
+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.
-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. +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.
-+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!
+In the udevinfo output, I also noticed this bit of useful and understandable information:# udevinfo -a -p /sys/block/sda/sda1@@ -314,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).SYSFS{product}="USB 2.0M DSC"-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.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 persistent "camera" symlink points to the mountable partition.
Additional notes on writing rules for USB storage
@@ -326,33 +383,72 @@ Carl's rule is: This rule creates symlinks such as:-
We agreed that depending on the situation and device in question, there are reasons for both wanting and not wanting the non-mountable /dev/sda node. Use whichever setup suits you best.- /dev/usbhdd - The fdiskable node
-- /dev/usbhdd1 - The first partition (mountable)
-- /dev/usbhdd2 - The second partition (mountable)
+- /dev/usbhd - The fdiskable node
+- /dev/usbhd1 - The first partition (mountable)
+- /dev/usbhd2 - The second partition (mountable)
+Another difficult situation is having a multiple-slot USB-storage card reader. These types of device generally do not inform the host when new cards are plugged in or out, so plugging a card into an unused slot while the reader is plugged in will not create the extra device node needed for mounting!
+This problem also applies to other USB disks - e.g. if you create a new partition, the new partition node will not appear until you re-plug the device.
+ +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:
+ ++ +You will now have nodes named: usbhd, usbhd1, usbhd2, usbhd3, ..., usbhd15.BUS="usb", SYSFS{product}="USB 2.0 Storage Device", NAME{all_partitions}="usbhd"
+Example: Writing convenience rules for my CD drives
I have two CD drives in my PC - a DVD reader, and a CD rewriter. My DVD is hdc and my CDRW is hdd. I would not expect this to change, unless I manually changed the cabling of my system.
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. --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 in local.rules are processed before the default rules, the default rules will not be used when naming the hardware you have written rules for.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 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:
+ ++ +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.BUS="usb", SYSFS{product}="Palm Handheld", KERNEL="ttyUSB*", SYMLINK="pilot"
+ + +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): + ++ +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.+# udevinfo -a -p /sys/class/net/eth0/ + looking at class device '/sys/class/net/eth0': + SYSFS{address}="00:52:8b:d5:04:48" +
+ +An example rule is shown below: + ++ +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.KERNEL="eth*", SYSFS{address}="00:52:8b:d5:04:48", NAME="lan"
+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
I'm looking for some more device specific tips here. Please contact me with any you can provide.+
- -- If the device you are looking to write rules for has created a device node under /dev, then you are in luck! Run the following command to get an appropriate /sys path: udevinfo -q path -n /dev/yournode
- Always use udevinfo to assist the rule-writing process. Always use udevinfo to look under /sys/block or /sys/class (it will not start reading a chain from anywhere else).
- If you get totally stuck, use the following command to find all "dev" files under /sys (udevinfo can work on directories containing this file): find /sys -iname dev
- If your device is a flash-card reader, usb flash-drive, or digital camera that acts as usb-storage, that is created as /dev/sdX, then start looking in /sys/block/sdX.
@@ -363,46 +459,32 @@ 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.
udev vs Nvidia's graphics drivers
- -This section isn't really relevant to the purpose of this document, but judging from the hits I get from google, this is a hot topic. I will leave it here for now.
+ +Debugging your rules
-Nvidia's graphics drivers (the closed-source ones, not the ones that come with XFree) do not work with a default installation of udev - you are unable to start X. This is because the nvidia module is loaded by X, but the /dev/nvidia* nodes are not created quick enough, so X bails out.
+If you have written rules and remembered to run udevstart but they do not appear to be taking effect, there are a couple of ways you can debug them.
-The solution to this problem is to autoload the nvidia module on bootup. Yes - you are *supposed* to do this - the NVidia FAQ confirms this! On devfs-based systems, devfs did this automatically at bootup anyway. Your linux distribution will have created a file which you can list modules to be loaded on bootup (e.g. /etc/modules.autoload.d/kernel-2.6 for Gentoo).
+The file /etc/udev/udev.conf contains a udev_log option. Setting this option to yes will cause udev to log some useful information about which rules are being applied to which nodes into the system logger. The logs will be included in /var/log/messages for most users.
-This isn't all - you will also need to patch the nvidia kernel interface to export some basic info to SYSFS so that udev will create the devices. Martin Schlemmer has written a patch against the 1.0.5336 version of the nvidia drivers, which can be found here. The Gentoo package nvidia-kernel-1.0.5336-r1 contains this patch.
+Additionally, if you know the path in sysfs for the node you want to create, you can use udevtest to see a rundown on what udev would do with the node. For example: -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. /etc/conf.d/local.start for Gentoo): -+-mknod /dev/nvidia0 c 195 0 -mknod /dev/nvidiactl c 195 255 -# 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'