Map Custom Device Entries with udev

From ArchWiki
Revision as of 19:21, 3 November 2008 by P3nguin (Talk | contribs) (Restart udev)

Jump to: navigation, search

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

This information is basically mirrored from the gentoo wiki with some additional hints. Recently it was updated to reflect changes in udev >= 98 syntax.

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!


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:

udevadm info -a -p `udevadm info -q path -n /dev/sda`

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:


Udevadm starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

  looking at device '/block/sda':
    KERNEL=="sda"
    SUBSYSTEM=="block"
    DRIVER==""
    ATTR{stat}=="      19      111      137      160        0        0        0        0        0      152      160"
    ATTR{size}=="2007040"
    ATTR{removable}=="1"
    ATTR{range}=="16"
    ATTR{dev}=="8:0"

  looking at parent device '/devices/pci0000:00/0000:00:02.2/usb1/1-5/1-5:1.0/host5/target5:0:0/5:0:0:0':
    KERNELS=="5:0:0:0"
    SUBSYSTEMS=="scsi"
    DRIVERS=="sd"
    ATTRS{ioerr_cnt}=="0x0"
    ATTRS{iodone_cnt}=="0x1c"
    ATTRS{iorequest_cnt}=="0x1c"
    ATTRS{iocounterbits}=="32"
    ATTRS{timeout}=="30"
    ATTRS{state}=="running"
    ATTRS{rev}=="1.20"
    ATTRS{model}=="01GB Tiny       "
    ATTRS{vendor}=="Pretec  "
    ATTRS{scsi_level}=="3"
    ATTRS{type}=="0"
    ATTRS{queue_type}=="none"
    ATTRS{queue_depth}=="1"
    ATTRS{device_blocked}=="0"
    ATTRS{max_sectors}=="240"

  looking at parent device '/devices/pci0000:00/0000:00:02.2/usb1/1-5/1-5:1.0/host5/target5:0:0':
    KERNELS=="target5:0:0"
    SUBSYSTEMS==""
    DRIVERS==""

  looking at parent device '/devices/pci0000:00/0000:00:02.2/usb1/1-5/1-5:1.0/host5':
    KERNELS=="host5"
    SUBSYSTEMS==""
    DRIVERS==""

  looking at parent device '/devices/pci0000:00/0000:00:02.2/usb1/1-5/1-5:1.0':
    KERNELS=="1-5:1.0"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb-storage"
    ATTRS{modalias}=="usb:v4146pBA01d0100dc00dsc00dp00ic08isc06ip50"
    ATTRS{bInterfaceProtocol}=="50"
    ATTRS{bInterfaceSubClass}=="06"
    ATTRS{bInterfaceClass}=="08"
    ATTRS{bNumEndpoints}=="03"
    ATTRS{bAlternateSetting}==" 0"
    ATTRS{bInterfaceNumber}=="00"

  looking at parent device '/devices/pci0000:00/0000:00:02.2/usb1/1-5':
    KERNELS=="1-5"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{configuration}==""
    ATTRS{serial}=="14AB0000000096"
    ATTRS{product}=="USB Mass Storage Device"
    ATTRS{maxchild}=="0"
    ATTRS{version}==" 2.00"
    ATTRS{devnum}=="7"
    ATTRS{speed}=="480"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bDeviceProtocol}=="00"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bDeviceClass}=="00"
    ATTRS{bcdDevice}=="0100"
    ATTRS{idProduct}=="ba01"
    ATTRS{idVendor}=="4146"
    ATTRS{bMaxPower}==" 98mA"
    ATTRS{bmAttributes}=="80"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bNumInterfaces}==" 1"

  looking at parent device '/devices/pci0000:00/0000:00:02.2/usb1':
    KERNELS=="usb1"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{configuration}==""
    ATTRS{serial}=="0000:00:02.2"
    ATTRS{product}=="EHCI Host Controller"
    ATTRS{manufacturer}=="Linux 2.6.18-ARCH ehci_hcd"
    ATTRS{maxchild}=="6"
    ATTRS{version}==" 2.00"
    ATTRS{devnum}=="1"
    ATTRS{speed}=="480"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bDeviceProtocol}=="01"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bDeviceClass}=="09"
    ATTRS{bcdDevice}=="0206"
    ATTRS{idProduct}=="0000"
    ATTRS{idVendor}=="0000"
    ATTRS{bMaxPower}=="  0mA"
    ATTRS{bmAttributes}=="e0"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bNumInterfaces}==" 1"

  looking at parent device '/devices/pci0000:00/0000:00:02.2':
    KERNELS=="0000:00:02.2"
    SUBSYSTEMS=="pci"
    DRIVERS=="ehci_hcd"
    ATTRS{broken_parity_status}=="0"
    ATTRS{enable}=="1"
    ATTRS{modalias}=="pci:v000010DEd00000068sv00001043sd00000C11bc0Csc03i20"
    ATTRS{local_cpus}=="f"
    ATTRS{irq}=="17"
    ATTRS{class}=="0x0c0320"
    ATTRS{subsystem_device}=="0x0c11"
    ATTRS{subsystem_vendor}=="0x1043"
    ATTRS{device}=="0x0068"
    ATTRS{vendor}=="0x10de"

  looking at parent device '/devices/pci0000:00':
    KERNELS=="pci0000:00"
    SUBSYSTEMS==""
    DRIVERS==""

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

udevadm info -a -p `udevadm info -q path -n /dev/sda` | grep ATTRS{serial}

output:

   ATTRS{serial}=="14AB0000000096"
   ATTRS{serial}=="0000:00:02.2"

Hmm, two serials. Which one to use?

udevadm info -a -p `udevadm info -q path -n /dev/sda` | grep ATTRS{product}

and we get

   ATTRS{product}=="USB Mass Storage Device"
   ATTRS{product}=="EHCI Host Controller"

So, we need to use first serial.


Create a udev rule

You then use the ATTRS{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 You may, however create a file with a different name. Just remember that udev processes these files in alphabetical order.

BUS=="usb", ATTRS{serial}=="14AB0000000096", KERNEL=="sd?1", NAME="%k", SYMLINK+="usbdrive", GROUP="storage"


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 rw,noauto,group,flush,quiet,nodev,nosuid,noexec,noatime,dmask=000,fmask=111    0 0

Additionally, depending on your locale preferences, add something like codepage=866,iocharset=utf-8 to be able to see non-Latin filenames correctly.

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.

To allow non-root users to access to USB stick do

gpasswd -a user1 storage
gpasswd -a user2 storage

Restart udev

To load your updated rules you can run:

 udevadm control --reload-rules

Note: As of udev-1.30, the following is deprecated and needs to be updated. It probably won't work anymore.

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

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.

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

# 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 you have a device with with multiple partitions, the following example maps the device to /dev/usbdisk, and partitions 1, 2, 3 etc. to /dev/usbdisk1, /dev/usbdisk2, /dev/usbdisk3 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"

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

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
SUBSYSTEMS=="usb", ATTRS{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
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 my Packard Bell MP3 player to /dev/mp3player

# 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 your own usb key to /dev/mykey and all of other keys to /dev/otherkey

# 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 /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.


This is an example how to distinguish USB HDD drive 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.