Creating happy little block devices with UDEV

I’m a long time admirer of Bob Ross and the amazing paintings he produced on his hit tv show the joy of painting. I’m equally a fan of udev and the power it places in administrators hands. While Bob painted amazing clouds, seascapes and mountains with a swipe of his brush I’m able to make /dev just as beautiful with a few keyboard strokes (I guess that makes my keyboard my easel).

This past weekend I decided to clean up and automate the device creation process on several of my database servers. These servers had hundreds of mapper devices which were defined in multipath {} sections in /etc/multipath.conf. The aliases used in these blocks had upper and lower case names so the udev rules file had become a bit of a mess. Here is a snippet to illustrate:

multipath {
    wwid  09876543210
    alias happydev_lun001
}

multipath {
    wwid  01234567890
    alias Happydev_lun002
}

…. more devs ….

By default udev creates /dev/dm-[0-9]+ entries in /dev. I prefer to have the friendly names (happydev_lunXXX in this case) added as well so I don’t have to cross reference the two when I’m dealing with storage issues. To ensure that the friendly names in /dev were consistently named I created the following udev rule:

$ cd /etc/udev.rules.d && cat 90-happydevs.rules

ENV{DM_NAME}=="[Hh]appydev*", NAME="%c" OWNER:="bob", GROUP:="ross", MODE:="660", PROGRAM="/usr/local/bin/lc_devname.sh $env{DM_NAME}"

The rule above checks the DM_NAME to see if it starts with the string [Hh]appydev. If this check succeeds UDEV will call the /usr/local/bin/lc_devname.sh helper script which in turn calls tr to lower() the name passed as an argument. Here are the contents of the lc_devname.sh script:

$ cat /usr/local/bin/lc_devname.sh

#!/bin/sh

echo ${1} | tr '[A-Z]' '[a-z]'

The STDOUT from lc_devname.sh gets assigned to the %c variable which I reference in the NAME key to create a friendly named device in /dev. We can run udevadm test to make sure this works as expected:

$ udevadm test /block/dm-2 2>&1 | grep DEVNAME
udevadm_test: DEVNAME=/dev/happydev_lun001

In the spirit of Bob Ross I udev’ed some pretty little block devices. :)

Leave a Reply

Your email address will not be published. Required fields are marked *