I want to remove my external HDD as safely as possible.

I want to use umount --lazy:

Lazy unmount. Detach the filesystem from the file hierarchy now, and clean up all references to this filesystem as soon as it is not busy anymore. (Requires kernel 2.4.11 or later.)

Then after a short delay I plan to kill any processes with open files on the device where the filesystem is still quasi-mounted.

  • I can't use lsof for an accurate list of the open files as the filesystem has become invisible to new processes.
  • If I use lsof before umount -l, there is a race contition of a new file being opened in between the two invocations.

Is there any way of finding out which processes are accessing a DEVICE rather than a filesystem?

  • 2
    umount --lazy is not safe and can not be made safe. You can achieve something similar with mount --move but the best method is still the old-fashioned regular umount. Aug 12 '17 at 8:37
  • 1
    mount --move requires mount --make-private /parent-mount-point which has implications.
    – Tom Hale
    Sep 3 '17 at 7:45

lsof can be used on a device:

lsof +f -- /dev/device
  • @Thomas, I'm confused: I was the author of the question. Perhaps because of this, I'm not seing things clearly. How does this not answer: Is there any way of finding out which processes are accessing a DEVICE rather than a filesystem?
    – Tom Hale
    Sep 3 '17 at 14:46
  • This works for me. My portable harddisk is hang halfway during a rsync process and I close that window. It then could not be unmounted since there still process accessing it. To find out which process, 'lsof +D /directory' can't help and hang also. Finally 'lsof +f /dev/device' let me find it out. Thanks.
    – sylye
    May 9 '21 at 17:06

You could try to remount the volume read-only. This works only if nothing on that volume is opened for writing.

You will probably not get rid of the race condition that a file could be opened read-only or that a process could have its current working directory on that volume but if you detach the hardware then you can at least be sure that its file system is in order.

  • Cheers! I'm happy to umount -f a read-only filesystem.
    – Tom Hale
    Aug 14 '17 at 5:32
  • @TomHale umount -f is relevant only for NFS mounts. Aug 14 '17 at 6:07
  • Dammit! fuser -k it must be then. I've had issues with device names incrementing (sdb becomes sdc) and btrfs corruption if the thing isn't fully gone.
    – Tom Hale
    Aug 14 '17 at 7:26

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.