Pages

Thursday, 28 July 2016

Writing your own udev rules with CentOS 7

Introduced with the 2.6 Linux kernel udev is a system that will automatically map devices that are plugged in such as hard drives, usb drives and so on and will create an entry in the dev filesystem.

There are a collection of pre-defined rules that udev uses to decide how devices will be presented and any other actions needed. These are located within:

/etc/udev/rules.d

In the below example I will be automatically mapping a specific usb mass storage device to /dev/spusb, mounting it and then executing a custom script.

Firstly we should collect some information about the device - we do this using the udevadm command:

udevadm info -a /dev/sdb

Which should return something like:
P: /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3:1.0/host8/target8:0:0/8:0:0:0/block/sdb
N: sdb
S: disk/by-id/usb-090c_1000_AA04012700008713-0:0
S: disk/by-path/pci-0000:00:14.0-usb-0:3:1.0-scsi-0:0:0:0
E: DEVLINKS=/dev/disk/by-id/usb-090c_1000_AA04012700008713-0:0
.................
What we are really after is some way of uniquely identifying the device - for example we might use the 'SERIAL' value:

AA04012700008713

Now lets create a new rule within the rules directory - although keep in mind there is a file naming convention in place - e.g.:

Rule files beginning with '60' will override any default rules and rule files beginning with '90' (or above) should be run last - refer to the man pages for more information.

KERNEL=="sd?1", ATTRS{serial}=="AA04012700008713", SYMLINK+="myspecialusb" RUN+="/usr/local/scripts/test.sh"

UDEV rules should be picked up automatically since it uses the ionotify function to detect changes in the /etc/udev/rules.d folder - although if you wish to manually force a reload (without rebooting) you can issue:

udevadm control --reload-rules

Now simply re-insert the usb drive - use dmesg to help debug any problems.

Sources:

http://unix.stackexchange.com/questions/39370/how-to-reload-udev-rules-without-reboot

No comments:

Post a Comment