I 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 :
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 :
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.
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
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
Great post dude! Was exactly what I was looking for though for the reverse reasons. I wanted a way to stop/hide the hard disk from the OS once it had loaded the live image in RAM, this was for a media centre I’m building.
Very cool, thanks for sharing. Not sure why this hasn’t been implemented to happen automatically since eSATA with hotplug support has been around for a while now.
Have you checked your BIOS settings? SATA hotplug does not (usually) work until you set BIOS to use AHCI for the chipsets; on most motherboards it is set to ‘ide-emulation’ or such by default.
And for further notes, even AHCI is enabled, some motherboards required to set a seprate option for SATA hotplugging.
Thank you for this posting – it helped me to replace fast one of my faulty SATA RAID disks on a running server.
Cheers 😉
Glad it helped 🙂
I knew my motherboard and Linux kernel support SATA hotplugging, I enabled it but had no idea on how to use it. Thanks!