Friday, 13 April 2012

Guide For UDEV rules

Guide For UDEV rules :
----------------------------------------------------

Whenever I insert a Usb stick the kernel is giving some device node to it which changes very often. So I thought of can I link it to a specific name. Then What I Learnt is UDEV.

 Udev - Linux dynamic device management
-----------------------------------------------------------------
GO to the steps for auto mount udev rules here. if you already have idea of udev.

Udev is to provide a userspace solution for a dynamic /dev directory, with persistent device naming. With the help Udev we can manage the permissions of the device node and can create additional symlinks in /dev directory .

We have to write some rules in order to create a device node (or) create a additional symlink (or) auto mount our usb to specific mount point. These are known as Udev rules.

  -->The Default rules are present in /lib/udev/rules.d/ directory
  -->The custom rules are present in /etc/udev/rules.d/ directory  :: This is the directory where we can write our own rules.

Udev expects its configuration file from /etc/udev/udev.conf.
*The rules files must be ended with .rules extension otherwise they are neglected.
*Every rule consists a series of key-value pairs separated by commas.

Match keys & Assignment keys are used in writing the rules.
Match keys are used to check for the conditions eg :: "==","!=".
Assignment keys are assigned with assigned values when rule is applied.

Basic Match keys:
 -->  KERNEL - match against the kernel name for the device
 -->  SUBSYSTEM - match against the subsystem of the device
 -->  DRIVER - match against the name of the driver backing the device
Basic Assignment keys:
  -->  NAME - the name that shall be used for the device node
  --> SYMLINK - a list of symbolic links which act as alternative names for the device node

here introducing a basic rule .
---------------------------------------------------------
KERNEL=="sdb1",NAME="vijay" 

In the above rule when a usb is inserted the kernel detects any device with name sdb1, instead of calling it sdb1 the device node is renamed as vijay

KERNEL=="sdb1",NAME="vijay",SYMLINK+="kumar",

This rule is same as the above but creates a extra link to vijay in /dev directory i.e /dev/kumar,/dev/vijay both points to same node.


Udevadm::
----------------------

make use of udevadm in order to know about the device Match keys.

udevadm info -a -p /sys/block/sdb :: I am using this path as I know the sysfs path of my /dev/sdb

or else make use of this

udevadm info -a -p $(udevadm info -q path -n /dev/sdb).

