Udev rules to create a /dev node when inserting a module


Udev rules to create a /dev node when inserting a module

Open a terminal and run the following command

$ sudo udevadm monitor --environment

And in another terminal insmod and rmmod the module.

thalib@SilentStorm:$ sudo insmod par.ko
thalib@SilentStorm:$ sudo rmmod par.ko

Now check the output at the termial where you ran the udevadm as below.

thalib@SilentStorm:$ sudo udevadm monitor --environment
udevmonitor will print the received events for:
UDEV the event which udev sends out after rule processing
UEVENT the kernel uevent

UEVENT[1262407545.812162] add      /module/par (module)
ACTION=add
DEVPATH=/module/par
SUBSYSTEM=module
SEQNUM=1282

UEVENT[1262407650.594942] remove   /module/par (module)
ACTION=remove
DEVPATH=/module/par
SUBSYSTEM=module
SEQNUM=1283

Now from the above output we will create udev rules.

Copy the output from the udevadm command as below. we will use this info to create udev rules

ACTION=add
DEVPATH=/module/par
SUBSYSTEM=module

ACTION=remove
DEVPATH=/module/par
SUBSYSTEM=module

Create a file in /etc/udev/rules.d with similar name as below

thalib@SilentStorm:$ sudo vim /etc/udev/rules.d/96-thalib.rules

The modified content is below

ACTION=="add", DEVPATH=="/module/par" SUBSYSTEM=="module" RUN="/home/thalib/sctipts/par_add.sh"

ACTION=="remove", DEVPATH=="/module/par" SUBSYSTEM=="module" RUN="/home/thalib/sctipts/par_rem.sh"

RUN is used to execute a file, here i am using my own scripts with below content

thalib@SilentStorm:$ cat /home/thalib/sctipt/par_add.sh
#!/bin/sh

mknod /dev/par c 200 0

thalib@SilentStorm:$ cat /home/thalib/sctipt/spar_rem.sh
#!/bin/sh

rm /dev/par

now I think we are done. if you didn’t enable inotifiy in the kernel configuration the you have to restart the udev daemon


thalib@SilentStorm:$ /etc/init.d/udev restart

simply insmod and rmmod the module the /dev/par node will be automatically created and deleted.

thalib@SilentStorm:$ ls /dev/par
ls: cannot access /dev/par: No such file or directory
thalib@SilentStorm:$ sudo insmod par.ko
thalib@SilentStorm:$ ls /dev/par -l
crw-r--r-- 1 root root 200, 0 2010-01-02 10:31 /dev/par
thalib@SilentStorm:$ sudo rmmod par
thalib@SilentStorm:$ ls /dev/par -l
ls: cannot access /dev/par: No such file or directory
thalib@SilentStorm:$

Reference
1. Udev manpages
2. http://www.reactivated.net/writing_udev_rules.html
3. http://hackaday.com/2009/09/18/how-to-write-udev-rules/

Hope it will be useful. All the best.

5 thoughts on “Udev rules to create a /dev node when inserting a module

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s