I sometimes need to plug a disk into a disk bay. At other times, I have the very weird setup of connecting a SSD using a SATA-eSATA cable on my laptop while pulling power from a desktop.How can I safely remove the SATA disk from the system? This Phoronix forum thread has some suggestions:justsumdood wrote:An(noymous)droid wrote:    What then do you do on the software side before unplugging? Is it a simple umount /dev/sd[drive letter]?    after unmounting the device, to power off (or sleep) the unit:hdparm -Y /dev/sdX(where X represents the device you wish to power off. for example: /dev/sdb)this will power the drive down allowing for it's removal w/o risk of voltage surge.Does this mean that the disk caches are properly flushed and powered off thereafter?Another suggestion from the same thread:chithanh wrote:  All SATA and eSATA hardware is physically able to be hotplugged (ie. not damaged if you insert/pull the plug).How the chipset and driver handles this is another question. Some driver/chipset combinations do not properly handle hotplugging and need a warmplug command such as the following one:echo 0 - 0 > /sys/class/scsi_host/hostX/scanReplace X with the appropriate number for your SATA/eSATA port. I doubt whether is the correct way to do so, but I cannot find some proof against it either.So, what is the correct way to remove an attached disk from a system? Assume that I have already unmounted every partition on the disk and ran sync. Please point to some official documentation if possible, I could not find anything in the Linux documentation tree, nor the Linux ATA wiki.
How can I safely remove a SATA disk from a running system?
ssd;sata;hot plug;disk
Naturally, you need to unmount any filesystems on the disk, and it'd be a good idea to deactivate any LVM groups (vgchange -an), and generally make sure nothing is using the disk for anything.Once you've done that, it should be safe to unplug.  If you want to be extra cautious, do echo 1 > /sys/block/(whatever)/device/delete first.  That'll unregister the device from the kernel, so you know nothing's using it when you unplug it.  When I do that with a drive in an eSATA enclosure, I can hear the drive's heads park themselves, so the kernel apparently tells the drive to prepare for power-down.If you're using an AHCI controller, it should cope with devices being unplugged.  If you're using some other sort of SATA controller, the driver might be confused by hotplugging.In my experience, SATA hotplugging (with AHCI) works pretty well in Linux.  I've unplugged an optical drive, plugged in a hard drive, scanned it for errors, made a filesystem and copied data to it, unmounted and unplugged it, plugged in a differerent DVD drive, and burned a disc, all with the machine up and running.