7

The file managers present in Linux Mint Cinnamon and MATE not only allow you to unmount external drives, but give you the option to "Safely remove" the drives as well, something not present in every Linux distribution.

When I unmount my external hard drive (powered via USB), its indicator light remains lit and I can still hear it spinning. When I "Safely remove" the drive, the light turns off and the disc stops spinning.

How can I accomplish this same thing via the terminal? As a side question, is this functionality as simple as unmount drive > kill power or is there something more complicated or sophisticated going on behind the scenes? Is this safer than only unmounting the drive and pulling the USB plug out?

4
  • In egneral the only dafe operation is umount so I would agree to your assumption that this is nothing more than "unmount drive + kill power"" Aug 19, 2017 at 10:41
  • Don't modern window managers use udisks or something similar that work without root? Aug 19, 2017 at 12:46
  • "Safely Remove" may or may not use eject under the hood, which itself calls umount. You might see if that replicates the spin-down behavior
    – Fox
    Aug 19, 2017 at 19:57
  • after umount, udisksctl power-off -b /dev/sdb
    – Ferroao
    Jul 22, 2020 at 1:58

5 Answers 5

5

umount is enough. After that it is completely safe to unplug your device.

I quote from this answer:

Unmounting is sufficient for USB/eSATA/etc. storage devices.

If you have a lot of cached data to be written on the drive, you may use this command:

sync

and then watch the progress with:

watch grep -e Dirty: -e Writeback: /proc/meminfo
3

To safely remove your drive you should :

  • unmount the partitions
  • powering down your device

unmount and sync is sufficient but it can't provide a safely remove for your device.

To safely unmount and totally remove the drive, enter the following command, replacing /dev/sdb with your own drive’s designation if need be:

udisks --unmount /dev/sdb1 && udisks --detach /dev/sdb

the udisks manpage:

--detach device_file [--detach-options options]

Detaches (e.g. powering down the physical port the device is connected
to) the device represented by device_file using a comma-separated list of 
options.

Ubuntu USB Storage: How to Safely Remove Drive When Eject and Unmount Are Only Options

2
  • 1
    udisks isn't even present on my centos 7 minimal server
    – Steven Lu
    Oct 21, 2018 at 23:05
  • Fedora has` udisksctl power-off -b /dev/sdX`
    – VPfB
    Mar 29 at 19:57
2

After unmounting and syncing, the way to safely remove drive sdX without any tools would be (as root):

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

If you want to do it through sudo, you'll need to do it this way:

echo 1 | sudo tee /sys/block/sdX/device/delete >/dev/null

This will tell the kernel that you're going to remove the device. When you do that, the kernel will take care of cleaning up any unfinished tasks with the device and then put the device in question into a ready-for-disconnect state; for example, USB HDDs may spin down when this command is used.

Some versions of the eject command are for CD/DVD/BluRay only, but others may have the ability to perform the same procedure as the commands above for any disk device. So depending on distribution, eject /dev/sdX might also work, or it might not.

1
  • I wish I could give more than 1 upvote for finally finding a solution that doesn't require utilities I don't have.
    – jbo5112
    Mar 16, 2021 at 17:27
1

I wrote a script: x-drv [device name]; I find 'eject' is more for CD/DVD's; you might have to modify where your stuff mounts; I put this in my user 'bin' directory, no './' to run the script

# x-drv
# safely remove drive
if [ "$#" = 0 ]
then
   echo -e "\nusage: x-drv drive_name\n"
else
   dev_pth=/media/`whoami`/$1
   if [ -d $dev_pth ]
   then
      tgt_dev=`findmnt -n -r -o SOURCE $dev_pth`
      echo -e "ejecting: $dev_pth - $tgt_dev"
      umount $tgt_dev
      udisksctl power-off -b $tgt_dev
      echo -e ">>> safe to remove <<<\n"
   else
      echo -e "$dev_pth - not mounted\n"
   fi
fi
1
  • The most relevant part, udisksctl power-off -b block_device, is a bit buried. It may be worth making it explicit that uidsksctl power-off "Arranges for the drive to be safely removed and powered off" in udisks2.
    – fra-san
    Jul 3, 2020 at 11:09
1

One can use utility udisks2 (instead of udisks), in the manner mentioned by GAD3R.

The binary in this package is udisksctl.

Ref: https://askubuntu.com/questions/647473/why-udisks-is-not-in-the-system

1
  • It may be worth adding that the command(s) in udisks2 (as opposed to the mentioned usisks counterpart) would be udisksctl unmount -b /dev/sdb1 && udisksctl power-off -b /dev/sdb.
    – fra-san
    Jul 3, 2020 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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