Force the kernel to unregister a drive and then initialize it again

I had a drive that would seem to be mounted and busy no matter what I did. But since I didn't want to reboot the whole system, I ran echo 1 > /sys/block/sd*/device/delete to force the kernel to "unplug" or unregister the drive.

Then I unplugged and replugged the SATA power cable of the disk, it showed up again in the system and worked as expected.

But what could I've done if I didn't have physically access to it? What would be the alternative to physically unplugging the SATA cable so the system could see it again after running echo 1 > /sys/block/sd*/device/delete ?


You could rescan the SCSI host bus by running the following command

echo "- - -" > /sys/class/scsi_host/host0/scan

NOTE: The host0 part of the command above is the host ID. And you might have multiple/other host ID's on your machine. Which means that if the command above doesn't get you the drive back, you might have to change the host ID to match what you have on your machine.

You can look up which host ID's you have available on your machine by running

ls /sys/class/scsi_host

Which on my machine returns

host0  host1  host2  host3  host4  host5

So look up your host ID's and run the first command on each ID to make sure that the drive gets detected.

Edit:

To repeat this command for each host ID you could just put in into a loop like this:

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