2

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
  • 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. Commented Aug 12, 2017 at 8:37
  • 1
    mount --move requires mount --make-private /parent-mount-point which has implications.
    – Tom Hale
    Commented Sep 3, 2017 at 7:45

2 Answers 2

2

lsof can be used on a device:

lsof +f -- /dev/device
2
  • @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
    Commented Sep 3, 2017 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
    Commented May 9, 2021 at 17:06
1

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.

3
  • Cheers! I'm happy to umount -f a read-only filesystem.
    – Tom Hale
    Commented Aug 14, 2017 at 5:32
  • @TomHale umount -f is relevant only for NFS mounts. Commented Aug 14, 2017 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
    Commented Aug 14, 2017 at 7:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .