rescanning the connected SATA/SCSI devices

Once in a while I stumble upon the problem where I have to attach a SATA or SCSI device to a running Linux box and for some reasons (ie. the device or the interface is not hot plugging capable) it doesn’t get recognoized by the system.

Now the solution is very simple, just tell the kernel to rescan the SATA/SCSI bus:

first find out how many SATA/SCSI busses you have:

% ls -l /sys/class/scsi_host/
drwxr-xr-x 2 root root 0 2009-08-24 15:05 host0
drwxr-xr-x 2 root root 0 2009-08-24 15:05 host1
drwxr-xr-x 2 root root 0 2009-08-24 16:26 host2
drwxr-xr-x 2 root root 0 2009-08-24 16:26 host3

So that means 4 host interfaces to query for. Querying or rescanning is very easy:

Find out which hard disks are currently recognoized by the kernel:

% cat /sys/class/scsi_disk/*/device/model
WDC WD1600YS-01S

In other words, a Western Digital WD1600YS hard drive is connected and recognoized by the computer.

Now, if you want the SATA/SCSI bus to rescan all host interfaces for new devices, do this:

% echo "- - -" > /sys/class/scsi_host/*/scan

Some readers have pointed out, that the above statements can lead to bash complaining about ambiguous redirects (more or less a security feature). So in order to make bash happy, use this syntax instead (thanks Tom):

% for scan in /sys/class/scsi_host/*/scan; do echo "- - -" > $scan; done

To theck if any previously unrecognized hard disks have been detected, refetch the list of hard disks:

% cat /sys/class/scsi_disk/*/device/model
WDC WD1600YS-01S
Seagate FreeAgen

So as you see, a new “Seagate FreeAgen” hard disk has been detected.

The procedure is essentially the same for all other types of SATA/SCSI devices such as scanners, tape drives, …

Spread the love

4
Leave a Reply

avatar
2 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
udoTomJim MillsAnonymous Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Anonymous
Guest
Anonymous

[root@delta6thc ~]# echo “- – -” > /sys/class/scsi_host/*/scan
-bash: /sys/class/scsi_host/*/scan: ambiguous redirect

I used this instead:

for i in $(ls /sys/class/scsi_host/); do echo “- – -” > $i/scan; done

Jim Mills
Guest
Jim Mills

I had to use this: for i in $(ls -d /sys/class/scsi_host/*); do echo “- – -” > $i/scan; done

Tom
Guest
Tom

This is probably the canonical bash way of doing this:

for scan in /sys/class/scsi_host/*/scan; do echo “—” > $scan; done

However, for me it gives “bash: echo: write error: Invalid argument” on linux 4.6.3 running in a QEMU box.

Post Navigation