What's the proper way to disconnect hot-swap SATA hard drive?

12,786

Solution 1

Here's the weirdest command that actually does the trick I was looking for:

device=sde; sudo sh -c "echo 1 > /sys/block/${device}/device/delete"

Replace sde in this example with the respective drive. sh is a link to /bin/dash.

... and after this it goes:

[1285227.129329] sd 5:0:0:0: [sde] Synchronizing SCSI cache
[1285227.129566] sd 5:0:0:0: [sde] Stopping disk
[1285228.209095] ata6.00: disabled

... and now I can POWER OFF and it goes:

[1285259.491044] ata6: exception Emask 0x10 SAct 0x0 SErr 0x4090000 action 0xe frozen
[1285259.491049] ata6: irq_stat 0x00400040, connection status changed
[1285259.491052] ata6: SError: { PHYRdyChg 10B8B DevExch }
[1285259.491058] ata6: hard resetting link
[1285260.211923] ata6: SATA link down (SStatus 0 SControl 300)
[1285260.211934] ata6: EH complete

Little bummer that it needs root privilege, but it looks much more satisfying :)

Solution 2

When you have unmounted all the partitions with file systems in the drive (and swapped off any active swap partitions), you can turn off the power without any risk for the file systems (and maybe data storage of a hibernated state), and after that you can disconnect the drive.

I have used that method for years with eSATA drives, that I use for backup, and I have had no problems.


The unmount process starts with flushing the buffers (finishes writing data to the mounted file systems, which can also be done separately with the command sync), and after that the unmount process is finished (and the file systems are no longer available).

The following links may help describing unmounting and ejecting USB pendrives, (eject = unmount and turn off the power),

  • more about unmount: Unmount my Windows dedicated drive on Ubuntu pendrive

  • unmount versus eject: How to unmount MTP USB device from command line

    This is unmount. The buffers are flushed, so it is safe to unplug the device. The device has still power on, so it can be 'seen on the desktop', and it can be mounted again. This is different from eject. An ejected pendrive will be powered off, and must be unplugged and plugged in again if you want to mount it.

    I don't use any general automatic eject method for [e]SATA drives in linux, but you can use the following sequence of actions,

    • unmount
    • turn off power
    • disconnect

You can find methods via hdparm to turn off a SATA drive, but I have not used them, and I don't think they are necessary.

Share:
12,786
GreggD
Author by

GreggD

Updated on September 18, 2022

