Safely remove drives from terminal

diskHave you noticed that different file manager apps provide different options to remove drives? You probably have sees the options unmount, eject and safely remove drives. In this article we’ll explore these options briefly and then hack a way to safety detach removable USB disk drives from a Linux system.

Typically there are multiple stages involved when the intention is to physically remove (or disconnect) a device, safely: sync all cached reads/writes, complete ongoing transfers, close open files and finally power down the device.

The options

  • unmount: The umount command detaches a filesystem from the file hierarchy. It cannot be unmounted while it is busy, i.e., any file is still open with some process. unmount syncs the data too. However the disk is not powered down. That’s why unmount doesn’t remove the device icon from your file manager, you can mount the device again.
  • eject: historically related to CD/DVD (optical) drives where you literally eject the tray. Note that the devices are not synced. However, in case of disks eject runs a check if any data transfer that needs to be complete is ongoing. If that’s the case, it will show a message. eject powers down a device. ejecting a device with multiple volumes is not safe.
  • safely remove: this is the correct option that should be shown in any file manager. This is not a single operation and involves cached data sync as well as power down. Once complete, the device icon will not be shown by the file manager. You need to disconnect and reconnect the device.

Command-line

How do you safely remove a disk from the terminal when your file manager (e.g. PCManFM) doesn’t show the option?

For all the mounted volumes, run:

$ udisksctl unmount -b /dev/sdXn

where n is the volume number.

Then power down the device:

$ udisksctl power-off -b /dev/sdX

Leave a Reply

Your email address will not be published. Required fields are marked *