Sunday, October 16, 2011

[转] Creating a Unique Device Node

Creating a unique device node may be needed because the kernel assigns device nodes names, for some devices, in a non-static manner. When the kernel adds a device node, naming can appear random (i.e. /dev/sda1, /dev/sdb1…) each time the device is plugged in – particularly with dynamic hardware. Hardware can range from USB flash drives, hard drives, cameras, but can include non-usb devices as well. Rules can be created with udev that can assign unique device nodes to each device.

Benefits of Unique Device Nodes

Having unique device nodes is particularly useful for bash scripts to backup to a specific external hard drive, or copy mp3 files to an mp3 player. It also can make using UUID’s in fstab unnecessary – see Getting Gnome Volume Manager to Play Nice.

Getting the Kernel Device Node for USB Devices

For USB storage devices (memory sticks, external drives, mp3 players…) find the kernel device node by plugging it in and running:
$ fdisk -l
...
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1        2010      996944+   6  FAT16
Omitting the partition number, the kernel defined device node for this storage device is /dev/sdb. Looking at the tail end of ‘dmesg‘ may give the device node. To get USB printer kernel defined device nodes look in the /dev directory for ‘/dev/lpX‘ entries.

Getting Info from the Device

Use udevadm to get information about the device. With this information, a udev rule can be created to give the device a unique id (device-node).
udevadm info -a -p `udevadm info -q path -n /dev/sda`
In the ouput of ‘udevadm‘ look for unique keys in the section for the device. The example here is for an mp3-player:
    ATTRS{manufacturer}=="SanDisk Corporation"
    ATTRS{product}=="U3 Cruzer Micro"
    ATTRS{serial}=="0000176C6960E789"
All devices will be different, if these keys don’t exist, look for idVendor and idProduct.

Creating a udev Rule

With these keys it’s easy to create a unique rule that applies to the device. Udev keeps it’s rules in /etc/udev/rules.d/. New rules are to be put in /etc/udev/rules.d/10-local.rules (create the file if it doesn’t already exist).
sudo vim /etc/udev/rules.d/10-local.rules
# /etc/udev/rules.d/10-local.rules
#
# udev rules for local
Rules are in the form of:
key, [key,...] NAME [, SYMLINK]
All thats needed is a single unique key followed by the name wanted for the device node (‘NAME=""‘). ‘SYMLINK is optional but can be used to create an extra node (e.g. printers/hp_220). It’s a good idea to have ‘NAME=""‘ as the kernel named device node as some programs look for it. Also it’s a good idea to have a key defining the parent device (BUS=="usb" below) to help Gnome and KDE volume managers.
In defining the rule use double equal mark for compare keys and a single equal mark to assign values. I like to use product and serial keys:
# SanDisk U3 Cruzer Micro - 1 GB Flash Drive
BUS=="usb", ATTRS{product}=="U3 Cruzer Micro", \
ATTRS{serial}=="0000176C6960E789", \
NAME="%k", SYMLINK="sandisk_u3_cruzer_micro"
For older versions of udev “SYSFS” is used in place of “ATTRS“.
Notice the use of ‘%k‘ for the ‘NAME=""‘ value. Using ‘%k‘ designates to use the kernel named device node. Another key to use if “udevadm info...” doesn’t give much info is KERNEL=="". Define the kernel device node generically with KERNEL=="sd*" or whatever the kernel device node begins with.
With the udev rule, programs can also be told to run a script or program with RUN+="".
# SanDisk U3 Cruzer Micro - 1 GB Flash Drive
BUS=="usb", ATTRS{product}=="U3 Cruzer Micro", \
ATTRS{serial}=="0000176C6960E789", \
NAME="%k", SYMLINK="sandisk_u3_cruzer_micro" \
RUN+="/home/dirk/.bin/configuration-backup"
To test if the device node is created, save the file, unplug the device and plug it back in.

Easy Method of Creating a Device Node for a USB Device

From Gentoo Wiki Archives.
emerge sys-apps/usbutils
Output devices using USB:
lsusb              idVendor:idProduct
Bus 001 Device 005: ID 0781:7400 SanDisk Corp.
Bus 001 Device 001: ID 1d6b:0002
Bus 002 Device 004: ID 046d:c03d Logitech, Inc.
Bus 002 Device 001: ID 1d6b:0001
lsusb‘ displays “idVendor” and “idProduct” keys. Using these two keys together will create a unique enough rule:
sudo vim /etc/udev/rules.d/10-local.rules

# SanDisk Sansa m240 mp3 player
BUS=="usb", ATTRS{idVendor}=="0781", ATTRS{idProduct}=="7400", \
NAME="sandisk_sansa_m240"

/etc/fstab

Remember to add the new device nodes to the fstab file.
References:

No comments: