6

How to eject SATA device properly in Linux? I know eject command can do it for usb device:

eject usbDevicePath 

Does it work same way for SATA devices? Will it sync caches, and properly power down SATA device?

2 Answers 2

9

The solution is to spin down the drive via software before turning it off and unplugging it. The best way to do this is with a utility called scsiadd. This program can add and remove drives to Linux’s SCSI subsystem. Additionally, with fairly modern kernels, removing a device will issue a stop command, which is exactly what we’re looking for.

Run:

$ sudo scsiadd -p

which should print something like:

Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
  Vendor: ATA      Model: SAMSUNG HD300LJ  Rev: ZT10
  Type:   Direct-Access                    ANSI  SCSI revision: 05
Host: scsi4 Channel: 00 Id: 00 Lun: 00
  Vendor: LITE-ON  Model: DVDRW LH-20A1L   Rev: BL05
  Type:   CD-ROM                           ANSI  SCSI revision: 05
Host: scsi5 Channel: 00 Id: 00 Lun: 00
  Vendor: ATA      Model: WDC WD10EACS-00Z Rev: 01.0
  Type:   Direct-Access                    ANSI  SCSI revision: 05

Identify the drive you want to remove and then issue:

$ sudo scsiadd -r host channel id lun

substituting the corresponding values from the scsiadd -p output. For example, if I wanted to remove “WDC WD10EACS-00Z”, I would run:

$ sync & sudo scsiadd -r 5 0 0 0

If everything works, scsiadd should print:

Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
  Vendor: ATA      Model: SAMSUNG HD300LJ  Rev: ZT10
  Type:   Direct-Access                    ANSI  SCSI revision: 05
Host: scsi4 Channel: 00 Id: 00 Lun: 00
  Vendor: LITE-ON  Model: DVDRW LH-20A1L   Rev: BL05
  Type:   CD-ROM                           ANSI  SCSI revision: 05

You can double-check the end of dmesg. You should see:

[608188.235216] sd 5:0:0:0: [sdb] Synchronizing SCSI cache
[608188.235362] sd 5:0:0:0: [sdb] Stopping disk
[608188.794296] ata6.00: disabled

At this point, the drive is removed from Linux’s SCSI subsystem and it should not be spinning. It’s safe to unplug and turn off.

2
  • 1
    Thank you, this works. Checked, scsiadd does not sync cache before turning off so we need to do it manually. Also, my WD HDD turned off in ~10 seconds after running the command. Commented Jun 2, 2013 at 21:36
  • 1
    What package is scsiadd from? I don't have that command available on my system. Commented Apr 20, 2019 at 7:31
14

This answer was most useful: after umounting, stopping LVM, LUKS, etc. you do:

echo 1 >/sys/block/sdX/device/delete

And since this answer seems to have attracted *buntu users who don’t know how to use sudo(8) with redirections correctly, here’s the easiest way to do it with sudo:

echo 1 | sudo dd of=/sys/block/sdX/device/delete

The literal way is:

sudo sh -c 'echo 1 >/sys/block/sdX/device/delete'

An otherwise unquoted redirection is handled by the shell outside of the call to sudo instead, and you need the redirection, not the echo, to be run with superuser privilegues.

7
  • This did remove it from lsblk, but what about stopping the drive from spinning? Commented Apr 20, 2019 at 7:33
  • Mine did that automatically, configure power saving before deleting the device if necessary… or just unplug it.
    – mirabilos
    Commented Apr 21, 2019 at 0:50
  • 1
    This was much easier than the selected answer - and did the job. Commented Mar 26, 2020 at 13:47
  • Had to do this as root, sudo didn't work on ubuntu 20.04 LTS. Commented Feb 1, 2022 at 1:07
  • @AndyArismendi using sudo is doing as root, but you probably misused it and did the wrong thing as root; I extended the answer to provide for people who are unskilled at system administration by adding explicit sudo examples.
    – mirabilos
    Commented Feb 1, 2022 at 1:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .