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?

Tom Hale
  • 30,455

2 Answers2

2

lsof can be used on a device:

lsof +f -- /dev/device
Tom Hale
  • 30,455
  • @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 03 '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 09 '21 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.

Hauke Laging
  • 90,279