Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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/scan

Replace 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.

share|improve this question

3 Answers 3

up vote 20 down vote accepted

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.

share|improve this answer
    
I had a need to unplug a HDD which I wanted to erase completely. After pulling the disk out of the bay, /dev/sdXY still showed up. Writing 1 to delete made it disappear and I could hear the disk spinning down. Just hdparm -Y was not enough because the /dev/ entries would still exist. Thanks! –  Lekensteyn Aug 9 '12 at 9:25
    
I strongly advice to always issue the echo 1 > /sys/block/(whatever)/device/delete command because the drive will park the heads, fully stop the disk and disable power on the bus. If a non-parked head touches a spinning plate the drive can be permanently destroyed. –  drumfire Feb 12 at 21:21
    
Also - if smartd happens to be running, it's a good idea to issue a SIGHUP to the process so that it reloads the drive info. Especially important if you're replacing drives because smartd will reload the info for that drive and all others. –  drumfire Feb 12 at 21:23

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.

share|improve this answer
    
Thanks for your answer, but I am still not convinced what the correct action would be. SSDs have a "Unsafe Shutdown Count" S.M.A.R.T. field, just unplugging it without doing anything does not sound safe to me. –  Lekensteyn Jul 18 '12 at 21:58
2  
If it helps, I routinely hot-plug and hot-unplug SATA hard drives as part of my job, with no more than making sure it's unmounted first, and I've never run into a problem. That's anecdotal, so don't take it as gospel, but it's at least some evidence that it's likely safe. In any case, I'd consider it a kernel bug if the OS doesn't ensure that the data is fully written at the end of an unmount operation, especially in a hotplug world. –  Jander Jul 19 '12 at 0:24
1  
@Lekensteyn, the hdparm -Y will take care of that. That is basically what the system does every time you suspend or shutdown. –  psusi Jul 19 '12 at 13:16

what about eject /dev/sdX? On my setup, this commands umounts, syncs and powers down the drive.

share|improve this answer
1  
I tried this with a disk connected over eSATA but the command failed with "not hotpluggable" or something. –  Lekensteyn Aug 30 '13 at 9:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.