Ejecting SATA device in Linux
Solution 1
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.
Solution 2
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.
Related videos on Youtube
Sergei Krivonos
Updated on September 18, 2022Comments
-
Sergei Krivonos almost 2 years
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?
-
Sergei Krivonos about 11 yearsThank 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.
-
Aaron Franke about 5 yearsWhat package is
scsiadd
from? I don't have that command available on my system. -
Aaron Franke about 5 yearsThis did remove it from
lsblk
, but what about stopping the drive from spinning? -
mirabilos about 5 yearsMine did that automatically, configure power saving before deleting the device if necessary… or just unplug it.
-
Maxim Imakaev over 4 yearsThis was much easier than the selected answer - and did the job.
-
Andy Arismendi over 2 yearsHad to do this as root, sudo didn't work on ubuntu 20.04 LTS.
-
mirabilos over 2 years@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.
-
Andy Arismendi over 2 yearsHmm didn't know about the idiosyncracy with piping and sudo. I assumed sudo took it's whole argument and ran it as root. I guess the shell evaluates the redirection char before setting up the sudo process with it's args.
-
mirabilos over 2 years@AndyArismendi exactly, this is how the shell works. (I maintain one.) But I suggest to delete your last two comments (and I’ll do mine) as this is offtopic and too chatty here.