here is the sample log when I inserted a pen drive. it is detected on /dev/sdd .

  looking at device '/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.1/2-1.1:1.0/host46/target46:0:0/46:0:0:0/block/sdd/sdd1':
    KERNEL=="sdd1"
    SUBSYSTEM=="block"
    DRIVER==""
    ATTR{partition}=="1"
    ATTR{start}=="32"
    ATTR{size}=="15625184"
    ATTR{ro}=="0"
    ATTR{alignment_offset}=="0"
    ATTR{discard_alignment}=="0"
    ATTR{stat}=="     247        5     1939      176        0        0        0        0        0      176      176"
    ATTR{inflight}=="       0        0"

  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.1/2-1.1:1.0/host46/target46:0:0/46:0:0:0/block/sdd':
    KERNELS=="sdd"
    SUBSYSTEMS=="block"
    DRIVERS==""
    ATTRS{range}=="16"
    ATTRS{ext_range}=="256"
    ATTRS{removable}=="1"
   
  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.1/2-1.1:1.0/host46/target46:0:0/46:0:0:0':
    KERNELS=="46:0:0:0"
    SUBSYSTEMS=="scsi"
    DRIVERS=="sd"
    ATTRS{device_blocked}=="0"
    ATTRS{type}=="0"
    ATTRS{scsi_level}=="3"
    ATTRS{vendor}=="SanDisk "
    ATTRS{model}=="Cruzer Blade    "
    ATTRS{rev}=="1.00"
    ATTRS{state}=="running"
    ATTRS{timeout}=="30"
    ATTRS{iocounterbits}=="32"
   
  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.1/2-1.1:1.0/host46/target46:0:0':
    KERNELS=="target46:0:0"
    SUBSYSTEMS=="scsi"
    DRIVERS==""

  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.1/2-1.1:1.0/host46':
    KERNELS=="host46"
    SUBSYSTEMS=="scsi"
    DRIVERS==""

  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.1/2-1.1:1.0':
    KERNELS=="2-1.1:1.0"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb-storage"
    ATTRS{bInterfaceNumber}=="00"
    ATTRS{bAlternateSetting}==" 0"
    ATTRS{bNumEndpoints}=="02"

  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.1':
    KERNELS=="2-1.1"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{configuration}==""
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bmAttributes}=="80"
    ATTRS{bMaxPower}=="200mA"
    ATTRS{urbnum}=="910"
    ATTRS{idVendor}=="0781"
   
  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-1':
    KERNELS=="2-1"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{configuration}==""
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bmAttributes}=="e0"
    ATTRS{bMaxPower}=="  0mA"
    ATTRS{urbnum}=="1093"
    ATTRS{idVendor}=="8087"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{authorized}=="1"

  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2':
    KERNELS=="usb2"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{configuration}==""
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bmAttributes}=="e0"
    ATTRS{bMaxPower}=="  0mA"
    ATTRS{urbnum}=="24"
    ATTRS{idVendor}=="1d6b"
    ATTRS{authorized_default}=="1"

  looking at parent device '/devices/pci0000:00/0000:00:1d.0':
    KERNELS=="0000:00:1d.0"
    SUBSYSTEMS=="pci"
    DRIVERS=="ehci_hcd"
    ATTRS{vendor}=="0x8086"
    ATTRS{device}=="0x1c26"
    ATTRS{subsystem_vendor}=="0x17aa"
    ATTRS{broken_parity_status}=="0"
    ATTRS{msi_bus}==""
    ATTRS{companion}==""

  looking at parent device '/devices/pci0000:00':
    KERNELS=="pci0000:00"
    SUBSYSTEMS==""
    DRIVERS==""

I Have deleted some log.

Make us of the above info for writing the rules.
eg:: make of KERNEL,KERNELS,SUBSYTEMS,
we can combine the attributes from the device in question and a single parent device, we cannot mix-and-match attributes from multiple parent devices - your rule will not work

eg :: SUBSYSTEM=="block",SUBSYSTEMS=="scsi",ATTRS{vendor}=="SanDisk
  (this is from 1st),        (this is frm 3rd),             (this is frm 3rd)
",SYMLINK+="vijay" -----> This rule will work
     
      SUBSYSTEM=="block",SUBSYSTEMS=="scsi",ATTRS{vendor}=="0x8086",SYMLINK+="vijay"
   (this is from 1st),       (this is frm 3rd),             (this is from last)
-------> this will not work




Here is the picture of the usb I inserted & by using udevadm got the kernels





ACTION=="add",KERNEL=="sd?[1-9]",KERNELS=="2-1.1",SUBSYSTEM=="block",SUBSYSTEMS=="usb",SYMLINK+="usb1",RUN+="/etc/pd.sh usb1"
ACTION=="remove",KERNEL=="sd?[1-9]",SUBSYSTEM=="block",SUBSYSTEMS=="usb",RUN+="/etc/pdut.sh usb1"


These are the rules I have written here KERNELS relate to my usb slot number,Run is nothing but it runs th program(script)
So whenever I connect to this particula port a symlink for my port is created with name usb1 in /dev and the data in it is mounted to /var/usb1

Got the solution what I need. ie to auto-mount the usbdrive to one particular location & creating a node in /dev directory.

Include these lines when connected to 2nd port i.e green one and to mount the data to /var/usb2

ACTION=="add",KERNEL=="sd?[1-9]",KERNELS=="2-1.2",SUBSYSTEM=="block",SUBSYSTEMS=="usb",SYMLINK+="usb2",RUN+="/etc/pd.sh usb2"
ACTION=="remove",KERNEL=="sd?[1-9]",SUBSYSTEM=="block",SUBSYSTEMS=="usb",RUN+="/etc/pdut.sh usb2"

GO to the steps for auto mount udev rules here.

Regards,
VJ.

No comments:

Post a Comment

Views