Udev
From ArchWiki
i18n |
---|
English |
Русский |
繁體中文 |
简体中文 |
Contents
|
Introduction
"udev is the device manager for the Linux 2.6 kernel series. Primarily, it manages device nodes in /dev. It is the successor of devfs and hotplug, which means that it handles the /dev directory and all user space actions when adding/removing devices, including firmware load." Source: Wikipedia
udev replaces the functionality of both hotplug and hwdetect.
udev loads kernel modules simultaneously, which can provide a speed increase during bootup. However, the downside is that it doesn't always load modules in the same order each time, which can cause problems with things like sound cards and network cards (if you have more than one of them). See below for more info on this.
About modules auto-loading
udev will not do any module loading for you unless MOD_AUTOLOAD is enabled in /etc/rc.conf. If you disable auto-loading you must manually load the modules you want/need by putting the list in the MODULES array in rc.conf, you can generate this list with the hwdetect --modules command.
About udev rules
udev rules go in /etc/udev/rules.d/, their file name has to end with .rules.
If you want to learn how to write udev rules see Writing udev rules.
To get a list of all the attributes of a device you can use to write rules:
udevadm info -a -p $(udevadm info -q path -n /dev/sda)
Tips & Tricks
Auto mounting USB devices
Mounting to /mnt/usbhd-sdXY
KERNEL!="sd[a-z][0-9]", GOTO="mnt_auto_mount_end" ACTION=="add", RUN+="/bin/mkdir -p /media/usbhd-%k", RUN+="/bin/ln -s /media/usbhd-%k /mnt/usbhd-%k" # Global mount options ACTION=="add", ENV{mount_options}="relatime,users" # Filesystem specific options ACTION=="add", PROGRAM=="/lib/initcpio/udev/vol_id -t %N", RESULT=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002" ACTION=="add", RUN+="/bin/mount -o $env{mount_options} /dev/%k /mnt/usbhd-%k" ACTION=="remove", RUN+="/bin/umount -l /mnt/usbhd-%k", RUN+="/bin/rmdir /mnt/usbhd-%k" LABEL="mnt_auto_mount_end"
Mounting to /mnt/usbhd-sdXY and creating a symbolic link in /media
KERNEL!="sd[a-z][0-9]", GOTO="mnt_media_auto_mount_end" # Global mount options ACTION=="add", ENV{mount_options}="relatime,users" # Filesystem specific options ACTION=="add", PROGRAM=="/lib/initcpio/udev/vol_id -t %N", RESULT=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002" ACTION=="add", RUN+="/bin/mkdir -p /mnt/usbhd-%k", RUN+="/bin/mount -o $env{mount_options} /dev/%k /mnt/usbhd-%k", RUN+="/bin/ln -s /mnt/usbhd-%k /media/usbhd-%k" ACTION=="remove", RUN+="/bin/rm -f /media/usbhd-%k", RUN+="/bin/umount -l /mnt/usbhd-%k", RUN+="/bin/rmdir /mnt/usbhd-%k" LABEL="mnt_media_auto_mount_end"
Mounting to /media using the partition label if it exists
Another version creating dir only in /media and mounting with the label’s partition if it exists (fallbacks to usbhd-sdXY otherwise):
KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end" # Global mount options ACTION=="add", ENV{mount_options}="relatime,users" # Filesystem specific options ACTION=="add", PROGRAM=="/lib/initcpio/udev/vol_id -t %N", RESULT=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002" ACTION=="add", PROGRAM=="/lib/initcpio/udev/vol_id --label %N", ENV{dir_name}="%c" ACTION=="add", PROGRAM!="/lib/initcpio/udev/vol_id --label %N", ENV{dir_name}="usbhd-%k" ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}" ACTION=="remove", ENV{dir_name}=="?*", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}" LABEL="media_by_label_auto_mount_end"
Mounting to /media only if the partition has a label
KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_only_auto_mount_end" ACTION=="add", PROGRAM!="/lib/initcpio/udev/vol_id --label %N", GOTO="media_by_label_only_auto_mount_end" ACTION=="add", RUN+="/bin/mkdir -p /media/$env{ID_FS_LABEL}" # Global mount options ACTION=="add", ENV{mount_options}="relatime,users" # Filesystem specific options ACTION=="add", PROGRAM=="/lib/initcpio/udev/vol_id -t %N", RESULT=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002" ACTION=="add", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/$env{ID_FS_LABEL}" ACTION=="remove", ENV{ID_FS_LABEL}=="?*", RUN+="/bin/umount -l /media/$env{ID_FS_LABEL}", RUN+="/bin/rmdir /media/$env{ID_FS_LABEL}" LABEL="media_by_label_only_auto_mount_end"
Mounting SD cards
The same rules as above can be used to auto-mount SD cards, you just need to replace sd[a-z][0-9] by mmcblk[0-9]p[0-9]:
KERNEL!="mmcblk[0-9]p[0-9]", GOTO="sd_cards_auto_mount_end" ACTION=="add", RUN+="/bin/mkdir -p /media/sd-%k", RUN+="/bin/ln -s /media/sd-%k /mnt/sd-%k" # Global mount options ACTION=="add", ENV{mount_options}="relatime,users" # Filesystem specific options ACTION=="add", PROGRAM=="/lib/initcpio/udev/vol_id -t %N", RESULT=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002" ACTION=="add", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/sd-%k" ACTION=="remove", RUN+="/bin/umount -l /media/sd-%k", RUN+="/bin/rmdir /media/sd-%k" LABEL="sd_cards_auto_mount_end"
Troubleshooting
Disabling modules auto-loading with the load_modules boot parameter
If you pass load_modules=off on your kernel boot line, then udev will skip all the auto-loading business. This is to provide you with a big ripcord to pull if something goes wrong. If udev loads a problematic module that hangs your system or something equally awful, then you can bypass auto-loading with this parameter, then go in and blacklist the offensive module(s).
Blacklisting Modules
In rare cases, Udev can make mistakes and load the wrong modules. To prevent it from doing this, you can blacklist modules. Once blacklisted, udev will never load that module. Not at boot-time or later on when a hotplug event is received (ie, you plug in your USB flash drive).
To blacklist a module, just prefix it with a bang (!) in your MODULES array in rc.conf:
MODULES=(!moduleA !moduleB)
Known Problems with Hardware
BusLogic devices can be broken and will cause a freeze during startup
This is a kernel bug and no fix has been provided yet.
PCMCIA Card readers are not treated as removable devices
To get access to them with hal's pmount backend add them to /etc/pmount.allow
Known Problems with Auto-Loading
CPU frequency modules
The current detection method for the various CPU frequency controllers is inadequate, so this has been omitted from the auto-loading process for the time being. To use CPU frequency scaling, load the proper module explicitly in your MODULES array in rc.conf.
Sound Problems or Some Modules Not Loaded Automatically
Some users have traced this problem to old entries in /etc/modprobe.conf. Try cleaning that file out and trying again.
Mixed Up Devices, Sound/Network Cards Changing Order Each Boot
Because udev loads all modules asynchronously, they are initialized in a different order. This can result in devices randomly switching names. For example, with two network cards, you may notice a switching of designations between eth0 and eth1.
Arch Linux provides the advantage of specifying the module load order by listing the modules in the MODULES array in rc.conf. Modules in this array are loaded before udev begins auto-loading, so you have full control over the load order.
# Always load 8139too before e100 MODULES=(8139too e100)
Another method for network card ordering is to use the udev-sanctified method of statically-naming each interface. Create the following file to bind the MAC address of each of your cards to a certain interface name:
SUBSYSTEM=="net", ATTR{address}=="aa:bb:cc:dd:ee:ff", NAME="lan0" SUBSYSTEM=="net", ATTR{address}=="ff:ee:dd:cc:bb:aa", NAME="wlan0"
A couple things to note:
- To get the MAC address of each card, use this command: udevadm info -a -p /sys/class/net/<yourdevice>
- Make sure to use the lower-case hex values in your udev rules. It doesn't like upper-case.
- Some people have problems naming their interfaces after the old style: eth0, eth1, etc. Try something like "lan" or "wlan" if you experience this problem.
Don't forget to update your /etc/rc.conf and other configuration files using the old ethX notation!
Known Problems for Custom Kernel Users
Udev doesn't start at all
Make sure you have a kernel version later than or equal to 2.6.15. Earlier kernels do not have the necessary uevent stuff that udev needs for auto-loading.
CD/DVD symlinks and permissions are broken
If you're using a 2.6.15 kernel, you'll need the uevent patch from ABS (which backports certain uevent functionality from 2.6.16). Just sync up your ABS tree with the abs command, then you'll find the patch in /var/abs/kernels/kernel26/.