Map Custom Device Entries with udev

From ArchWiki
Revision as of 12:03, 2 October 2006 by Romashka (Talk | contribs) (1. Get the udev info for your usb device)

Jump to: navigation, search


WIP - DibbleTheWrecker

Use udev and hotplug to map multiple nodes to a device

This information is basically mirrored from the gentoo wiki with some additional hints.

This process allows you to always map a specific device to the same /dev node. This can then be used in fstab to ensure you can always mount the device same device in exactly the same place - which is great for desktop shortcuts!

1. Get the udev info for your usb device

Make sure one of your target devices is plugged in and then run the following as root:

udevinfo -a -p `udevinfo -q path -n /dev/sda`

If this method works for you then just and "| grep SYSFS{serial}" when you get to the next part.

This gets the udev device info for the device on /dev/sda - if your device is not mapped to /dev/sda then obviously use the correct mapping :)

You should get some output like this:

udevinfo starts with the device the node belongs to and then walks up the
device chain, to print for every device found, all possibly useful attributes
in the udev key format.
Only attributes within one device section may be used together in one rule,
to match the device for which the node will be created.

device '/sys/block/sda/sda1' has major:minor 8:1
  looking at class device '/sys/block/sda/sda1':
    SYSFS{dev}="8:1"
    SYSFS{size}="499195"
    SYSFS{start}="4"
    SYSFS{stat}="      84       84      202      202"

follow the class device's "device"
  looking at the device chain at '/sys/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1:1.0/host0/target0:0:0/0:0:0:0':
    BUS="scsi"
    ID="0:0:0:0"
    SYSFS{detach_state}="0"
    SYSFS{device_blocked}="0"
    SYSFS{max_sectors}="240"
    SYSFS{model}="disgo           "
    SYSFS{queue_depth}="1"
    SYSFS{rev}="4.70"
    SYSFS{scsi_level}="3"
    SYSFS{state}="running"
    SYSFS{timeout}="30"
    SYSFS{type}="0"
    SYSFS{vendor}="        "

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1:1.0/host0/target0:0:0':
    BUS=""
    ID="target0:0:0"
    SYSFS{detach_state}="0"

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1:1.0/host0':
    BUS=""
    ID="host0"
    SYSFS{detach_state}="0"

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:1d.0/usb1/1-1/1-1:1.0':
    BUS="usb"
    ID="1-1:1.0"
    SYSFS{bAlternateSetting}=" 0"
    SYSFS{bInterfaceClass}="08"
    SYSFS{bInterfaceNumber}="00"
    SYSFS{bInterfaceProtocol}="50"
    SYSFS{bInterfaceSubClass}="06"
    SYSFS{bNumEndpoints}="02"
    SYSFS{detach_state}="0"

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:1d.0/usb1/1-1':
    BUS="usb"
    ID="1-1"
    SYSFS{bConfigurationValue}="1"
    SYSFS{bDeviceClass}="00"
    SYSFS{bDeviceProtocol}="00"
    SYSFS{bDeviceSubClass}="00"
    SYSFS{bMaxPower}="178mA"
    SYSFS{bNumConfigurations}="1"
    SYSFS{bNumInterfaces}=" 1"
    SYSFS{bcdDevice}="0200"
    SYSFS{bmAttributes}="80"
    SYSFS{detach_state}="0"
    SYSFS{devnum}="2"
    SYSFS{idProduct}="0012"
    SYSFS{idVendor}="08ec"
    SYSFS{manufacturer}="M-Sys"
    SYSFS{maxchild}="0"
    SYSFS{product}="disgo"
    SYSFS{serial}="1730C13B18000B84"
    SYSFS{speed}="12"
    SYSFS{version}=" 2.00"

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:1d.0/usb1':
    BUS="usb"
    ID="usb1"
    SYSFS{bConfigurationValue}="1"
    SYSFS{bDeviceClass}="09"
    SYSFS{bDeviceProtocol}="00"
    SYSFS{bDeviceSubClass}="00"
    SYSFS{bMaxPower}="  0mA"
    SYSFS{bNumConfigurations}="1"
    SYSFS{bNumInterfaces}=" 1"
    SYSFS{bcdDevice}="0206"
    SYSFS{bmAttributes}="c0"
    SYSFS{detach_state}="0"
    SYSFS{devnum}="1"
    SYSFS{idProduct}="0000"
    SYSFS{idVendor}="0000"
    SYSFS{manufacturer}="Linux 2.6.10-ARCH uhci_hcd"
    SYSFS{maxchild}="2"
    SYSFS{product}="Intel Corp. 82801CA/CAM USB (Hub #1)"
    SYSFS{serial}="0000:00:1d.0"
    SYSFS{speed}="12"
    SYSFS{version}=" 1.10"

  looking at the device chain at '/sys/devices/pci0000:00/0000:00:1d.0':
    BUS="pci"
    ID="0000:00:1d.0"
    SYSFS{class}="0x0c0300"
    SYSFS{detach_state}="0"
    SYSFS{device}="0x2482"
    SYSFS{irq}="11"
    SYSFS{local_cpus}="1"
    SYSFS{subsystem_device}="0x4541"
    SYSFS{subsystem_vendor}="0x8086"
    SYSFS{vendor}="0x8086"

  looking at the device chain at '/sys/devices/pci0000:00':
    BUS=""
    ID="pci0000:00"
    SYSFS{detach_state}="0"