Comments

  • GreggD
    GreggD over 1 year

    I'm using a SATA hot-swap bay and I have in BIOS this particular port configured specifically for "Hot-Swap".

    This bay has convenient "POWER ON/OFF" button, so no interruptions during plugging-in/out cables occurs.

    When I'm turning a drive ON, I'm immediately getting some kernel errors, before it mounts:

    [1263413.036674] ata6: exception Emask 0x10 SAct 0x0 SErr 0x4040000 action 0xe frozen
    [1263413.036680] ata6: irq_stat 0x00000040, connection status changed
    [1263413.036683] ata6: SError: { CommWake DevExch }
    [1263413.036698] ata6: hard resetting link
    [1263418.796687] ata6: link is slow to respond, please be patient (ready=0)
    [1263423.052529] ata6: COMRESET failed (errno=-16)
    [1263423.052547] ata6: hard resetting link
    [1263428.032390] ata6: SATA link up 3.0 Gbps (SStatus 123 SControl 300)
    [1263428.032836] ACPI Error: [DSSP] Namespace lookup failure, AE_NOT_FOUND (20150930/psargs-359)
    [1263428.032846] ACPI Error: Method parse/execution failed [\_SB.PCI0.SAT0.SPT5._GTF] (Node ffff88040e0c5780), AE_NOT_FOUND (20150930/psparse-542)
    [1263428.159582] ata6.00: ATA-9: HGST HDN726040ALE614, APGNW7JH, max UDMA/133
    [1263428.159588] ata6.00: 7814037168 sectors, multi 0: LBA48 NCQ (depth 31/32), AA
    [1263428.161980] ACPI Error: [DSSP] Namespace lookup failure, AE_NOT_FOUND (20150930/psargs-359)
    [1263428.161988] ACPI Error: Method parse/execution failed [\_SB.PCI0.SAT0.SPT5._GTF] (Node ffff88040e0c5780), AE_NOT_FOUND (20150930/psparse-542)
    [1263428.164472] ata6.00: configured for UDMA/133
    [1263428.164483] ata6: EH complete
    [1263428.164650] scsi 5:0:0:0: Direct-Access     ATA      HGST HDN726040AL W7JH PQ: 0 ANSI: 5
    [1263428.165064] sd 5:0:0:0: Attached scsi generic sg4 type 0
    [1263428.165502] sd 5:0:0:0: [sdd] 7814037168 512-byte logical blocks: (4.00 TB/3.64 TiB)
    [1263428.165508] sd 5:0:0:0: [sdd] 4096-byte physical blocks
    [1263428.165582] sd 5:0:0:0: [sdd] Write Protect is off
    [1263428.165587] sd 5:0:0:0: [sdd] Mode Sense: 00 3a 00 00
    [1263428.165618] sd 5:0:0:0: [sdd] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
    [1263428.213630]  sdd: sdd1
    [1263428.214062] sd 5:0:0:0: [sdd] Attached SCSI disk
    [1263534.534238] EXT4-fs (dm-5): mounted filesystem with ordered data mode. Opts: (null)
    

    And when I'm unmounting it (like any other drive) and then turning the bay OFF, I'm getting more weird output from kernel (last parts occurs about 10 seconds after turning OFF - system still thinks that drive is connected for a while until it timeouts):

    [1263588.964461] ata6: exception Emask 0x10 SAct 0x0 SErr 0x4090000 action 0xe frozen
    [1263588.964467] ata6: irq_stat 0x00400040, connection status changed
    [1263588.964478] ata6: SError: { PHYRdyChg 10B8B DevExch }
    [1263588.964483] ata6: hard resetting link
    [1263589.687000] ata6: SATA link down (SStatus 0 SControl 300)
    [1263594.686806] ata6: hard resetting link
    [1263595.006827] ata6: SATA link down (SStatus 0 SControl 300)
    [1263595.006837] ata6: limiting SATA link speed to 1.5 Gbps
    [1263600.006678] ata6: hard resetting link
    [1263600.330626] ata6: SATA link down (SStatus 0 SControl 310)
    [1263600.330634] ata6.00: disabled
    [1263600.330645] ata6: EH complete
    [1263600.330653] ata6.00: detaching (SCSI 5:0:0:0)
    [1263600.334909] sd 5:0:0:0: [sdd] Synchronizing SCSI cache
    [1263600.334943] sd 5:0:0:0: [sdd] Synchronize Cache(10) failed: Result: hostbyte=DID_BAD_TARGET driverbyte=DRIVER_OK
    [1263600.334946] sd 5:0:0:0: [sdd] Stopping disk
    [1263600.334955] sd 5:0:0:0: [sdd] Start/Stop Unit failed: Result: hostbyte=DID_BAD_TARGET driverbyte=DRIVER_OK
    

    However drive works fine, no problems or data errors whatsoever, but I'm getting a feeling that it might have been done in a better/safer way, without such timeout-ing.

    Is there a more proper way for disconnecting hot-swap SATA ?


    I've been trying sending a udisksctl power-off -b /dev/sdd command, but while it works fine for USB drives, it does not do anything with SATA. Best thing I figured is to suspend a drive, before powering OFF, but kernel output is this same anyway.


    $ uname -a
    Linux dev 4.4.0-93-generic #116-Ubuntu SMP Fri Aug 11 21:17:51 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
    
    • sudodus
      sudodus over 6 years
      My way to mount and unmount drives is via the mount and umount commands. The drive seems to be identified as /dev/sdd. Have you tried the following command to unmount all partitions on the drive, sudo umount /dev/sdd? before turning it off with the power button? (Please notice that the questionmark is part of the command line.)
    • sudodus
      sudodus over 6 years
      I think mounting depends on how the drive is connected and powered on, and what kind of file systems there are on the drive, and if you want automatic mounting or if you want to control the mounting (with special commands). My eSATA drives behave pretty much like standard SATA drives (I can mount and unmount them like SATA drives).
    • ridgy
      ridgy over 6 years
      I think there is no problem with those messages (I get the same with hot-swap SATA drives). When powering on the drive, it takes some while to spin the drive and so (depending on the drive) it does not respond correctly. And unmounting the drive just means releasing the filesystem; the physical drive is still connected, and the kernel tries to reconnect until some timeout. And udisksctl power-off "depends on the drive itself and the interconnect used" (see man udisksctl).
    • GreggD
      GreggD over 6 years
      Oh, I see that I've asked wrong question. I meant "disconnect" not "unmount" :) I don't have problems with current output, I've been just hoping that there is something within the system to f.e. always expect "hot-swap" from given port or maybe some command to more "safely" unplug/poweroff such device, without just waiting for it to timeout. @sudodus I'm usually unmounting with Caja, but identical effect is with unmount or gnome-disk-utility, but there's no Turn/Power-Off function, like for USB devices.
  • GreggD
    GreggD over 6 years
    Thx, those are valuable informations, but I actually knew all that already and was looking for something else ;)
  • sudodus
    sudodus over 6 years
    @GreggD, Let us hope that someone who knows more about advanced methods with SATA drives can chip in and give you what you are looking for :-)
  • GreggD
    GreggD over 6 years
    Yeah, check my answer below ;) Found that command on some blog.
  • Csabi Vidó
    Csabi Vidó over 3 years
    That's nice, but a bit confusing as you issue a command to sdd and reference a log excerpt for sde. ;-) Bonus points for not using echo | tee which is also possible, but yours looks more elegant to me. Friendly reminder to everyone else: sh may not be what you think it is, it's not bash, zsh or fish. It's dash (Debian Almquist Shell), which was introduced over 10 years ago to speed up Ubuntu's boot up process before Systemd became popular and start up shell scripts were standard. On Alpine Linux you may find ash which is included in Busybox. I tried to improve your answer, please check.
  • Csabi Vidó
    Csabi Vidó over 3 years
    I can only find the message about "Synchronizing SCSI cache" with journalctl --since today -t kernel but the disk I wanted to eject is gone from lsblk. I may be missing something*, the 18.04 machine I am testing this on has quite messy logs and uses zfs, but I checked with "jounalctl --since today | grep -v messystuff" which confirmed that there is no "Stopping disk message". *Uh wait I'm also using an LSI 2308 based HBA, that might be a reason I'm not seeing all of those messages.