rescan-scsi-bus.sh 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. #!/bin/bash
  2. # Skript to rescan SCSI bus, using the
  3. # scsi add-single-device mechanism
  4. # (c) 1998--2008 Kurt Garloff <kurt@garloff.de>, GNU GPL v2 or later
  5. # (c) 2006--2008 Hannes Reinecke, GNU GPL v2 or later
  6. # $Id: rescan-scsi-bus.sh,v 1.29 2008/10/29 10:03:04 garloff Exp $
  7. setcolor ()
  8. {
  9. red="\e[0;31m"
  10. green="\e[0;32m"
  11. yellow="\e[0;33m"
  12. bold="\e[0;1m"
  13. norm="\e[0;0m"
  14. }
  15. unsetcolor ()
  16. {
  17. red=""; green=""
  18. yellow=""; norm=""
  19. }
  20. # Return hosts. sysfs must be mounted
  21. findhosts_26 ()
  22. {
  23. hosts=
  24. if ! ls /sys/class/scsi_host/host* >/dev/null 2>&1; then
  25. echo "No SCSI host adapters found in sysfs"
  26. exit 1;
  27. fi
  28. for hostdir in /sys/class/scsi_host/host*; do
  29. hostno=${hostdir#/sys/class/scsi_host/host}
  30. if [ -f $hostdir/isp_name ] ; then
  31. hostname="qla2xxx"
  32. elif [ -f $hostdir/lpfc_drvr_version ] ; then
  33. hostname="lpfc"
  34. else
  35. hostname=`cat $hostdir/proc_name`
  36. fi
  37. hosts="$hosts $hostno"
  38. echo "Host adapter $hostno ($hostname) found."
  39. done
  40. hosts=`echo $hosts | sed 's/ /\n/g' | sort -n`
  41. }
  42. # Return hosts. /proc/scsi/HOSTADAPTER/? must exist
  43. findhosts ()
  44. {
  45. hosts=
  46. for driverdir in /proc/scsi/*; do
  47. driver=${driverdir#/proc/scsi/}
  48. if test $driver = scsi -o $driver = sg -o $driver = dummy -o $driver = device_info; then continue; fi
  49. for hostdir in $driverdir/*; do
  50. name=${hostdir#/proc/scsi/*/}
  51. if test $name = add_map -o $name = map -o $name = mod_parm; then continue; fi
  52. num=$name
  53. driverinfo=$driver
  54. if test -r $hostdir/status; then
  55. num=$(printf '%d\n' `sed -n 's/SCSI host number://p' $hostdir/status`)
  56. driverinfo="$driver:$name"
  57. fi
  58. hosts="$hosts $num"
  59. echo "Host adapter $num ($driverinfo) found."
  60. done
  61. done
  62. }
  63. # Get /proc/scsi/scsi info for device $host:$channel:$id:$lun
  64. # Optional parameter: Number of lines after first (default = 2),
  65. # result in SCSISTR, return code 1 means empty.
  66. procscsiscsi ()
  67. {
  68. if test -z "$1"; then LN=2; else LN=$1; fi
  69. CHANNEL=`printf "%02i" $channel`
  70. ID=`printf "%02i" $id`
  71. LUN=`printf "%02i" $lun`
  72. if [ -d /sys/class/scsi_device ]; then
  73. SCSIPATH="/sys/class/scsi_device/${host}:${channel}:${id}:${lun}"
  74. if [ -d "$SCSIPATH" ] ; then
  75. SCSISTR="Host: scsi${host} Channel: $CHANNEL Id: $ID Lun: $LUN"
  76. if [ "$LN" -gt 0 ] ; then
  77. IVEND=$(cat ${SCSIPATH}/device/vendor)
  78. IPROD=$(cat ${SCSIPATH}/device/model)
  79. IPREV=$(cat ${SCSIPATH}/device/rev)
  80. SCSIDEV=$(printf ' Vendor: %-08s Model: %-16s Rev: %-4s' "$IVEND" "$IPROD" "$IPREV")
  81. SCSISTR="$SCSISTR
  82. $SCSIDEV"
  83. fi
  84. if [ "$LN" -gt 1 ] ; then
  85. ILVL=$(cat ${SCSIPATH}/device/scsi_level)
  86. type=$(cat ${SCSIPATH}/device/type)
  87. case "$type" in
  88. 0) ITYPE="Direct-Access " ;;
  89. 1) ITYPE="Sequential-Access" ;;
  90. 2) ITYPE="Printer " ;;
  91. 3) ITYPE="Processor " ;;
  92. 4) ITYPE="WORM " ;;
  93. 5) ITYPE="CD-ROM " ;;
  94. 6) ITYPE="Scanner " ;;
  95. 7) ITYPE="Optical Device " ;;
  96. 8) ITYPE="Medium Changer " ;;
  97. 9) ITYPE="Communications " ;;
  98. 10) ITYPE="Unknown " ;;
  99. 11) ITYPE="Unknown " ;;
  100. 12) ITYPE="RAID " ;;
  101. 13) ITYPE="Enclosure " ;;
  102. 14) ITYPE="Direct-Access-RBC" ;;
  103. *) ITYPE="Unknown " ;;
  104. esac
  105. SCSITMP=$(printf ' Type: %-16s ANSI SCSI revision: %02d' "$ITYPE" "$((ILVL - 1))")
  106. SCSISTR="$SCSISTR
  107. $SCSITMP"
  108. fi
  109. else
  110. return 1
  111. fi
  112. else
  113. grepstr="scsi$host Channel: $CHANNEL Id: $ID Lun: $LUN"
  114. SCSISTR=`cat /proc/scsi/scsi | grep -A$LN -e"$grepstr"`
  115. fi
  116. if test -z "$SCSISTR"; then return 1; else return 0; fi
  117. }
  118. # Find sg device with 2.6 sysfs support
  119. sgdevice26 ()
  120. {
  121. if test -e /sys/class/scsi_device/$host\:$channel\:$id\:$lun/device/generic; then
  122. SGDEV=`readlink /sys/class/scsi_device/$host\:$channel\:$id\:$lun/device/generic`
  123. SGDEV=`basename $SGDEV`
  124. else
  125. for SGDEV in /sys/class/scsi_generic/sg*; do
  126. DEV=`readlink $SGDEV/device`
  127. if test "${DEV##*/}" = "$host:$channel:$id:$lun"; then
  128. SGDEV=`basename $SGDEV`; return
  129. fi
  130. done
  131. SGDEV=""
  132. fi
  133. }
  134. # Find sg device with 2.4 report-devs extensions
  135. sgdevice24 ()
  136. {
  137. if procscsiscsi 3; then
  138. SGDEV=`echo "$SCSISTR" | grep 'Attached drivers:' | sed 's/^ *Attached drivers: \(sg[0-9]*\).*/\1/'`
  139. fi
  140. }
  141. # Find sg device that belongs to SCSI device $host $channel $id $lun
  142. sgdevice ()
  143. {
  144. SGDEV=
  145. if test -d /sys/class/scsi_device; then
  146. sgdevice26
  147. else
  148. DRV=`grep 'Attached drivers:' /proc/scsi/scsi 2>/dev/null`
  149. repdevstat=$((1-$?))
  150. if [ $repdevstat = 0 ]; then
  151. echo "scsi report-devs 1" >/proc/scsi/scsi
  152. DRV=`grep 'Attached drivers:' /proc/scsi/scsi 2>/dev/null`
  153. if [ $? = 1 ]; then return; fi
  154. fi
  155. if ! `echo $DRV | grep 'drivers: sg' >/dev/null`; then
  156. modprobe sg
  157. fi
  158. sgdevice24
  159. if [ $repdevstat = 0 ]; then
  160. echo "scsi report-devs 0" >/proc/scsi/scsi
  161. fi
  162. fi
  163. }
  164. # Test if SCSI device is still responding to commands
  165. testonline ()
  166. {
  167. : testonline
  168. if test ! -x /usr/bin/sg_turs; then return 0; fi
  169. sgdevice
  170. if test -z "$SGDEV"; then return 0; fi
  171. sg_turs /dev/$SGDEV >/dev/null 2>&1
  172. RC=$?
  173. # echo -e "\e[A\e[A\e[A${yellow}Test existence of $SGDEV = $RC ${norm} \n\n\n"
  174. if test $RC = 1; then return $RC; fi
  175. # OK, device online, compare INQUIRY string
  176. INQ=`sg_inq $sg_len_arg /dev/$SGDEV`
  177. IVEND=`echo "$INQ" | grep 'Vendor identification:' | sed 's/^[^:]*: \(.*\)$/\1/'`
  178. IPROD=`echo "$INQ" | grep 'Product identification:' | sed 's/^[^:]*: \(.*\)$/\1/'`
  179. IPREV=`echo "$INQ" | grep 'Product revision level:' | sed 's/^[^:]*: \(.*\)$/\1/'`
  180. STR=`printf " Vendor: %-08s Model: %-16s Rev: %-4s" "$IVEND" "$IPROD" "$IPREV"`
  181. IPTYPE=`echo "$INQ" | sed -n 's/.* Device_type=\([0-9]*\) .*/\1/p'`
  182. IPQUAL=`echo "$INQ" | sed -n 's/ *PQual=\([0-9]*\) Device.*/\1/p'`
  183. if [ "$IPQUAL" != 0 ] ; then
  184. echo -e "\e[A\e[A\e[A\e[A${red}$SGDEV changed: ${bold}\nLU not available (PQual $IPQUAL)${norm}\n\n\n"
  185. return 1
  186. fi
  187. procscsiscsi
  188. TMPSTR=`echo "$SCSISTR" | grep 'Vendor:'`
  189. if [ "$TMPSTR" != "$STR" ]; then
  190. echo -e "\e[A\e[A\e[A\e[A${red}$SGDEV changed: ${bold}\nfrom:${TMPSTR#* } \nto: $STR ${norm}\n\n\n"
  191. return 1
  192. fi
  193. TMPSTR=`echo "$SCSISTR" | sed -n 's/.*Type: *\(.*\) *ANSI.*/\1/p'`
  194. if [ $TMPSTR != $TYPE ] ; then
  195. echo -e "\e[A\e[A\e[A\e[A${red}$SGDEV changed: ${bold}\nfrom:${TMPSTR} \nto: $TYPE ${norm}\n\n\n"
  196. return 1
  197. fi
  198. return $RC
  199. }
  200. # Test if SCSI device $host $channen $id $lun exists
  201. # Outputs description from /proc/scsi/scsi, returns SCSISTR
  202. testexist ()
  203. {
  204. : testexist
  205. SCSISTR=
  206. if procscsiscsi; then
  207. echo "$SCSISTR" | head -n1
  208. echo "$SCSISTR" | tail -n2 | pr -o4 -l1
  209. fi
  210. }
  211. # Returns the list of existing channels per host
  212. chanlist ()
  213. {
  214. local hcil
  215. local cil
  216. local chan
  217. local tmpchan
  218. for dev in /sys/class/scsi_device/${host}:* ; do
  219. hcil=${dev##*/}
  220. cil=${hcil#*:}
  221. chan=${cil%%:*}
  222. for tmpchan in $channelsearch ; do
  223. if test "$chan" -eq $tmpchan ; then
  224. chan=
  225. fi
  226. done
  227. if test -n "$chan" ; then
  228. channelsearch="$channelsearch $chan"
  229. fi
  230. done
  231. }
  232. # Returns the list of existing targets per host
  233. idlist ()
  234. {
  235. local hcil
  236. local cil
  237. local il
  238. local target
  239. local tmpid
  240. for dev in /sys/class/scsi_device/${host}:${channel}:* ; do
  241. hcil=${dev##*/}
  242. cil=${hcil#*:}
  243. il=${cil#*:}
  244. target=${il%%:*}
  245. for tmpid in $idsearch ; do
  246. if test "$target" -eq $tmpid ; then
  247. target=
  248. fi
  249. done
  250. if test -n "$target" ; then
  251. idsearch="$idsearch $target"
  252. fi
  253. done
  254. }
  255. # Returns the list of existing LUNs
  256. getluns ()
  257. {
  258. if test ! -x /usr/bin/sg_luns; then return; fi
  259. sgdevice
  260. if test -z "$SGDEV"; then return; fi
  261. sg_luns -d /dev/$SGDEV | sed -n 's/.*lun=\(.*\)/\1/p'
  262. }
  263. # Perform scan on a single lun
  264. dolunscan()
  265. {
  266. SCSISTR=
  267. devnr="$host $channel $id $lun"
  268. echo "Scanning for device $devnr ..."
  269. printf "${yellow}OLD: $norm"
  270. testexist
  271. : f $remove s $SCSISTR
  272. if test "$remove" -a "$SCSISTR"; then
  273. # Device exists: Test whether it's still online
  274. # (testonline returns 1 if it's gone or has changed)
  275. testonline
  276. if test $? = 1 -o ! -z "$forceremove"; then
  277. echo -en "\r\e[A\e[A\e[A${red}REM: "
  278. echo "$SCSISTR" | head -n1
  279. echo -e "${norm}\e[B\e[B"
  280. if test -e /sys/class/scsi_device/${host}:${channel}:${id}:${lun}/device; then
  281. echo 1 > /sys/class/scsi_device/${host}:${channel}:${id}:${lun}/device/delete
  282. # Try reading, should fail if device is gone
  283. echo "$channel $id $lun" > /sys/class/scsi_host/host${host}/scan
  284. else
  285. echo "scsi remove-single-device $devnr" > /proc/scsi/scsi
  286. # Try reading, should fail if device is gone
  287. echo "scsi add-single-device $devnr" > /proc/scsi/scsi
  288. fi
  289. fi
  290. if test $RC = 0 ; then
  291. if test -e /sys/class/scsi_device/${host}:${channel}:${id}:${lun}/device; then
  292. echo 1 > /sys/class/scsi_device/${host}:${channel}:${id}:${lun}/device/rescan
  293. fi
  294. fi
  295. printf "\r\x1b[A\x1b[A\x1b[A${yellow}OLD: $norm"
  296. testexist
  297. if test -z "$SCSISTR"; then
  298. printf "\r${red}DEL: $norm\r\n\n"
  299. let rmvd+=1;
  300. fi
  301. fi
  302. if test -z "$SCSISTR"; then
  303. # Device does not exist, try to add
  304. printf "\r${green}NEW: $norm"
  305. if test -e /sys/class/scsi_host/host${host}/scan; then
  306. echo "$channel $id $lun" > /sys/class/scsi_host/host${host}/scan 2> /dev/null
  307. else
  308. echo "scsi add-single-device $devnr" > /proc/scsi/scsi
  309. fi
  310. testexist
  311. if test -z "$SCSISTR"; then
  312. # Device not present
  313. printf "\r\x1b[A";
  314. # Optimization: if lun==0, stop here (only if in non-remove mode)
  315. if test $lun = 0 -a -z "$remove" -a $optscan = 1; then
  316. break;
  317. fi
  318. else
  319. let found+=1;
  320. fi
  321. fi
  322. }
  323. # Perform report lun scan
  324. doreportlun()
  325. {
  326. lun=0
  327. SCSISTR=
  328. devnr="$host $channel $id $lun"
  329. echo "Scanning for device $devnr ..."
  330. printf "${yellow}OLD: $norm"
  331. testexist
  332. if test -z "$SCSISTR"; then
  333. # Device does not exist, try to add
  334. printf "\r${green}NEW: $norm"
  335. if test -e /sys/class/scsi_host/host${host}/scan; then
  336. echo "$channel $id $lun" > /sys/class/scsi_host/host${host}/scan 2> /dev/null
  337. else
  338. echo "scsi add-single-device $devnr" > /proc/scsi/scsi
  339. fi
  340. testexist
  341. if test -z "$SCSISTR"; then
  342. # Device not present
  343. printf "\r\x1b[A";
  344. lunsearch=
  345. return
  346. fi
  347. fi
  348. lunsearch=`getluns`
  349. lunremove=
  350. # Check existing luns
  351. for dev in /sys/class/scsi_device/$host\:$channel\:$id\:*; do
  352. lun=${dev##*:}
  353. newsearch=
  354. oldsearch="$lunsearch"
  355. for tmplun in $lunsearch; do
  356. if test $tmplun -eq $lun ; then
  357. # Optimization: don't scan lun 0 again
  358. if [ $lun -ne 0 ]; then
  359. dolunscan
  360. fi
  361. else
  362. newsearch="$newsearch $tmplun"
  363. fi
  364. done
  365. if [ "${#oldsearch}" = "${#newsearch}" ] ; then
  366. # Stale lun
  367. lunremove="$lunremove $lun"
  368. fi
  369. lunsearch="$newsearch"
  370. done
  371. # Add new ones and check stale ones
  372. for lun in $lunsearch $lunremove; do
  373. dolunscan
  374. done
  375. }
  376. # Perform search (scan $host)
  377. dosearch ()
  378. {
  379. if test -z "$channelsearch" ; then
  380. chanlist
  381. fi
  382. for channel in $channelsearch; do
  383. if test -z "$idsearch" ; then
  384. idlist
  385. fi
  386. for id in $idsearch; do
  387. if test -z "$lunsearch"; then
  388. doreportlun
  389. else
  390. for lun in $lunsearch; do
  391. dolunscan
  392. done
  393. fi
  394. done
  395. done
  396. }
  397. # main
  398. if test @$1 = @--help -o @$1 = @-h -o @$1 = @-?; then
  399. echo "Usage: rescan-scsi-bus.sh [options] [host [host ...]]"
  400. echo "Options:"
  401. echo " -l activates scanning for LUNs 0-7 [default: 0]"
  402. echo " -L NUM activates scanning for LUNs 0--NUM [default: 0]"
  403. echo " -w scan for target device IDs 0 .. 15 [default: 0-7]"
  404. echo " -c enables scanning of channels 0 1 [default: 0]"
  405. echo " -r enables removing of devices [default: disabled]"
  406. echo " -i issue a FibreChannel LIP reset [default: disabled]"
  407. echo "--remove: same as -r"
  408. echo "--issue-lip: same as -i"
  409. echo "--forceremove: Remove and readd every device (DANGEROUS)"
  410. echo "--nooptscan: don't stop looking for LUNs is 0 is not found"
  411. echo "--color: use coloured prefixes OLD/NEW/DEL"
  412. echo "--hosts=LIST: Scan only host(s) in LIST"
  413. echo "--channels=LIST: Scan only channel(s) in LIST"
  414. echo "--ids=LIST: Scan only target ID(s) in LIST"
  415. echo "--luns=LIST: Scan only lun(s) in LIST"
  416. echo " Host numbers may thus be specified either directly on cmd line (deprecated) or"
  417. echo " or with the --hosts=LIST parameter (recommended)."
  418. echo "LIST: A[-B][,C[-D]]... is a comma separated list of single values and ranges"
  419. echo " (No spaces allowed.)"
  420. exit 0
  421. fi
  422. expandlist ()
  423. {
  424. list=$1
  425. result=""
  426. first=${list%%,*}
  427. rest=${list#*,}
  428. while test ! -z "$first"; do
  429. beg=${first%%-*};
  430. if test "$beg" = "$first"; then
  431. result="$result $beg";
  432. else
  433. end=${first#*-}
  434. result="$result `seq $beg $end`"
  435. fi
  436. test "$rest" = "$first" && rest=""
  437. first=${rest%%,*}
  438. rest=${rest#*,}
  439. done
  440. echo $result
  441. }
  442. if test ! -d /sys/class/scsi_host/ -a ! -d /proc/scsi/; then
  443. echo "Error: SCSI subsystem not active"
  444. exit 1
  445. fi
  446. # Make sure sg is there
  447. modprobe sg >/dev/null 2>&1
  448. sg_version=$(sg_inq -V 2>&1 | cut -d " " -f 3)
  449. sg_version=${sg_version##0.}
  450. if [ "$sg_version" -lt 70 ] ; then
  451. sg_len_arg="-36"
  452. else
  453. sg_len_arg="--len=36"
  454. fi
  455. # defaults
  456. unsetcolor
  457. lunsearch=""
  458. idsearch=`seq 0 7`
  459. channelsearch="0"
  460. remove=
  461. forceremove=
  462. optscan=1
  463. if test -d /sys/class/scsi_host; then
  464. findhosts_26
  465. else
  466. findhosts
  467. fi
  468. # Scan options
  469. opt="$1"
  470. while test ! -z "$opt" -a -z "${opt##-*}"; do
  471. opt=${opt#-}
  472. case "$opt" in
  473. l) lunsearch=`seq 0 7` ;;
  474. L) lunsearch=`seq 0 $2`; shift ;;
  475. w) idsearch=`seq 0 15` ;;
  476. c) channelsearch="0 1" ;;
  477. r) remove=1 ;;
  478. i) lipreset=1 ;;
  479. -remove) remove=1 ;;
  480. -forceremove) remove=1; forceremove=1 ;;
  481. -hosts=*) arg=${opt#-hosts=}; hosts=`expandlist $arg` ;;
  482. -channels=*) arg=${opt#-channels=};channelsearch=`expandlist $arg` ;;
  483. -ids=*) arg=${opt#-ids=}; idsearch=`expandlist $arg` ;;
  484. -luns=*) arg=${opt#-luns=}; lunsearch=`expandlist $arg` ;;
  485. -color) setcolor ;;
  486. -nooptscan) optscan=0 ;;
  487. -issue-lip) lipreset=1 ;;
  488. *) echo "Unknown option -$opt !" ;;
  489. esac
  490. shift
  491. opt="$1"
  492. done
  493. # Hosts given ?
  494. if test "@$1" != "@"; then
  495. hosts=$*;
  496. fi
  497. echo "Scanning SCSI subsystem for new devices"
  498. test -z "$remove" || echo " and remove devices that have disappeared"
  499. declare -i found=0
  500. declare -i rmvd=0
  501. for host in $hosts; do
  502. echo -n "Scanning host $host "
  503. if test -e /sys/class/fc_host/host$host ; then
  504. # It's pointless to do a target scan on FC
  505. if test -n "$lipreset" ; then
  506. echo 1 > /sys/class/fc_host/host$host/issue_lip 2> /dev/null;
  507. echo "- - -" > /sys/class/scsi_host/host$host/scan 2> /dev/null;
  508. fi
  509. channelsearch=""
  510. idsearch=""
  511. fi
  512. [ -n "$channelsearch" ] && echo -n "channels $channelsearch "
  513. echo -n "for "
  514. if [ -n "$idsearch" ] ; then
  515. echo -n " SCSI target IDs " $idsearch
  516. else
  517. echo -n " all SCSI target IDs"
  518. fi
  519. if [ -n "$lunsearch" ] ; then
  520. echo ", LUNs " $lunsearch
  521. else
  522. echo ", all LUNs"
  523. fi
  524. dosearch;
  525. done
  526. echo "$found new device(s) found. "
  527. echo "$rmvd device(s) removed. "