How To Hot Plug a SATA drive in Linux

Published 1 min read
image
Image Credit: Unknown (if this is your work, reach out to me and I'll credit you!)

Hi guys,

I knew SATA drives were hot-swappable, and a feature of a script I was writing required that the user be able to swap the drive that was plugged in at a certain point. It became obvious after checking the usual suspects (/var/log/messages, /var/log/dmesg) that the drive was not going to be detected by simply plugging it in. After a quick search online, I came up with fredericiana.

The reason appears to be that the OS needs a poke to tell it to rescan the device. In Linux, this is as simple as echoing a value to a file. Here goes:

echo "0 0 0" >/sys/class/scsi_host/host[n]/scan

[n] will be one of a few numbers. Look in /sys/class/scsi_host to see what's available. There is one of these for each SATA port (this possibly includes PATA/IDE ports on newer kernels).

To remove a drive, use the following:

echo "x" > /sys/bus/scsi/devices/[n]:0:0:0/delete

I'll repeat the original author's warning, just so there's no confusion generated:

"Also, just to state the obvious, don't do that to a mounted drive, ever. Especially not the one that holds your system partition ;)"

n00b

All content © Alex Shepherd 2008- unless otherwise noted