Friday, 13 April 2012

Steps for automount usb by udev rules

Steps for automount usb by udev rules.
======================================

--> Go to /etc/udev/rules.d/ directory

--> create a file with .rules extension eg :: vijay.rules etc

--> Copy the below code to your file

   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"
   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"

--> save and exit

--> go to /etc directory

--> create a file with name pd.sh

--> copy the below code to your file

     #!/bin/sh

     sudo mkdir -p /var/$1
     sudo mount /dev/$1 /var/$1

--> Save and exit
--> chmod +x pd.sh

--> create a file with name pdut.sh

--> copy the below code to you file

    #!/bin/sh

    sudo umount /var/$1
    sudo rm -rf /var/$1

--> Save and exit
--> chmod +x pdut.sh

--> Reload the rules with command ::
  udevadm control --reload-rules or  udevcontrol --reload_rules(old Kernels)
--> From next time if you plug in usb these rules are applied

--> Please make the changes wrt your pc in
     KERNELS :: please change this as per the usb slot you want to work on
   & KERNEL :: if no partition is present i.e only sdd or sdb etc.... if it has sdd1 or sdb1 you can work normally.

=================================================
If You are not interested in kernels and if you are not stick to one mount point you can use these lines

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

these two lines are enough which can mount any number of usb devices connected to any slot of the pc. pd.sh & pdut.sh are same

but the drawback is the mountpoint name changes

eg :: if 1st usb is connected and if kernel detects it as sdb1 then symlink usbsdb1 is created and mountpoint is /var/usbsdb1

      if 2nd usb is connected and if  kernel detects it as sdc1 then symlink usbsdbc1 is created and mountpoint is /var/usbsdc1 etc....

VJ

No comments:

Post a Comment

Views