Search Members Help

» Welcome Guest
[ Log In :: Register ]

Mini-ITX Boards Sale, Fanless BareBones Mini-ITX, Bootable 1G DSL USBs, 533MHz Fanless PC <-- SALE $200 each!
Get The Official Damn Small Linux Book. DSL Market , Great VPS hosting provided by Tektonic

[ Track this topic :: Email this topic :: Print this topic ]

reply to topic new topic new poll
Topic: rescan script for USB/SCSI< Next Oldest | Next Newest >
CappyCaffeine Offline

Group: Members
Posts: 64
Joined: Jan. 2004
Posted: Oct. 30 2004,06:29 QUOTE

I found the following script to rescan USB devices like pendrives if you insert them after you have your computer on.

# ---------------------Start Cut ---------
# Skript to rescan SCSI bus, using the
# scsi add-single-device mechanism
# (w) 1998-03-19 Kurt Garloff <> © GNU GPL
# (w) 2003-07-16 Kurt Garloff <> © GNU GPL
# $Id:,v 1.11 2003/10/24 21:15:22 garloff Exp $

setcolor ()

unsetcolor ()
 red=""; green=""
 yellow=""; norm=""

# Return hosts. /proc/scsi/HOSTADAPTER/? must exist
findhosts ()
 for driverdir in /proc/scsi/*; do
   [ $driver = scsi -o $driver = sg -o $driver = dummy ] && continue
   for hostdir in $driverdir/*; do
     [ $name = add_map -o $name = map -o $name = mod_parm ] && continue
     if [ -r $hostdir/status ]; then
num=$(printf '%d\n' `sed -n 's/SCSI host number://p' $hostdir/status`)
     hosts="$hosts $num"
     echo "Host adapter $num ($driverinfo) found."

# Test if SCSI device $host $channen $id $lun exists
# Outputs description from /proc/scsi/scsi, returns new
testexist ()
 grepstr="scsi$host Channel: 0*$channel Id: 0*$id Lun: 0*$lun"
 new=`cat /proc/scsi/scsi | grep -e"$grepstr"`
 if test ! -z "$new"; then
   cat /proc/scsi/scsi | grep -e"$grepstr"
   cat /proc/scsi/scsi | grep -A2 -e"$grepstr" | tail -n2 | pr -o4 -l1

# Perform search (scan $host)
dosearch ()
 for channel in $channelsearch; do
   for id in $idsearch; do
     for lun in $lunsearch; do
devnr="$host $channel $id $lun"
echo "Scanning for device $devnr ..."
printf "${yellow}OLD: $norm"
if test ! -z "$remove" -a ! -z "$new"; then
 # Device exists and we're in remove mode, so remove and readd
 echo "scsi remove-single-device $devnr" >/proc/scsi/scsi
 echo "scsi add-single-device $devnr" >/proc/scsi/scsi
 printf "\r\x1b[A\x1b[A\x1b[A${yellow}OLD: $norm"
 if test -z "$new"; then
   printf "\r${red}DEL: $norm\r\n\n\n\n"; let rmvd+=1;
if test -z "$new"; then
 # Device does not exist, try to add
 printf "\r${green}NEW: $norm"
 echo "scsi add-single-device $devnr" >/proc/scsi/scsi
 if test -z "$new"; then
   # Device not present
   printf "\r\x1b[A";
     # Optimization: if lun==0, stop here (only if in non-remove mode)
   if test $lun = 0 -a -z "$remove" -a $optscan = 1; then
   let found+=1;

# main
if test @$1 = @--help -o @$1 = @-h -o @$1 = @-?; then
   echo "Usage: [options] [host [host ...]]"
   echo "Options:"
   echo " -l activates scanning for LUNs 0-7    [default: 0]"
   echo " -w scan for target device IDs 0 .. 15 [default: 0-7]"
   echo " -c enables scanning of channels 0 1   [default: 0]"
   echo " -r enables removing of devices        [default: disabled]"
   echo "--remove:        same as -r"
   echo "--nooptscan:     don't stop looking for LUNs is 0 is not found"
   echo "--color:         use coloured prefixes OLD/NEW/DEL"
   echo "--hosts=LIST:    Scan only host(s) in LIST"
   echo "--channels=LIST: Scan only channel(s) in LIST"
   echo "--ids=LIST:      Scan only target ID(s) in LIST"
   echo "--luns=LIST:     Scan only lun(s) in LIST"  
   echo " Host numbers may thus be specified either directly on cmd line (deprecated) or"
   echo " or with the --hosts=LIST parameter (recommended)."
   echo "LIST: A[-B][,C[-D]]... is a comma separated list of single values and ranges"
   echo " (No spaces allowed.)"
   exit 0

expandlist ()
   while test ! -z "$first"; do
if test "$beg" = "$first"; then
   result="$result $beg";
   result="$result `seq $beg $end`"
test "$rest" = "$first" && rest=""
   echo $result

# defaults
idsearch=`seq 0 7`

# Scan options
while test ! -z "$opt" -a -z "${opt##-*}"; do
 case "$opt" in
   l) lunsearch=`seq 0 7` ;;
   w) idsearch=`seq 0 15` ;;
   c) channelsearch="0 1" ;;
   r) remove=1 ;;
   -remove)      remove=1 ;;
   -hosts=*)     arg=${opt#-hosts=};   hosts=`expandlist $arg` ;;
   -channels=*)  arg=${opt#-channels=};channelsearch=`expandlist $arg` ;;
   -ids=*)   arg=${opt#-ids=};         idsearch=`expandlist $arg` ;;
   -luns=*)  arg=${opt#-luns=};        lunsearch=`expandlist $arg` ;;
   -color) setcolor ;;
   -nooptscan) optscan=0 ;;
   *) echo "Unknown option -$opt !" ;;

# Hosts given ?
if test @$1 != @; then

echo "Scanning hosts $hosts channels $channelsearch for "
echo " SCSI target IDs " $idsearch ", LUNs " $lunsearch
test -z "$remove" || echo " and remove devices that have disappeared"
declare -i found=0
declare -i rmvd=0
for host in $hosts; do
echo "$found new device(s) found.               "
echo "$rmvd device(s) removed.                 "

# -------------------------------- End Cut Here! ----

Back to top
Profile PM MSN 
CappyCaffeine Offline

Group: Members
Posts: 64
Joined: Jan. 2004
Posted: Oct. 30 2004,06:30 QUOTE

Maybe to include on the tools menu or Control Panel in DSL? Hmm?

Back to top
Profile PM MSN 
1 replies since Oct. 30 2004,06:29 < Next Oldest | Next Newest >

[ Track this topic :: Email this topic :: Print this topic ]

reply to topic new topic new poll
Quick Reply: rescan script for USB/SCSI

Do you wish to enable your signature for this post?
Do you wish to enable emoticons for this post?
Track this topic
View All Emoticons
View iB Code