Command line equivalent to "Safely remove drive"?

16,968

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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

Solution 5

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
Share:
16,968

Related videos on Youtube

ArdentCertes
Author by

ArdentCertes

Updated on September 18, 2022

Comments

  • ArdentCertes
    ArdentCertes almost 2 years

    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?

    • Hauke Laging
      Hauke Laging almost 7 years
      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""
    • Arkadiusz Drabczyk
      Arkadiusz Drabczyk almost 7 years
      Don't modern window managers use udisks or something similar that work without root?
    • Fox
      Fox almost 7 years
      "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
    • Ferroao
      Ferroao almost 4 years
      after umount, udisksctl power-off -b /dev/sdb
  • Steven Lu
    Steven Lu over 5 years
    udisks isn't even present on my centos 7 minimal server
  • fra-san
    fra-san almost 4 years
    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
    fra-san almost 4 years
    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.
  • jbo5112
    jbo5112 over 3 years
    I wish I could give more than 1 upvote for finally finding a solution that doesn't require utilities I don't have.
  • VPfB
    VPfB about 2 years
    Fedora has` udisksctl power-off -b /dev/sdX`