Wednesday, April 18, 2012

automount usb drive and execute a script in embedded linux using udev

Today let us see how to use udev for automounting of a usb pendrive and execute some scripts to copy files..


For reference use the link given here.


Lets get started
  • Ensure that udev feature was enabled during kernel configuration. Alternatively you can use mdev from busybox for same purpose. But here we see examples for udev.
  • For udev we must write rules for what udev must do when a particular device is mounted. For finer understanding of udev please read the man pages.
  • Make a entry in /etc/udev/rules.d/  with a file name like "10-local.rules". This is because each rules are applied in lexical order and we have to ensure our rules load before the default the default rules.
    • PS. See the default rule to have an idea on how to write a rule
  • i wrote my rule like this:-
    • SUBSYSTEMS=="usb",DRIVERS=="usb",SYMLINK+="test_usb",RUN+="/home/usb_mount.sh"
  • the values for writing this can be got from executing the command                                              udevinfo -a -p /sys/block/sda for all sda device
  • A new device file /dev/test_usb is created. the script usb_mount.sh will contain the script  to execute like  mounting and copying file. 
    
    
    
    
    NOTE:Please do not use the command like 'cat' to test the script as it may not print on         console (given in refference). Instead use echo >>(redirection) text file to debug and test
    
    
    this is the script i wrote
    
    
    
    #!/bin/sh
    logfile=/home/usb_txt
    if [ "${ACTION}" = "add" ]; then 
            #mount as vfat partion
     mount -t vfat -o umask=777 /dev/cdac_usb /home/disk
          #to copy all contents of the file to an array and each can be accessed as ${variable[i]} 
     file1=($(cat /home/igate/pass.txt))
     file2=($(cat /home/disk/test.txt))
     echo password in file 1:${file1[0]}>>$logfile
     echo password in file 2:${file2[0]}>>$logfile
     if [ ${file1[0]} = ${file2[0]} ]; then
         echo 'password authenticated'>>$logfile
     for i in 1 2
     do
      ls /home/disk/${file2[$i]}
           if [ $? == 0 ]; then
              echo "file found" >>$logfile
    #this  portion ensures that the program is killed before copying 
        if (ps -A | grep ${file2[$i]} 1>/dev/null 2>/dev/null)
        then
               killall -KILL ${file2[$i]} 1>/dev/null 2>/dev/null
        echo "Stopping " ${file2[$i]}>>$logfile
        else
        echo "Failed to kill" ${file2[$i]}>>$logfile
        fi
        sleep 2
        cp -f /home/disk/${file2[$i]} /home/igate/${file2[$i]}
        if [ $? == 0 ]; then
               echo "file found" >>$logfile
        fi
       else
              echo "file not found">>$logfile
          fi
     done
     else
         echo 'password failed'>>$logfile
     fi
    umount /home/disk
    fi
    
    
    Script working:-
    It first stores all the values in the text files in arrays file1 and file2. Then it checks whether program it plans to update is not running before copying the file. All this is recorded in th ffile usb_log.txt
     
    
    
    
    
    
    
    
    
    
    
    
    
  

1 comment:

Post a Comment