Skip to content

Instantly share code, notes, and snippets.

@zachwalton
Created September 5, 2014 18:34
Show Gist options
  • Save zachwalton/3ce41e064f177635d05f to your computer and use it in GitHub Desktop.
Save zachwalton/3ce41e064f177635d05f to your computer and use it in GitHub Desktop.
Discover FDs on lazily umounted filesystems in Linux
There's no "right" way to do this, so...
When you lazily umount a filesystem in Linux, procfs changes /proc/pid/fd/* from absolute to relative paths. E.g. if you have a tail open on the FS:
[uid] 31624 0.0 0.0 100924 616 pts/86 Ss+ 10:12 0:00 tail -f /mnt/test
$ readlink /proc/31624/fd/3
/mnt/test
Then lazily umount the FS:
$ umount -fl /mnt
$ readlink /proc/31624/fd/3
test
...Linux changes the absolute path to a relative one. There are only three valid scenarios in which a symlink in /proc/[pid]/fd can point to a relative path:
1) The symlink points to a [socket] or [pipe]
2) The symlink points to "inotify"
3) The symlink points to a file on a lazily umounted FS.
So we can find all symlinks in /proc/*/fd, readlink them, get rid of the ones pointing to a socket, pipe, or inotify FD, get rid of ones pointing to absolute paths, and whatever is left is an open file descriptor for a lazily umounted filesystem.
Solution pasted below.
for lnk in $(find /proc/*/fd -type l 2>/dev/null | grep -v $$); do
readlink ${lnk} | egrep -v "[\[/]|inotify" >/dev/null &&
echo "lazily umounted file discovered at: ${lnk}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment