From ArchWiki
Revision as of 16:16, 28 February 2012 by JonnyJD (talk | contribs) (setting static devices with udev: extract only the important info)
Jump to: navigation, search

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.

Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어

External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

udev replaces the functionality of both hotplug and hwdetect.

"udev is the device manager for the Linux kernel. 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 article

udev loads kernel modules by utilizing coding parallelism to provide a potential performance advantage versus loading these modules serially. The modules are therefore loaded asynchronously. The inherent disadvantage of this method is that udev does not always load modules in the same order on each boot. If the machine has multiple block devices, this may manifest itself in the form of device nodes changing designations randomly. For example, if the machine has two hard drives, /dev/sda may randomly become /dev/sdb. See below for more info on this.

About udev rules

udev rules written by the administrator go in /etc/udev/rules.d/, their file name has to end with .rules. The udev rules shipped with various packages are found in /lib/udev/rules.d/. If there are two files by the same name under /lib and /etc, the ones in /etc take precedence.

If you want to learn how to write udev rules, see Writing udev rules.

To get a list of all of the attributes of a device you can use to write rules, run this command:

# udevadm info -a -n [device name]

Replace [device name] with the device present in the system, such as /dev/sda or /dev/ttyUSB0.

udev automatically detects changes to rules files, so changes take effect immediately without requiring udev to be restarted. However, the rules are not re-triggered automatically on already existing devices, so hot-pluggable devices, such as USB devices, will probably have to be reconnected for the new rules to take effect.


Simply install the udisks package, and all of your media should be automatically mounted in GNOME and KDE SC 4.6. There is no need for any additional rules this way. As an extra bonus you can remove HAL if you were only using that for auto mounting purposes.

Automounting UDisks Wrappers

A UDisks wrapper has the advantage of being very easy to install and needing no (or minimal) configuration. The wrapper will automatically mount things like CDs and flash drives.

  • devmon - devmonAUR is a configuration-less Bash wrapper script for udisks which automatically mounts optical discs and removable drives. It can also selectively automatically start applications or execute commands after mounting, ignore specified devices and volume labels, and unmount removable drives.
  • udiskie - Written in Python. Enables automatic mounting and unmounting by any user.
  • udisksevtAUR - Written in Haskell. Enables automatic mounting by any user. Designed to be integrated with traydeviceAUR.
  • The udisksvmAUR Bash script uses udisks and traydeviceAUR to automatically mount removable media and to control in GUI, with mouse clicks in systray, the un-mounting and re-mounting of disks or the ejection of optical disks.

UDisks Shell Functions

While UDisks includes a simple method of (un)mounting devices via command-line, it can be tiresome to type the commands out each time. These shell functions will generally shorten and ease command-line usage.

Tips & Tricks

Auto mounting USB devices

Mount options

In the following rules the mount options are defined as ENV{mount_options}="relatime", see man mount (and possibly man ntfs-3g) for all available options and Maximizing Performance#Mount options for performance-related options.

  • The users mount option will not allow users to unmount the filesystem.
  • The noexec mount option prevents execution of binaries on the mounted filesystem.
  • Mounting partitions in read-write mode while booting might prevent fsck from running. In this case, you will need to manually remount that partition in read-only mode.

Mount under /media; use partition label if present

The following udev rule set automatically mounts devices/partitions that are represented by /dev/sd* (USB drives, external hard drives and sometimes SD cards). If a partition label is available, it mounts the device to /media/<label> and otherwise to /media/usbhd-sd* (ex: /media/usbhd-sdb1):

# Start at sdb to avoid system harddrive. 
KERNEL!="sd[b-z][0-9]", GOTO="media_by_label_auto_mount_end"

# Import FS infos
IMPORT{program}="/sbin/blkid -o udev -p %N"

# Get a label if present, otherwise specify one
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"

# Global mount options
ACTION=="add", ENV{mount_options}="relatime"
# Filesystem-specific mount options
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"

# Mount the device
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}"

# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"

# Exit

Mount under /media; use partition label if present; supports LUKS encryption

Similar to the above rule set, but if the device is a LUKS-encrypted partition it will open an xterm window to ask for the passphrase (provided that xterm is installed). Also see this post and the follow-ups.

Note: You may need to modify the path to cryptsetup, depending on the version installed (e.g., < 1.1.1_rc2-1).
KERNEL!="sd[a-z]*", GOTO="media_by_label_auto_mount_end"
ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="media_by_label_auto_mount_end"

