3

Is umount $mountpoint always enough to safely remove a single-partition USB drive on a modern GNU/Linux system? Or should I use sync && umount $mountpoint && sync, or perhaps umount $mountpoint && udisks --detach $device? I'd like to use the safest way possible, but on the other hand I don't want to introduce superfluous dependencies or delays into my program.

EDIT: Does umount calls sync to complete any pending writes indeed answers a part of this question (whether sync && umount $mountpoint && sync provides any benefit over umount $mountpoint), but not the whole question (whether udisks --detach $device or some other command provides improved safety), so it's not an exact duplicate.

  • 2
    SHort answer: umount is safe and doesn't need anything else, but of course, you have to check its return value to see if the command was effective. – xhienne Mar 27 '17 at 16:05
  • 2
    Unmounting a USB drive only partially makes it safe to remove. It stops I/O to the filesystem, but it doesn't power down the device, making it possible to remount the device. It also doesn't flush any I/O cache to the device, so the data you think may be on the device, may not be. In a gui, you'll often see an option to "Safely Remove" a USB device. That option sync's first, then unmounts, then powers down the device. – Tim Kennedy Mar 28 '17 at 14:23
5

Quote from Eject USB drives / eject command:

eject will work, but will not really "finish the job" regarding USB rotating drives.

The best way to unplug a USB external hard-drive, after proper unmounting, is:

udisks --detach /dev/sdb

This usually causes the drive to spin down gracefully.

EDIT: Also, as g.rocket points out, this command may be equivalent:

udisksctl power-off -b /dev/sdb
| improve this answer | |
  • Does executing this command after unmounting improve protection against data loss/corruption or only reduce power usage? – Pkkm Mar 27 '17 at 18:35
  • 2
    You'd hope it never makes a difference, but I don't know. It does help in the sense that a spinning drive is more vulnerable (docs on mine say "don't move it"), & with a short thick cable it is very hard to unplug without also moving the drive around a bit. – sourcejedi Mar 28 '17 at 7:49

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