[How To] Hot plug/unplug SATA/SAS hard drives on Linux
SCSI, SAS and SATA2 support hot-plugging on a protocol level. If your chassis is equipped with a hotswap drive bays, then that’s all you really need. I haven’t noticed any problems with SATA I hotswaps, but they appear to be less successful.
A common task I need to do is either:
upgrade a drive in a server
replace a dying drive with a new one
If the drive is in a RAID, or if it’s a separate sized drive for a disk upgrade, it’s a good idea to trigger the kernel driver to rescan information about the new disk.
In /sys/bus/scsi/devices, you’ll find a number of numbers that’ll correspond to your disk drives:
server devices # ls -1
You can determine more information about the drive by cat’ing it’s model file:
server devices # cat "0:0:0:0/model"
Now, to cause the kernel to rescan the drive attached to the port, do this:
echo > "0:0:0:0/rescan"
Check dmesg now:
server devices # dmesg
SCSI device sda: 488397168 512-byte hdwr sectors (250059 MB)
sda: Write Protect is off
sda: Mode Sense: 00 3a 00 00
SCSI device sda: drive cache: write back
Now, that’s pretty exciting, as this can be used to skip a reboot. If you switch the drive, and trigger the rescan, it’ll update the drive information including the partition layout. Make sure you don’t swap out your main system drive, otherwise your computer will freeze, and there will be data loss and possibly corruption.
Hot plug: Scan for new disks
# echo "- - -" >/sys/class/scsi_host/host<n>/scan
Hot Unplug: Safely remove a disk:
# echo 1 > /sys/block/sda/device/delete