- Unmount any filesystems on the disk. (
- Deactivate any LVM groups. (
- Make sure nothing is using the disk for anything.
- You Could unplug the HDD here, but it is recommended to also do the last two steps
- Spin the HDD down. (irrelevant for SSD’s) (
sudo hdparm -Y /dev/(whatever))
- Tell the system, that we are unplugging the HDD, so it can prepare itself. (
echo 1 | sudo tee /sys/block/(whatever)/device/delete)
If you want to be extra cautious, do
echo 1 | sudo tee /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.
Those two sections are for different things.
The first is for unplugging. The second is for plugging.
For unplugging, the OS will sync the data during the unmount operation. Thus, if the disk is unmounted (assuming you in fact do have full hardware support) you can power off the disk then unplug it without risk of data loss or corruption.
For plugging, the device should be automatically recognized. If not, you can execute that command to trigger a bus scan. Once the device is recognized you can mount it.
And let me caveat this by saying I’ve only ever done this sort of thing with USB drives.
eject /dev/sdX? On my setup, this commands umounts, syncs and powers down the drive.