# Do not mount devices already mounted somewhere else to avoid entries for all your local partitions in /media
ACTION=="add", PROGRAM=="/bin/grep -q ' /dev/%k ' /proc/self/mountinfo", GOTO="media_by_label_auto_mount_end"

# Open LUKS partition if necessary
PROGRAM=="/sbin/blkid -o value -s TYPE %N",  RESULT=="crypto_LUKS", ENV{crypto}="mapper/", ENV{device}="/dev/mapper/%k"
ENV{crypto}=="", ENV{device}="%N"
ACTION=="add", ENV{crypto}!="", PROGRAM=="/usr/bin/xterm -display :0.0 -e 'echo Password for /dev/%k; /sbin/cryptsetup luksOpen %N %k'"
ACTION=="add", ENV{crypto}!="", TEST!="/dev/mapper/%k", GOTO="media_by_label_auto_mount_end"

# Global mount options
ACTION=="add", ENV{mount_options}="noatime"
# Filesystem-specific mount options
ACTION=="add", PROGRAM=="/sbin/blkid -o value -s TYPE %E{device}", RESULT=="vfat|ntfs", ENV{mount_options}="%E{mount_options},utf8,gid=100,umask=002"

# Get label if present, otherwise assign one
PROGRAM=="/sbin/blkid -o value -s LABEL %E{device}", ENV{dir_name}="%c"
# Use basename to correctly handle labels such as ../mnt/foo
PROGRAM=="/usr/bin/basename '%E{dir_name}'", ENV{dir_name}="%c"
ENV{dir_name}=="", ENV{dir_name}="usbhd-%k"

# Mount the device
ACTION=="add", ENV{dir_name}!="", RUN+="/bin/mkdir -p '/media/%E{dir_name}'", RUN+="/bin/mount -o %E{mount_options} /dev/%E{crypto}%k '/media/%E{dir_name}'"

# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l '/media/%E{dir_name}'"
ACTION=="remove", ENV{crypto}!="", RUN+="/sbin/cryptsetup luksClose %k"
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/rmdir '/media/%E{dir_name}'"

# Exit

Mount under /media; use partition label if present; support user un-mounting

This is a variation on the above rule set. It uses pmount (which will need to be installed) instead of mount, allowing a non-root user to unmount udev-mounted devices, and automatically removing the mount point. The required username (here tomk) must be hard-coded in the RUN command, so this rule set may not be suitable for multi-user systems. LUKS support has also been removed from the example, but can be easily reinstated as above. You must edit the /bin/su invocation to run as the correct user for your system. Note that the mount point will remain if the device is mounted and the system, with a persistent /media, is shutdown, since rc.shutdown uses umount, which does not remove the mount point. To avoid having /media contain old mount points, one solution is to mount /media as tmpfs.

KERNEL!="sd[a-z]*", GOTO="media_by_label_auto_mount_end"
ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="media_by_label_auto_mount_end"

# Get label
PROGRAM=="/sbin/blkid -o value -s LABEL %N", ENV{dir_name}="%c"
# use basename to correctly handle labels such as ../mnt/foo
PROGRAM=="/usr/bin/basename '%E{dir_name}'", ENV{dir_name}="%c"
ENV{dir_name}=="", ENV{dir_name}="usbhd-%k"

ACTION=="add", ENV{dir_name}!="", RUN+="/bin/su tomk -c '/usr/bin/pmount %N %E{dir_name}'"
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/su tomk -c '/usr/bin/pumount /media/%E{dir_name}'"

Mount under /mnt; create symbolic link under /media

The following rule set does not make use of partition labels; instead it mounts devices as usbhd-sdXY under the /mnt directory (ex: /mnt/usbhd-sdb1) and creates a symbolic link under /media.

KERNEL!="sd[a-z][0-9]", GOTO="mnt_auto_mount_end"

# Global mount options
ACTION=="add", ENV{mount_options}="relatime"
# Filesystem-specific mount options
ACTION=="add", IMPORT{program}="/sbin/blkid -o udev -p %N"
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"

# Mount under /mnt and create the symbolic link in /media 
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"

# Clean up after removal
ACTION=="remove", RUN+="/bin/rm -f /media/usbhd-%k", RUN+="/bin/umount -l /mnt/usbhd-%k", RUN+="/bin/rmdir /mnt/usbhd-%k"

# Exit

Mount under /media only if the partition has a label

KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_only_auto_mount_end"

# Import FS infos
IMPORT{program}="/sbin/blkid -o udev -p %N"
ENV{ID_FS_LABEL}=="", GOTO="media_by_label_only_auto_mount_end"

