Haven't updated this for awhile, until recently when a kind soul found an error in one of the rules, rules have changed since I wrote it. As of kernel-2.6.13 using devfs is no longer an option and I believe most people are using udev now instead of devfs. That being said, most of this document is probably not relevent anymore, but feel free to read it cause you might actually find something you can use. If I have a issue now, it is usually finding out what udev is calling my device when I plug it in. Udevmonitor command is helpful in finding that info, I make a little snippet on that in the Snippet of How to Write UDEV Rules section. Then later HAL and DBUS came along and made using Udev even easier. About the only thing I have to change anymore is permissions so the user can access device or find out what the device is being called. Otherwise I am leaving this up for legacy sake, you can see what we had to go thru when udev first came into use and you might actually find something helpful.
If you do find something relevent to today and needs clarified,... let me know so I can look into it. Otherwise my spare time is spent working on my Stock Portfolio App for Linux, playing HL2, fishing,...
If you already have UDEV installed and working you might want to read DSD's Guide on Writing UDev Rules. In the links at the bottom.
Little preliminary first. What is going on in the 2.6 kernel with Devfs? Straight from the horse mouth:
Note that devfs has been obsoleted by udev,RECENT CHANGES to Primer:
1) Fixed a rule for scanner that has changed since first wrote this and added another for same device.
2) Changed Intro above letting readers know most of the document is not relevent anymore.
3) Added a little about udevmonitor commmand and removed references to udevread.py (changes broke it and wasn't worth fixing).
INDEX Most recent x86 stable udev version 115-r1, x86_64 stable version 114, unstable 116-r1.
What is UDEV?
In a nutshell UDEV is an replacement of DEVFS in user-space using sysfs and /sbin/hotplug. It will create and destroy /dev entries based on the current system configuration. It does this by watching the /sbin/hotplug events on the system, and readinginformation about these events from sysfs.
Udev works entirely in user-space, using /sbin/hotplug calls that the kernel makes whenever a device is added or removed from the kernel. All naming policy, and permission control is done in user-space.
Link to read for more information.
http://ftp.kernel.org/pub/linux/utils/kernel/hotplug/udev-FAQWell your first question is probably: How do I get started with udev?
It isn't that hard at all. Unless you have never compiled a kernel, which really isn't in the scope of this tutorial.
Steps to Setting Up UDEV.
Like mentioned above you need to install and setup the 2.6 Kernel. This example is for a 2.6.14 kernel. For older kernels see below this in same section.
As of kernel-2.6.13 choosing devfs is no longer an option. If you want devfs as an option, see below and choose one less than 2.6.13 .
#Older kernels (pre 2.6.13)
Like mentioned above you need to install and setup the 2.6 Kernel. Your best bet for kernels to use right now are: >mm-sources-2.6.1-mm3 (right now I am using: 2.6.4-gentoo-r1) or >gentoo-dev-sources-2.6.1 or >development-sources-2.6.2. Watch the Greater-Than sign and get a kernel newer than. Udev will work with some of the lesser kernels, but improvements have been made, so it will improve your success with a newer kernel. The important part of the configuration is shown below. Also note: You can still compile in DEVFS if you want and choose which to use at boot, so don't feel you cannot compile devfs in kernel to use udev. See the editing lilo or grub section for more details. If for some reason you do not see one of the kernel options you need below in your kernel, keep reading, some kernels are different.
Initrd Note: It would appear that if you want to use initrd , which I don't use, you do have to either use the tarball or Compile your kernel with Devfs support or you will get an error about 'devfs not mounting on /dev' . Doesn't mean you have to use it, just have it compiled in the kernel.
Genkernel Note: If you are using the genkernel, it looks like version 3.0.2f has been setup to work with Udev. Compile your genkernel with the --udev option. For example: genkernel --save-config --menuconfig --udev all
Also: when you write your Lilo or Grub bootloader you will need to use udev instead of gentoo=nodevfs , which is used with manually compiled kernels (pre 2.6.13). Otherwise despite your best effort it still won't use Udev. See the bootloader section if you need examples. Apart from the kernel and bootloader option, the rest of the primer should apply.
NOTE: DO NOT Enable Devfs to Automatically mount at Boot!! Haven't seen this before, but at least as of baselayout-1.8.6.13-r1 it will ignore your 'nomount' option in the bootloader. It will try to load devfs no matter what setting you have in the /etc/conf.d/rc file. Your system will not boot and will fail at 'checking root filesystem'. The filesystem isn't corrupt though. You will need to chroot from somewhere else and take OUT the option to Automatically mount Devfs at boot and recompile the kernel. You might even have to do it with 'make mrproper' before running 'make menuconfig'. Before running 'make mrproper' be sure to copy your .config somewhere safe or lose it. Note: You will not see the option for 'Automatically mount at boot' for devfs unless you have enabled devfs. Then it is still not enabled unless you enable it!
There are some options in the config that do not show up during menuconfig that will if you look at the .config itself. So if you want to check the file itself, this is what it should look like. Also in some kernels CONFIG_HOTPLUG might appear somewhere else, like in General Setup. You could grep what you are looking for in the .config file and then go back to menuconfig to set it. (hint)
mm-sources-2.6.3-rc1-mm1 example:
# Bus options (PCI, PCMCIA, EISA, MCA, ISA)You can still compile DEVFS (CONFIG_DEVFS_FS) in the kernel if you want, but see warning above. If you do, there are some boot options for lilo or grub that will mention later, where you can switch between the two. Remember though that devfs has been stripped down. Also make note that if you make changes in devfs and reboot the changes won't be saved for udev. The only way to save device state with udev is if using the /lib/udev-state/devices.tar.bz2 in the default Gentoo Udev install. If your running a pure udev system (ie. Not using devices.tar.bz2), then no changes will be saved, it is dynamic device creation. If you want to make changes to your devices in udev, you could try adding to the /etc/udev/rules.d/10-local.rules file. You will have to create that file, try not to edit the 50-udev.rules in there. That is the Default rules, add your own rules to the 10-local.rules you will/can create. You will not have to make your own rules to get started for most systems. If you need to though, create your own where mentioned.
If you do then: rc-update del hotplug default to remove it and only Keep it in boot level.
Now at this point you might think you actually have udev, sysfs and hotplug installed and ready to go. Sorry, but NO. Until sysfs is mounted, /dev mounted in tmpfs (or ramfs if you don't have tmpfs enabled in kernel) and hotplug started you aren't really doing anything. You will see changes in /sys if remove or add devices, but the system is not doing anything with this information.
Saving the state of /dev kind of goes against the grain of what UDEV is all about doesn't it? Udev is suppose to be a dynamic filesystem that creates only the devices on your system and only when needed, for example: your scanner isn't plugged in, you don't need a device for it, so it is not created. You plug it in, then udev creates the device. If the state of /dev is saved, then it would be there next time, even if not plugged in. The reason for doing so was because at first only the basic devices needed were created by Udev. Devices like block (harddrives), tty,... UDEV has come a long way now and has most of the functionality you need now. So it is possible to run a Pure Udev system now. There are a few problems, but there are work-arounds for them.
Sometime in the future Gentoo will probably remove the devices.tar.bz2 file, when udev gets further along. It looks like udev is getting closer to making that happen.
Make sure you put the new config files in place that need updating by running etc-update. If you don't, then it won't save your current /dev folder in /lib/udev-state/devices/devices.tar.bz2 . Even if your not planning on using the default devices.tar.bz2 and want to run a pure udev system, it won't have the necessary scripts in place to use udev.
Note: If you are using >=baselayout-1.8.6.13-r1 you do not have to edit /etc/init.d/halt.sh or /sbin/rc. For old baselayout ( <1.8.6.13-r1 ) see next section.
Newer Baselayout ( >=1.11.13-r1 )Note: With the baselayout-1.11.13-r1 and newer you do not have to add gentoo=noudev or gentoo=nodevfs to the bootloader (ie. kernel param) any longer. If you have a udev enabled system and /etc/conf.d/rc is set to auto or udev, it will boot using udev without using gentoo=nodevfs. If you have a device that needs earlier detection and is not working without it, then put gentoo=nodevfs back in bootloader and try it, /sbin/rc still looks for it. Also, with newer kernels like 2.6.14 (since 2.6.13) devfs is no longer an option in the kernel. So with those kernels nodevfs is irrelevant.
After a lot of trial and error. This is how you can setup these options: (Note: do not use 'devfs=nomount' in your bootloader, the /sbin/rc is not looking for that option, it looks for 'gentoo=nodevfs' or 'gentoo=noudev')
Below part is really only relevant from baselayout-1.8.6.13-r1 to pre 1.11.13-r1 because /etc/conf.d/rc options have changed. So you can skip this if doesn't apply to you. Also with newer kernels like 2.6.14, devfs is no longer an option in the kernel.
This is default setting. You can still boot to Devfs or Udev. If have the same or different kernel setup to use either, then have the proper options shown below for lilo or grub (ie. gentoo=noudev or gentoo=nodevfs)
Devfs or Udev (with Tarball)
RC_DEVICE_TARBALL="yes"This next setting is to Not use the Tarball (devices.tar.bz2), but still be able to boot to devfs if desire. If have the same or different kernel setup to use either, then have the proper options shown below for lilo or grub (ie. gentoo=noudev or gentoo=nodevfs)
Devfs or Udev (without Tarball (ie. pure udev))
RC_DEVICE_TARBALL="no"This next setting is to Not use the Tarball or Do use the Tarball (devices.tar.bz2). But you will not be able to boot to Devfs at all!
Udev (with Tarball) and NO Devfs
RC_DEVICE_TARBALL="yes"OR
Udev (without Tarball (ie. pure udev)) and NO Devfs
RC_DEVICE_TARBALL="no"After this many variables have been introduced, I can't wait for Devfs to be totally gone, so it is no longer an option!
/etc/init.d/halt.sh
If you just comment out in this file (as root), where the blue ## are, you will stop it from saving the state of /dev into the devices.tar.bz2 file when you shut down. This part of the script where it saves the /dev state is near the top of halt.sh
/sbin/rc
There is just one line in this file (as root) to comment out where the blue ## is at. This will prevent /sbin/rc from populating /dev from the contents of devices.tar.bz2 file when you start up.
This will create the /dev/console and /dev/null statically in your /dev folder.
If you did not compile DEVFS into the kernel OR are using a >=kernel-2.6.13 (devfs is no longer an option). You are set to go. There is nothing you need to add to the boot-loaders. If you did compile DEVFS into the kernel (Did you compile Devfs to automatically load at boot? If so, go back and recompile the kernel without that option. Last warning!) You need to make a couple decisions. Gentoo will want to load devfs at boot if you compiled it into the kernel, even if you have udev. So here are a few options:
Option 1: Don't load devfs, use udev: You will need to add --> gentoo=nodevfs
Option 2: Don't load udev, use devfs: You will need to add --> gentoo=noudev
Option 3: Have two options in grub or lilo that let you boot to the same kernel, but with either udev or devfs: You need to add two boot options to lilo or grub. One with first option, the other with second option.
Genkernel Options 1: Don't load devfs, use udev: You will need to add --> udev
Genkernel Options 2: Don't load udev, use devfs: You will need to add --> devfs
Lilo examples:
Option 1: Don't load devfs, use udev.
# Udev system mm1 2.6.3 kernelOption 2: Don't load udev, use devfs.
# Devfs system mm1 2.6.3 kernelOption 3: Choose which to use at boot.
# Udev system mm1 2.6.3 kernelGenkernel Options 1: Don't load devfs, use udev.
# Udev system kernel-2.6.10Genkernel Options 2: Don't load udev, use devfs.
# Devfs system kernel-2.6.10Grub example: Pretty much the same, just different boot-loader.
Option 1: Don't load devfs, use udev.
title=Gentoo UDEVAnother Grub example:
title=Gentoo MainIf you did not compile DEVFS in the kernel and want to at a later date, recompile your kernel with DEVFS support. Then just go back and edit your boot-loader and add the appropriate changes like above and use one or the other or both. Just can't use udev or devfs at the same time. Sorry, one per customer. Again, I stress: Do Not compile the Kernel to Automatically load Devfs at Boot!!! (Really, really, this is the last warning!!) If your using a >=kernel-2.6.13 then DEVFS is no longer an option to use.
Always check your /var/log/error_log_files first, they might have useful clues.
Edit the devices.tar.bz2: Some people are opting to use the default Gentoo udev install and simply change their devices.tar.bz2 to contain only the devices that they need that are not being created by udev. So if your wanting to do this for devices like console and null and/or others. One simple way is by using Ark. There are other ways graphically and commandline. I simply moved devices.tar.bz2 to a temp folder in Home, Viewed it, selected everything but console and null and said delete. It did it, then complained that delete failed, but it didn't. Saved the file. Rechecked and only those two devices are there. Then you can replace the old devices.tar.bz2 with this one. Just make sure you don't edit the scripts to not use the tarball if doing it this way. If your using the Tarball this way, you would want to leave RC_DEVICE_TARBALL="yes"
Nvidia Drivers: Sysfs can now only be used by code using the GPL. Udev won't create the nvidiadevice nodes now. Nvidia will have to take care of creating the device nodes. Nvidia-glx will create the device nodes when X starts.Unfortunetly Nvidia has not fixed this issue with legacy cards. So if you have an older card like: RivaTNT, then you willhave to create the device nodes yourself.
See /usr/share/doc/nvidia-kernel-<version>/README.gz in Appendix A to find if your card is Unsupported (legacy) now. The latest version for legacy cards is nvidia-kernel-1.0.7174 and nvidia-glx-1.0.7174-r5
By the way, it is standard procedure to include nvidia modules in /etc/modules.autoload.d/your_kernel. So make sure you have added it to autoload with the other modules at boot.
Until Nvidia corrects this you will have to manually create the nvidia# and nvidiactl devices in /dev. One way of doing this that I use is just add it to your /etc/conf.d/local.start file like so:
Not sure how many devices you need. I just create two and haven't had a problem, just change the minor number to match. If using a legacy card and Nvidia starts creating the nvidia device nodes in /dev, you will see an error at boot, that the devices already exist. Then you can just remove the lines.
Nvidia/Alsa Devices: As of udev-021 there is a new binary /sbin/udevstart. Have not used versions with this new binary until emerged udev-023-r1, then ran into the problem with NO nvidia devices and only one alsa device (/dev/snd/controlC0) being created. So if you have installed version 021 and up and find your self rebooting and sitting their looking at a command-line login and no X. Run as root: /sbin/udevstart . It will create the devices and you can then exit back to user and run: startx . Have been informed this is a nvidia and alsa package problem and not a problem with udev.
Mixer/Midi/Dsp Devices: Some people, including me, report that the /dev/mixer isn't being created or /dev/midi or /dev/dsp. The problem is not udev. If you compiled your kernel with them as modules you have to load them in /etc/modules.autoload.d/kernel-2.6. So if those devices aren't being created add: snd_pcm_oss & snd_seq_oss to kernel-2.6 file. The snd_pcm_oss will load snd_mixer_oss itself.
snd_mixer_oss alias is /dev/mixerPPP Device: This appears to be one of the devices still needing the be ported to sysfs, so cannot make a udev rule to create it either. If you need a /dev/ppp you will have to manually create it. You could add it to your local.start like the nvidia device. Others have changed the tarball to just contain devices they need that aren't being created. If want to add it to your /etc/conf.d/local.start just add this line:
Mice, Meese and Mouses: For those of you that find your mouse doesn't work in udev. It really is not a udev problem, it is how you have configured it in the past. Udev (at this time) has 4 device locations you can use for mice.
/dev/input/miceSo whichever you are using (ie. mouse#, mice or psaux), just make sure your /etc/X11/XF86Config-4 or XF86Config or xorg.conf has the correct location for the device. You could make your own symlink in /etc/udev/rules.d/10-local.rules, but better off editing the XF86Config-4 or XF86Config file.
OR
Mounting /dev for udev ...There are a few other issues adding at the bottom here for things have seen on the forums.
LINKS:
Italian Udev Primer translation (Thanks to: Enrico Morelli) Udev Primer: Italian
Japanese Udev Primer translation (Thanks to: Masanao Igarashi) Udev Primer: Japanese
Japanese Udev Primer Misc translation (Thanks to: Masanao Igarashi) Udev Primer Misc: Japanese
German Udev Primer translation (Thanks to: Alexander Minges) Udev Primer: German
DSD's Guide on how to Write Udev Rules: Writing udev rules.
Main UDEV post going at Gentoo Forums:Gentoo Forum Thread.
Snippet of How to Write UDev Rules: Using DSD's Guide on how to Write UDEV Rules. Wanted to see how easy it was. So I picked the device most recently working with udev, my Microtek Scanner. Turned out to be very easy.
Update: udevread.py broken, not worth fixing, referenced here only for example/legacy purposes
New: To find out what a device is being called and other sysfs information on it now I use the command: udevmonitor --env as root. You can use just the udevmonitor command, but with the --env you get a lot more info. I recently used it on a Verizon Razr V3m to see what it was being called and found out it wasn't being claimed by any driver, so had to recompile my kernel with correct driver in. There is a lot of info printed, so just type the command in as root and plug your device in. That should give you plenty of information to write a rule. Below is the original way did it, so just disregard the udevread.py script and you can see how to use the info to develop a rule.
Wrote python script to show sysfs information for all the devices in /dev. New version: Added ability to search /sys for one device. Download it here: udevread-0.3.tar . To run it just extract it, run: python udevread.py --all for all devices in /dev OR python udevread.py --dev device_path. Example: python udevread.py --dev input/mice . Probably better open your terminal fullscreen. To see what the output looks like go here: udevread.py output
Note: You can now get udevinfo by entering the device path now instead of having to search for the class in /sys. Real time saver. Kinda makes udevread.py less useful now. But still has some advantages. Check out DSD's udev rules link to see the updated udevinfo information.
Without udevread.py: First I know that my scanner device in /dev is /dev/sg0. Can also use tail -f /var/log/syslog if have debug on hotplug turned on and see something like this:
Feb 25 09:16:17 decibels default.hotplug[18643]: arguments (scsi_generic) env (OLDPWD=/ DEVPATH=/class/scsi_generic/sg0 ...
So using his information I picked out the best non changing identifiers.
bash-2.05b# find | grep sg0Based on that response:
bash-2.05b# udevinfo -a -p /sys/class/scsi_generic/sg0With udevread.py: First I know that my scanner device in /dev is /dev/sg0. Can also use tail -f /var/log/syslog if have debug on hotplug turned on and see something like this:
Feb 25 09:16:17 decibels default.hotplug[18643]: arguments (scsi_generic) env (OLDPWD=/ DEVPATH=/class/scsi_generic/sg0 ...
The results are similar, but you don't have to search for the path in /sys. Plus give a little more info, where you would have to use udevinfo twice to get the same. Plus you can use the --all option to print the same results for all device nodes in /dev.
Making the Rule: Okay, let's see if can use that to make a rule to also call my scanner /dev/sg0 as /dev/scanner .
Where to add the rules? There is /etc/udev/udev-rules , /etc/udev/rules.d/50-udev.rules. Well 50-udev.rules is Default and DSD doesn't recommend editing that file. /etc/udev/udev.rules is defunct now and is not used, you can safely delete it now if up-to-date on your udev version. DSD says to make a new file in /etc/udev/rules.d called 10-local.rules for local rules. So create a new file in /etc/udev/rules.d called 10-local.rules and add below: (Tested, don't need to add the spaces at end of model name).
# Microtek Scanneror the one I use now, if you want or need some extra rules:
BUS=="scsi", SYSFS{model}=="Scanner V6UPL", KERNEL=="sg?", NAME="%k", SYMLINK+="scanner", GROUP="scanner", MODE="0660"I know that my XSane backend is microtek2.conf in /etc/sane.d folder. It looks for a scsi device, so added /dev/scanner to it. And started two scanner programs. Both of them now give me the choice of using /dev/sg0 or /dev/scanner. So works perfect.
What good is this? Well in this instance not much. Mainly an exercise. I mean one is just a symlink to /dev/sg0. But in other cases you might have multiple devices and want to tell which one is which. In some cases though symlinks can be very helpful. See below for example. You can read DSD's Guide for more details.
DVD: Next problem I had was playing dvd's and vcd's,... Using a cdwriter/dvd reader combo drive. DSD has good information on this particular on his howto. Udev creates the symlink /dev/cdroms/cdrom0 fine by default, but may not create the /dev/dvd (which was my case). So couldn't play a dvd or vcd,... Also I am not using scsi emulation, instead using hdc=ide-cd . So, using the udev rules howto, added this to my /etc/udev/rules.d/10-local.rules :
# cdrom/dvd devicesThat created the extra /dev/dvd symlink to my /dev/hdc (remember, you do not have to reboot, just run udevstart from the command line as root). So now have two symlinks to /dev/hdc.
Camera: Some Digital Camera's need a device and some don't. If you camera uses one of the scsi devices (ie. /dev/sd# or /dev/sg#) then see DSD's rules. The ones that do not require a device just require some setup. My camera Kodak DX3215 just requires gphoto2 and can use gtkam or digikam gui's. It does show up in /sys folder, but not in /sys/class as a device I can make a rule on. At least any attempts to do so have failed miserably, at no time during testing was a new device created in /dev and found no reference to any device in /sys except it was on the usb bus. The only problem had was permissions, both gtkam and digikam require a little work to be done in /etc/hotplug/usb.usermap and choose which script wanted to use as /etc/hotplug/usbcam and make usbcam executable. Gtkam works fine, but get error about Could not list folders in '/'. even as root. Digikam doesn't get that error.
Thanks for visiting Decibels Help.
Tip Jar