Linux : SATA hot plug / unplug
May 4th, 2009 by Stephane KattoorI have 3 hard disks in SATA-to-eSATA external enclosure which I occasionally need to plug to perform backups and to unplug when done. I found it annoying to have to restart the whole computer at every turn, especially when SATA is supposed to bring hotplug abilities. If you mainboard / SATA chipset and disks support hot plugging and unplugging, you can do this by following those instructions.
Hot plug : Scanning for newly added disks
When you plug the disks, you need to tell the OS to scan for new SATA devices, you do this with the following command :
1 | roadrunner / # echo "- - -" > /sys/class/scsi_host/host0/scan |
Here “host0″ is the SATA channel you want to scan, so you obviously need to replace that by the appropriate channel number. Linux will then hopefully discover the newly attached disk.
Hot unplug : safely removing a disk
First thing, you need to unmount all the mounted filesystems which are residing on the disk you wish to disconnect.
Once this is done, you need to instruct Linux to flush all data still in memory to the disk and then to disconnect it. You do this by issuing the following command :
1 | roadrunner / # echo 1 > /sys/block/sda/device/delete |
At this point you’ll usually hear the “click” of the drive spinning down, and you can power off and disconnect it.
Tagged with: hotplug • linux • sata


















May 25th, 2009 at 9:50
Hi Stéphane,
thanks for sharing this.
If I would put the hotplug scan into a bash script, I would like to have the script wait, until “# fdsik -l” actually recognizes the newly added device / redo the scan until the device has been recognized.
Any ideas on how to do this?
Thanks, Nils
May 26th, 2009 at 22:49
Hello Nils,
Here are some hints to get you started :
1) here’s how to get the list of the attached disks : fdisk -l | grep ‘^/dev/sd’ | awk ‘{print $1}’
2) Do this at script start up, save result in a temp file
3) Run the command to discover new disks as described in the post
4) Get the disk list again. Is there a new disk (maybe use diff) ? Yes -> terminate. No -> sleep 10 sec and go to 3
Hope this helps ! (and hopefully I understood correctly what you need
)
Stéphane