# Global mount options
ACTION=="add", ENV{mount_options}="relatime"
# Filesystem-specific mount options
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"

# Mount the device
ACTION=="add", RUN+="/bin/mkdir -p /media/$env{ID_FS_LABEL}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/$env{ID_FS_LABEL}"

# Clean up after removal
ACTION=="remove", ENV{ID_FS_LABEL}!="", RUN+="/bin/umount -l /media/$env{ID_FS_LABEL}", RUN+="/bin/rmdir /media/$env{ID_FS_LABEL}"

# Exit

Mount under /media; use partition label if present; ntfs-3g

Yet another example, this time making use of ntfs-3g read/write drivers for NTFS filesystems:

# vim:enc=utf-8:nu:ai:si:et:ts=4:sw=4:ft=udevrules:
# /etc/udev/rules.d/10-my-media-automount.rules

# start at sdb to ignore the system hard drive
KERNEL!="sd[b-z]*", GOTO="my_media_automount_end"
ACTION=="add", PROGRAM!="/sbin/blkid %N", GOTO="my_media_automount_end"

# import some useful filesystem info as variables
IMPORT{program}="/sbin/blkid -o udev -p %N"

# get the label if present, otherwise assign one based on device/partition
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"

# create the dir in /media and symlink it to /mnt
ACTION=="add", RUN+="/bin/mkdir -p '/media/%E{dir_name}'"

# global mount options
ACTION=="add", ENV{mount_options}="relatime"
# filesystem-specific mount options (777/666 dir/file perms for ntfs/vfat) 
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},gid=100,dmask=000,fmask=111,utf8"

# automount ntfs filesystems using ntfs-3g driver
ACTION=="add", ENV{ID_FS_TYPE}=="ntfs", RUN+="/bin/mount -t ntfs-3g -o %E{mount_options} /dev/%k '/media/%E{dir_name}'"
# automount all other filesystems
ACTION=="add", ENV{ID_FS_TYPE}!="ntfs", RUN+="/bin/mount -t auto -o %E{mount_options} /dev/%k '/media/%E{dir_name}'"

# clean up after device removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l '/media/%E{dir_name}'", RUN+="/bin/rmdir '/media/%E{dir_name}'"

# exit

Mount 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"

# Global mount options
ACTION=="add", ENV{mount_options}="relatime"
# Filesystem specific options
ACTION=="add", IMPORT{program}="/sbin/blkid -o udev -p %N"
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"

ACTION=="add", RUN+="/bin/mkdir -p /media/sd-%k", RUN+="/bin/ln -s /media/sd-%k /mnt/sd-%k", 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"

Mount CDs

To auto mount a CD a simple UDisks wrapper will get the job done properly.

Note: Maybe this should be merged to the Udisks wrapper section.

Accessing iPod

Tango-inaccurate.pngThe factual accuracy of this article or section is disputed.Tango-inaccurate.png

