3

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?

  • 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"" – Hauke Laging Aug 19 '17 at 10:41
  • Don't modern window managers use udisks or something similar that work without root? – Arkadiusz Drabczyk Aug 19 '17 at 12:46
  • @Arkadiusz Why do you use code formatting on things which are not code? – tripleee Aug 19 '17 at 13:04
  • @tripleee: because I can. – Arkadiusz Drabczyk Aug 19 '17 at 13:04
  • "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 '17 at 19:57
3

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
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

  • udisks isn't even present on my centos 7 minimal server – Steven Lu Oct 21 '18 at 23:05
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

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.