home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / sg3-utils / examples / archive / rescan-scsi-bus.sh < prev   
Encoding:
Linux/UNIX/POSIX Shell Script  |  2004-05-07  |  2.6 KB  |  102 lines

  1. #!/bin/bash
  2. # Skript to rescan SCSI bus, using the 
  3. # scsi add-single-device mechanism
  4. # (w) 98/03/19 Kurt Garloff <kurt at garloff dot de> (c) GNU GPL
  5.  
  6. # Return hosts. /proc/scsi/HOSTADAPTER/? must exist
  7. findhosts ()
  8. {
  9.   hosts=
  10.   for name in /proc/scsi/*/?; do
  11.     name=${name#/proc/scsi/}
  12.     if test ! $name = scsi
  13.       then hosts="$hosts ${name#*/}"
  14.       echo "Host adapter ${name#*/} (${name%/*}) found."
  15.     fi
  16.   done
  17. }
  18.  
  19. # Test if SCSI device $host $channen $id $lun exists
  20. # Outputs description from /proc/scsi/scsi, returns new
  21. testexist ()
  22. {
  23.   grepstr="scsi$host Channel: 0$channel Id: 0*$id Lun: 0$lun"
  24.   new=`cat /proc/scsi/scsi|grep -e"$grepstr"`
  25.   if test ! -z "$new"
  26.     then cat /proc/scsi/scsi|grep -e"$grepstr"
  27.     cat /proc/scsi/scsi|grep -A2 -e"$grepstr"|tail -2|pr -o4 -l1
  28.   fi
  29. }
  30.  
  31. # Perform search (scan $host)
  32. dosearch ()
  33. {
  34.   for channel in $channelsearch; do
  35.     for id in $idsearch; do
  36.       for lun in $lunsearch; do
  37.         new=
  38.     devnr="$host $channel $id $lun"
  39.     echo "Scanning for device $devnr ..."
  40.     printf "OLD: "
  41.     testexist
  42.     if test ! -z "$remove" -a ! -z "$new"
  43.       then echo "scsi remove-single-device $devnr" >/proc/scsi/scsi
  44.       echo "scsi add-single-device $devnr" >/proc/scsi/scsi
  45.       printf "\r\x1b[A\x1b[A\x1b[AOLD: "
  46.       testexist
  47.       if test -z "$new"; then printf "\rDEL: \r\n\n\n\n"; let rmvd+=1; fi
  48.     fi
  49.     if test -z "$new"
  50.       then printf "\rNEW: "
  51.       echo "scsi add-single-device $devnr" >/proc/scsi/scsi
  52.       testexist
  53.       if test -z "$new"; then printf "\r\x1b[A"; else let found+=1; fi
  54.     fi
  55.       done
  56.     done
  57.   done
  58. }
  59.       
  60.   
  61. # main
  62. if test @$1 = @--help -o @$1 = @-h
  63.   then 
  64.     echo "Usage: rescan-scsi-bus.sh [-l] [-w] [-c] [host [host ...]]"
  65.     echo " -l activates scanning for LUNs 0 .. 7 [default: 0]"
  66.     echo " -w enables scanning for device IDs 0 .. 15 [def.: 0 .. 7]"
  67.     echo " -r enables removing of devices        [default: disabled]"
  68.     echo " -c enables scanning of channels 0 1   [default: 0]"
  69.     echo " If hosts are given, only these are scanned [default: all]"
  70.     exit 0
  71. fi
  72.  
  73. # defaults
  74. lunsearch="0"
  75. idsearch="0 1 2 3 4 5 6 7"
  76. channelsearch="0"
  77. remove=""
  78.  
  79. # Scan options
  80. opt="$1"
  81. while test ! -z "$opt" -a -z "${opt##-*}"; do
  82.   opt=${opt#-}
  83.   case "$opt" in
  84.     l) lunsearch="0 1 2 3 4 5 6 7" ;;
  85.     w) idsearch="0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" ;;
  86.     c) channelsearch="0 1" ;;
  87.     r) remove=1 ;;
  88.     *) echo "Unknown option -$opt !" ;;
  89.   esac
  90.   shift
  91.   opt="$1"
  92. done    
  93.  
  94. # Hosts given ?
  95. if test @$1 = @; then findhosts; else hosts=$*; fi
  96.  
  97. declare -i found=0
  98. declare -i rmvd=0
  99. for host in $hosts; do dosearch; done
  100. echo "$found new device(s) found.               "
  101. echo "$rmvd device(s) removed.                 "
  102.