Reason: please use the first argument of the template to provide a brief explanation. (Discuss in Talk:Udev#)

I have not tried this yet, but here is the basics:

BUS="scsi", SYSFS{model}="iPod*", NAME="ipod"

Accessing Firmware Programmers and USB Virtual Comm Devices

The following ruleset will allow normal users (within the "users" group) the ability to access the USBtinyISP USB programmer for AVR microcontrollers and a generic (SiLabs CP2102) USB to UART adapter. Adjust the permissions accordingly. Verified as of 2010-02-11.

# USBtinyISP Programmer rules
SUBSYSTEMS=="usb", ATTRS{idVendor}=="1781", ATTRS{idProduct}=="0c9f", GROUP="users", MODE="0666"
SUBSYSTEMS=="usb", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="0479", GROUP="users", MODE="0666"
# USBasp Programmer rules http://www.fischl.de/usbasp/
SUBSYSTEMS=="usb", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="05dc", GROUP="users", MODE="0666"

# Mdfly.com Generic (SiLabs CP2102) 3.3v/5v USB VComm adapter
SUBSYSTEMS=="usb", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", GROUP="users", MODE="0666"

Execute on USB Insert

See execute on usb insert article or the devmon wrapper script.

Mount internal drives as a normal user

If you want to mount an internal drive in KDE or Gnome (maybe on other Desktop Environments too) as a normal user (without the need to type your superuser password), you just have to create the following file in PolicyKit Local Authority:

[Mount a system-internal device]

Mark internal SATA-Ports as eSATA-Ports

If you connected a eSATA bay or an other eSATA adapter the system will still recognize this disk as an internal SATA drive. Gnome and KDE will ask you for your root password all the time. The following rule will mark the specified SATA-Port as an external eSATA-Port. With that, a normal Gnome user can connect their eSATA drives to that port like a USB drive, without any root password and so on.

DEVPATH=="/devices/pci0000:00/0000:00:1f.2/host4/*", ENV{UDISKS_SYSTEM_INTERNAL}="0"
Note: The DEVPATH can be found after connection the eSata drive with the following command (replace sdb to your needs):
# find /sys/devices/ -name sdb

Setting static device names (for iscsi)

Test the output from scsi_id:

/lib/udev/scsi_id --whitelisted --replace-whitespace --device=/dev/sdb
 # the iscsi device rules
 # this will create an iscsi device for each of the targets
 KERNEL=="sd*", SUBSYSTEM=="block", PROGRAM="/lib/udev/scsi_id --whitelisted --replace-whitespace /dev/$name", RESULT=="3600601607db11e0013ab5a8e371ce111",


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. See blacklisting. Not at boot-time or later on when a hotplug event is received (eg, you plug in your USB flash drive).

udevd hangs at boot

After migrating to LDAP or updating an LDAP-backed system udevd can hang at boot at the message "Starting UDev Daemon". This is usually caused by udevd trying to look up a name from LDAP but failing, because the network is not up yet. The solution is to ensure that all system group names are present locally.

Extract the group names referenced in udev rules and the group names actually present on the system:

# fgrep -r GROUP /etc/udev/rules.d/ /lib/udev/rules.d | perl -nle '/GROUP\s*=\s*"(.*?)"/ && print $1;' | sort | uniq > udev_groups
# cut -f1 -d: /etc/gshadow /etc/group | sort | uniq > present_groups

To see the differences, do a side-by-side diff:

# diff -y present_groups udev_groups
network							      <
nobody							      <
ntp							      <
optical								optical
power							      |	pcscd
rfkill							      <
root								root
scanner								scanner
smmsp							      <
storage								storage

In this case, the pcscd group is for some reason not present in the system. Add the missing groups:

# groupadd pcscd

Also, make sure local resources are looked up before resorting to LDAP. /etc/nsswitch.conf should contain the line

group: files ldap

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.

Some devices, that should be treated as removable, are not

Create a custom udev rule, setting UDISKS_SYSTEM_INTERNAL=0. For more details, see the manpage of udisks.

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 /etc/rc.conf. Further reading: rc.conf.

Sound Problems or Some Modules Not Loaded Automatically

Some users have traced this problem to old entries in /etc/modprobe.d/sound.conf. Try cleaning that file out and trying again.

Note: Since udev>=171, the OSS emulation modules (snd_seq_oss, snd_pcm_oss, snd_mixer_oss) are not automatically loaded by default.

Persistent device names

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.

One method for network card ordering is to use the udev-sanctioned 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> | grep address | tr [A-Z] [a-z]
  • 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!

Other methods for setting network interface names are described in the Configuring Network#Interface names varying wiki entry.

Note: With a recent version of udev, this problem should be solved automatically thanks to the /lib/udev/write_net_rules program which runs the 75-persistent-net-generator.rules script which produces a 70-persistent-net.rules.

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.32. Earlier kernels do not have the necessary uevent stuff that udev needs for auto-loading.

IDE CD/DVD-drive support

Starting with version 170, udev doesn't support CD-ROM/DVD-ROM drives, which are loaded as traditional IDE drives with the ide_cd_mod module and show up as /dev/hd*. The drive remains usable for tools which access the hardware directly, like cdparanoia, but is invisible for higher userspace programs, like KDE.

A cause for the loading of the ide_cd_mod module prior to others, like sr_mod, could be e.g. that you have for some reason the module piix loaded with your initramfs. In that case you can just replace it with ata_piix in your /etc/mkinitcpio.conf.

Optical Drives Have Group ID Set To Disk

If the group ID of your optical drive is set to disk and you want to have it set to optical you have to create a custom udev rule:

# permissions for IDE CD devices
SUBSYSTEMS=="ide", KERNEL=="hd[a-z]", ATTR{removable}=="1", ATTRS{media}=="cdrom*", GROUP="optical"

# permissions for SCSI CD devices
SUBSYSTEMS=="scsi", KERNEL=="s[rg][0-9]*", ATTRS{type}=="5", GROUP="optical"

Other Resources