Map Custom Device Entries with udev

From ArchWiki
Revision as of 23:56, 9 December 2009 by Time (Talk | contribs) (spelling)

Jump to: navigation, search

Template:I18n links start Template:I18n entry Template:I18n entry Template:I18n links end

This process allows to map a specific device to a constant device-node, located in Template:Filename, by using udev rules. This can then be used in fstab, among other places, to ensure that the device can be mounted with a unchanging device-node--ideal for desktop shortcuts and other mounting operations.

This article focuses on USB devices and was copied almost verbatim from the Gentoo wiki, later supplemented with additional hints.

Get USB device udev information

Here is script that facilitates this operation. Simply plug in the device before running the script. Save it as Template:Filename, for example, and run it like so: Template:Command The script is available here.

Manual method

Make sure one of the target devices is plugged in and then run the following (replacing Template:Filename as needed): Template:Command

The only part of this that is needed is Template:Codeline}, so use Template:Codeline to filter the information: Template:Command

This narrows down the search to a much greater degree; however, one of the two serials is irrelevant. Trim down the Template:Codeline: Template:Command The first choice is obviously the correct one.

Create udev rule

Use the Template:Codeline} in a udev rule as follows. Place it in Template:Filename:

BUS=="usb", ATTRS{serial}=="14AB0000000096", KERNEL=="sd?1", NAME="%k", SYMLINK+="usbdrive", GROUP="storage"
Note: When creating a file with a different naming scheme that those in the directory, remember that udev processes these files in alphabetical order.

Make fstab entry

Create a directory for mounting:

# mkdir /mnt/usbdrive

In Template:Filename, create an entry as:

/dev/usbdrive /mnt/usbdrive vfat rw,noauto,group,flush,quiet,nodev,nosuid,noexec,noatime,dmask=000,fmask=111 0 0

Options Template:Codeline are unnecesary; they are stated for security reasons only. Additionally, depending on your locale preferences, add Template:Codeline and Template:Codeline options (such as Template:Codeline) in order to be able to display non-Latin file-names correctly.

Now, root and users who belongs to the Template:Codeline group can mount the USB device by:

mount /mnt/usbdrive

To allow a non-root user to access to USB devices, add them to the storage group:

# gpasswd -a username storage

Restart udev

To load the updated rules, run:

# udevadm control --reload-rules

Examples

Here are some mapping and mounting examples. This system's devices sometimes made nodes as Template:Filename or Template:Filename so I two rules for each needed to be specified, which aid "device not found" problems. The Template:Filename node is also needed for disk-level activities; e.g., Template:Codeline.

This always maps a specific USB device (in this case, a pendrive) to Template:Filename, which is then set in Template:Filename to mount on Template:Filename:

# Symlink USB pen
SUBSYSTEMS=="usb", ATTRS{serial}=="1730C13B18000B84", KERNEL=="sd?", NAME="%k", SYMLINK+="usbpen", GROUP="storage"
SUBSYSTEMS=="usb", ATTRS{serial}=="1730C13B18000B84", KERNEL=="sd?1", NAME="%k", SYMLINK+="usbpen", GROUP="storage"

If for devices with multiple partitions, the following example maps the device to Template:Filename, and partitions 1, 2, 3 etc., to Template:Filename, Template:Filename, Template:Filename, etc.

# Symlink multi-part device
SUSSYSTEMS=="usb", ATTRS{serial}=="1730C13B18000B84", KERNEL=="sd?", NAME="%k", SYMLINK+="usbdisk", GROUP="storage"
SUBSYSTEMS=="usb", ATTRS{serial}=="1730C13B18000B84", KERNEL=="sd?[1-9]", NAME="%k", SYMLINK+="usbdisk%n", GROUP="storage"

The above rules are equivalent to the following one:

# Symlink multi-part device
SUBSYSTEMS=="usb", ATTRS{serial}=="1730C13B18000B84", KERNEL=="sd*", NAME="%k", SYMLINK+="usbdisk%n", GROUP="storage"

It's also possible to omit the Template:Codeline and Template:Codeline statements, so that the defaults from Template:Filename are used. Meaning that the shortest and simplest solution would be adding this rule:

# Symlink multi-part device
SUBSYSTEMS=="usb", ATTRS{serial}=="1730C13B18000B84", KERNEL=="sd*", SYMLINK+="usbdisk%n"

This always maps a Olympus digicam to Template:Filename, which can be stated in Template:Filename to mount on Template:Filename:

# Symlink USB camera
SUBSYSTEMS=="usb", ATTRS{serial}=="000207532049", KERNEL=="sd?", NAME="%k", SYMLINK+="usbcam", GROUP="storage"
SUBSYSTEMS=="usb", ATTRS{serial}=="000207532049", KERNEL=="sd?1", NAME="%k", SYMLINK+="usbcam", GROUP="storage"

And this maps a Packard Bell MP3 player to Template:Filename:

# Symlink MP3 player
SUBSYSTEMS=="usb", ATTRS{serial}=="0002F5CF72C9C691", KERNEL=="sd?", NAME="%k", SYMLINK+="mp3player", GROUP="storage"
SUBSYSTEMS=="usb", ATTRS{serial}=="0002F5CF72C9C691", KERNEL=="sd?1", NAME="%k", SYMLINK+="mp3player", GROUP="storage"

To map a selected usb-key to Template:Filename and all of other keys to Template:Filename:

# Symlink USB keys
SUBSYSTEMS=="usb", ATTRS{serial}=="insert serial key", KERNEL=="sd?1", NAME="%k", SYMLINK+="mykey"
SUBSYSTEMS=="usb", KERNEL=="sd?1", NAME="%k", SYMLINK+="otherkey"

Note the order of the lines. Since all the USB keys should create the Template:Filename node, udev will first check if it is a rules-stated USB-key, defined by serial number. But if an unknown USB-key is plugged, it will create also create a node, using the previously stated generic name, "otherkey". That rule should be the last one in rules file so that it does not override the others.

This is an example on how to distinguish USB HDD drives and USB sticks:

BUS=="usb", ATTRS{product}=="USB2.0 Storage Device", KERNEL=="sd?", NAME="%k", SYMLINK+="usbdisk", GROUP="storage"
BUS=="usb", ATTRS{product}=="USB2.0 Storage Device", KERNEL=="sd?[1-9]", NAME="%k", SYMLINK+="usbdisk%n", GROUP="storage"
BUS=="usb", ATTRS{product}=="USB Mass Storage Device", KERNEL=="sd?1", NAME="%k", SYMLINK+="usbflash", GROUP="storage"

Note that this udev rule doesn't use serials at all.