Bit too much information! The only bit of this you actaully need is the =SYSFS{serial}= part - so now you know what the above command does just grep out the bit you want in future cases:

udevinfo -a -p `udevinfo -q path -n /dev/sda` | grep SYSFS{serial}

2. Create a udev rule

You then use the SYSFS{serial} in a udev rule as follows:

Note: The convention for Arch Linux is to place custom rules into /etc/udev/rules.d/00.rules

BUS=="usb", SYSFS{serial}=="0402170100000020EB5D00000000000", KERNEL=="sd?1", NAME="%k", SYMLINK="usbdrive", GROUP="storage"

3. Create an fstab entry and mount point

Create a directory: mkdir /mnt/usbdrive

In your /etc/fstab, create an entry like this:

/dev/usbdrive    /mnt/usbdrive    vfat noauto,user,noexec,nodev,nosuid    0 0

Now root or any user who belongs to the "storage" group can mount the usb stick by simply doing mount /mnt/usbdrive

Btw, all the last 3 additional mount options are meant to increase your system's security, e.g. they will prevent you running an executable file directly from the usb drive.

Check if /etc/group contains an entry called "storage".

If it does not, simply do a "groupadd storage"

Then edit /etc/group to make the new storage group entry look like this:

storage:x:102:user1,user2

user1 and user2 are the names of the non-root users you wish to have access to the usb stick.

4. Restart udev

Only if really needed, you may restart udev like this. As root, run those 3 commands:

/etc/./start_udev
mount /dev/pts
mount /dev/shm

5. Examples

Here are some examples from my system. My devices sometimes mount on sda or sda1 so I have two rules for each - this is a work around for device not found problems. The sda node is also needed for disk-level activities e.g. fdisk /dev/sda.

Note: The convention for Arch Linux is to place custom rules into /etc/udev/rules.d/00.rules

This always maps my disgo USB pen to /dev/usbpen which i then map in fstab to mount on /mnt/usbpen

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

If you have a device with with multiple partitions, the following example maps the device to /dev/usbdisk, and partitions 1,2,3,etc to usbdisk1, usbdisk2, usbdisk3, etc

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

These rules are equivalent to the following one:

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

You can also omit the NAME and GROUP statements, so that the defaults from udev.rules are used. So the shortest and simplest solution would be adding this rule:

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

This always maps our Olympus digicam to /dev/usbcam which i then map in fstab to mount on /mnt/usbcam

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

And this maps my Packard Bell MP3 player to /dev/mp3player

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

To map your own usb key to /dev/mykey and all of other keys to /dev/otherkey

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

Note the order of the lines. Since all the usb keys should create the /dev/sd<a||b> node, udev will first check if it is your own usb key, defined with the serial number. But if you plug another key witch you don't know the serial number, it will create a node too, with a generic name "otherkey". That rule should be the last one your rules file.