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.

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:

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).

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:

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+="".

I agree with VxJasonxV. So far I have written my udev rules to create nodes like /dev/tv0 /dev/tv1 etc based on the harddisks. only to use those nodes with autofs. but I will use the /dev/disk/by* from now on because it makes life easier and takes out the middle man (writing the udev rules).
Having said that of course it is nice to have very descripting node names.

hi!,I really like your writing so so much! proportion we communicate more about your post on AOL? I need a specialist on this space to unravel my problem. Maybe that’s you! Looking ahead to see you. dadfkdaebdfe

Welcome to linuxtidbits.

linuxtidbits is a place for common bits of Linux knowledge. Linux is a good operating system with good people. Being part of Linux, to me, means being a part of something that everyone can contribute to.