# Script to rescan SCSI bus, using the scsi add-single-device mechanism.
# (c) 1998--2010 Kurt Garloff <kurt@garloff.de>, GNU GPL v2 or v3
# (c) 2006--2018 Hannes Reinecke, GNU GPL v2 or later
# $Id: rescan-scsi-bus.sh,v 1.57 2012/03/31 14:08:48 garloff Exp $
SCAN_WILD_CARD=4294967295
if [ $debug -eq 1 ] ; then
# Output some text and return cursor to previous position
# (only works for simple strings)
# Stores length of string in LN and returns it
while test $cntr -lt $LN; do BK="$BK\e[D"; let cntr+=1; done
# Overwrite a text of length $LN with whitespace
while test $cntr -lt $LN; do BK="$BK\e[D"; WH="$WH "; let cntr+=1; done
# Return hosts. sysfs must be mounted
hosts=`find /sys/class/scsi_host/host* -maxdepth 4 -type d -o -type l 2> /dev/null | awk -F'/' '{print $5}' | sed -e 's~host~~' | sort -nu`
scsi_host_data=`echo "$hosts" | sed -e 's~^~/sys/class/scsi_host/host~'`
for hostdir in $scsi_host_data; do
hostno=${hostdir#/sys/class/scsi_host/host}
if [ -f $hostdir/isp_name ] ; then
elif [ -f $hostdir/lpfc_drvr_version ] ; then
hostname=`cat $hostdir/proc_name`
echo_debug "Host adapter $hostno ($hostname) found."
if [ -z "$hosts" ] ; then
echo "No SCSI host adapters found in sysfs"
# Not necessary just use double quotes around variable to preserve new lines
#hosts=`echo $hosts | tr ' ' '\n'`
# Return hosts. /proc/scsi/HOSTADAPTER/? must exist
for driverdir in /proc/scsi/*; do
driver=${driverdir#/proc/scsi/}
if test $driver = scsi -o $driver = sg -o $driver = dummy -o $driver = device_info; then continue; fi
for hostdir in $driverdir/*; do
name=${hostdir#/proc/scsi/*/}
if test $name = add_map -o $name = map -o $name = mod_parm; then continue; fi
if test -r "$hostdir/status"; then
num=$(printf '%d\n' "$(sed -n 's/SCSI host number://p' "$hostdir/status")")
driverinfo="$driver:$name"
echo "Host adapter $num ($driverinfo) found."
0) echo "Direct-Access" ;;
1) echo "Sequential-Access" ;;
7) echo "Optical-Device" ;;
8) echo "Medium-Changer" ;;
9) echo "Communications" ;;
14) echo "Direct-Access-RBC" ;;
# Get /proc/scsi/scsi info for device $host:$channel:$id:$lun
# Optional parameter: Number of lines after first (default = 2),
# result in SCSISTR, return code 1 means empty.
if test -z "$1"; then LN=2; else LN=$1; fi
CHANNEL=`print02i "$channel"`
if [ -d /sys/class/scsi_device ]; then
SCSIPATH="/sys/class/scsi_device/${host}:${channel}:${id}:${lun}"
if [ -d "$SCSIPATH" ] ; then
SCSISTR="Host: scsi${host} Channel: $CHANNEL Id: $ID Lun: $LUN"
if [ "$LN" -gt 0 ] ; then
IVEND=$(cat ${SCSIPATH}/device/vendor)
IPROD=$(cat ${SCSIPATH}/device/model)
IPREV=$(cat ${SCSIPATH}/device/rev)
SCSIDEV=$(printf ' Vendor: %-08s Model: %-16s Rev: %-4s' "$IVEND" "$IPROD" "$IPREV")
if [ "$LN" -gt 1 ] ; then
ILVL=$(cat ${SCSIPATH}/device/scsi_level)
type=$(cat ${SCSIPATH}/device/type)
SCSITMP=$(printf ' Type: %-17s ANSI SCSI revision: %02d' "$ITYPE" "$((ILVL - 1))")
grepstr="scsi$host Channel: $CHANNEL Id: $ID Lun: $LUN"
SCSISTR=$(grep -A "$LN" -e "$grepstr" /proc/scsi/scsi)
if test -z "$SCSISTR"; then return 1; else return 0; fi
# Find sg device with 2.6 sysfs support
gendev=/sys/class/scsi_device/${host}:${channel}:${id}:${lun}/device/generic
if test -e "$gendev"; then
SGDEV=$(basename "$(readlink "$gendev")")
for SGDEV in /sys/class/scsi_generic/sg*; do
DEV=`readlink $SGDEV/device`
if test "${DEV##*/}" = "$host:$channel:$id:$lun"; then
SGDEV=`basename $SGDEV`; return
# Find sg device with 2.4 report-devs extensions
SGDEV=`echo "$SCSISTR" | grep 'Attached drivers:' | sed 's/^ *Attached drivers: \(sg[0-9]*\).*/\1/'`
# Find sg device that belongs to SCSI device $host $channel $id $lun
if test -d /sys/class/scsi_device; then
DRV=`grep 'Attached drivers:' /proc/scsi/scsi 2>/dev/null`
if [ $repdevstat = 0 ]; then
echo "scsi report-devs 1" >/proc/scsi/scsi
DRV=`grep 'Attached drivers:' /proc/scsi/scsi 2>/dev/null`
if [ $? = 1 ]; then return; fi
if ! echo "$DRV" | grep -q 'drivers: sg'; then
if [ $repdevstat = 0 ]; then
echo "scsi report-devs 0" >/proc/scsi/scsi
# Whether or not the RMB (removable) bit has been set in the INQUIRY response.
# Uses ${host}, ${channel}, ${id} and ${lun}. Assumes that sg_device() has
# already been called. How to test this function: copy/paste this function
# (cd /sys/class/scsi_device && for d in *; do set ${d//:/ }; echo -n "$d $(</sys/class/scsi_device/${d}/device/block/*/removable) <> "; SGDEV=bsg/$d host=$1 channel=$2 id=$3 lun=$4 is_removable; done)
p=/sys/class/scsi_device/${host}:${channel}:${id}:${lun}/device/inquiry
# Extract the second byte of the INQUIRY response and check bit 7 (mask 0x80).
b=$(od -tx1 -j1 -N1 "$p" 2>/dev/null |
{ read -r _ byte rest; echo -n "$byte"; })
echo $(((0x$b & 0x80) != 0))
sg_inq /dev/$SGDEV 2>/dev/null | sed -n 's/^.*RMB=\([0-9]*\).*$/\1/p'
# Test if SCSI device is still responding to commands
# 2 device has been removed
if test ! -x /usr/bin/sg_turs; then return 0; fi
if test -z "$SGDEV"; then return 0; fi
sg_turs /dev/$SGDEV >/dev/null 2>&1
# Handle in progress of becoming ready and unit attention
while test $RC = 2 -o $RC = 6 && test $ctr -le 30; do
if test $RC = 2 -a "$RMB" != "1"; then echo -n "."; let LN+=1; sleep 1
sg_turs /dev/$SGDEV >/dev/null 2>&1
# Check for removable device; TEST UNIT READY obviously will
# fail for a removable device with no medium
print_and_scroll_back "$host:$channel:$id:$lun $SGDEV ($RMB) "
test $RC = 2 -a "$RMB" = "1" && break
if test $ctr != 0; then white_out; fi
# echo -e "\e[A\e[A\e[A${yellow}Test existence of $SGDEV = $RC ${norm} \n\n\n"
if test $RC = 1; then return $RC; fi
# Reset RC (might be !=0 for passive paths)
# OK, device online, compare INQUIRY string
INQ=`sg_inq $sg_len_arg /dev/$SGDEV 2>/dev/null`
echo -e "\e[A\e[A\e[A\e[A${red}$SGDEV changed: ${bold}INQUIRY failed${norm} \n\n\n"
IVEND=`echo "$INQ" | grep 'Vendor identification:' | sed 's/^[^:]*: \(.*\)$/\1/'`
IPROD=`echo "$INQ" | grep 'Product identification:' | sed 's/^[^:]*: \(.*\)$/\1/'`
IPREV=`echo "$INQ" | grep 'Product revision level:' | sed 's/^[^:]*: \(.*\)$/\1/'`
STR=`printf " Vendor: %-08s Model: %-16s Rev: %-4s" "$IVEND" "$IPROD" "$IPREV"`
IPTYPE=`echo "$INQ" | sed -n 's/.* Device_type=\([0-9]*\) .*/\1/p'`
IPQUAL=`echo "$INQ" | sed -n 's/ *PQual=\([0-9]*\) Device.*/\1/p'`
if [ "$IPQUAL" != 0 ] ; then
[ -z "$IPQUAL" ] && IPQUAL=3
[ -z "$IPTYPE" ] && IPTYPE=31
echo -e "\e[A\e[A\e[A\e[A${red}$SGDEV changed: ${bold}LU not available (PQual $IPQUAL)${norm} \n\n\n"
TYPE=$(printtype $IPTYPE)
echo -e "\e[A\e[A\e[A\e[A${red}$SGDEV removed.\n\n\n"
TMPSTR=`echo "$SCSISTR" | grep 'Vendor:'`
if test $ignore_rev -eq 0 ; then
if [ "$TMPSTR" != "$STR" ]; then
echo -e "\e[A\e[A\e[A\e[A${red}$SGDEV changed: ${bold}\nfrom:${SCSISTR#* } \nto: $STR ${norm} \n\n\n"
# Ignore disk revision change
old_str_no_rev=`echo "$TMPSTR" | sed -e 's/.\{4\}$//'`
new_str_no_rev=`echo "$STR" | sed -e 's/.\{4\}$//'`
if [ "$old_str_no_rev" != "$new_str_no_rev" ]; then
echo -e "\e[A\e[A\e[A\e[A${red}$SGDEV changed: ${bold}\nfrom:${SCSISTR#* } \nto: $STR ${norm} \n\n\n"
TMPSTR=`echo "$SCSISTR" | sed -n 's/.*Type: *\(.*\) *ANSI.*/\1/p' | sed 's/ *$//g'`
if [ "$TMPSTR" != "$TYPE" ] ; then
echo -e "\e[A\e[A\e[A\e[A${red}$SGDEV changed: ${bold}\nfrom:${TMPSTR} \nto: $TYPE ${norm} \n\n\n"
# Test if SCSI device $host $channel $id $lun exists
# Outputs description from /proc/scsi/scsi (unless arg passed)
# Returns SCSISTR (empty if no dev)
if procscsiscsi && test -z "$1"; then
echo "$SCSISTR" | head -n1
echo "$SCSISTR" | tail -n2 | pr -o4 -l1
# Returns the list of existing channels per host
for dev in /sys/class/scsi_device/${host}:* ; do
for tmpchan in $channelsearch ; do
if test "$chan" -eq $tmpchan ; then
if test -n "$chan" ; then
channelsearch="$channelsearch $chan"
if test -z "$channelsearch"; then channelsearch="0"; fi
# Returns the list of existing targets per host
oldlist=$(ls /sys/class/scsi_device/ | sed -n "s/${host}:${channel}:\([0-9]*:[0-9]*\)/\1/p" | uniq)
# Rescan LUN 0 to check if we found new targets
echo "${channel} - 0" > /sys/class/scsi_host/host${host}/scan
newlist=$(ls /sys/class/scsi_device/ | sed -n "s/${host}:${channel}:\([0-9]*:[0-9]*\)/\1/p" | uniq)
for newid in $newlist ; do
for tmpid in $oldlist ; do
if test $newid = $tmpid ; then
if test -n "$oldid" ; then
dev=/sys/class/scsi_device/${host}:${channel}:${id}:${lun}
printf "\r${green}NEW: $norm"
if test "$SCSISTR" ; then
idsearch=$(ls /sys/class/scsi_device/ | sed -n "s/${host}:${channel}:\([0-9]*\):[0-9]*/\1/p" | uniq)
# Returns the list of existing LUNs from device $host $channel $id $lun
# and returns list to stdout
if test -z "$SGDEV"; then return 1; fi
if test ! -x /usr/bin/sg_luns; then echo 0; return 1; fi
LLUN=`sg_luns /dev/$SGDEV 2>/dev/null | sed -n 's/ \(.*\)/\1/p'`
# Added -z $LLUN condition because $? gets the RC from sed, not sg_luns
if test $? != 0 -o -z "$LLUN"; then echo 0; return 1; fi
l1=$(( ($l0 >> 48) & 0xffff ))
l2=$(( ($l0 >> 32) & 0xffff ))
l3=$(( ($l0 >> 16) & 0xffff ))
l0=$(( ( ( ($l4 * 0xffff) + $l3 ) * 0xffff + $l2 ) * 0xffff + $l1 ))
# Wait for udev to settle (create device nodes etc.)
if test -x /sbin/udevadm; then
print_and_scroll_back " Calling udevadm settle (can take a while) "
# Loop for up to 60 seconds if sd devices still are settling..
# This allows us to continue if udev events are stuck on multipaths in recovery mode
while [ $tmo -gt 0 ] ; do
if ! /sbin/udevadm settle --timeout=1 | egrep -q sd[a-z]+ ; then
elif test -x /sbin/udevsettle; then
print_and_scroll_back " Calling udevsettle (can take a while) "
# Perform scan on a single lun $host $channel $id $lun
devnr="$host $channel $id $lun"
echo -e " Scanning for device $devnr ... "
printf "${yellow}OLD: $norm"
# Device exists: Test whether it's still online
# (testonline returns 2 if it's gone and 1 if it has changed)
if test "$SCSISTR" ; then
# Well known lun transition case. Only for Direct-Access devs (type 0)
# If block directory exists && and PQUAL != 0, we unmapped lun0 and just have a well-known lun
# If block directory doesn't exist && PQUAL == 0, we mapped a real lun0
if test $lun -eq 0 -a $IPTYPE -eq 0 ; then
if test -e /sys/class/scsi_device/${host}:${channel}:${id}:${lun}/device; then
if test -d /sys/class/scsi_device/${host}:${channel}:${id}:${lun}/device/block ; then
remappedlun0=2 # Transition from real lun 0 to well-known
RC=0 # Set this so the system leaves the existing well known lun alone. This is a lun 0 with no block directory
elif test $RC = 0 -a $IPTYPE -eq 0; then
if test -e /sys/class/scsi_device/${host}:${channel}:${id}:${lun}/device; then
if test ! -d /sys/class/scsi_device/${host}:${channel}:${id}:${lun}/device/block ; then
remappedlun0=1 # Transition from well-known to real lun 0
# Special case: lun 0 just got added (for reportlunscan),
# so make sure we correctly treat it as new
if test "$lun" = "0" -a "$1" = "1" -a -z "$remappedlun0"; then
if test "$remove" -a "$SCSISTR" -o "$remappedlun0" = "1"; then
if test $RC != 0 -o ! -z "$forceremove" -o -n "$remappedlun0"; then