Reputation: 83
There can be several mount points under management in linux. I want to umount them all or don't umount any. Since there are cases when linux cannot umount a device (like someone is on the mount point), I want to add a function to check all mount points and see if the devices can be umounted before I actually perform umount action.
Is there any functions like trylock
to test if a device is umountable? Or if there are any function to check if any user is using the mount point like lsof
?
Upvotes: 0
Views: 666
Reputation: 31296
This answer describes it pretty well. Although the question is quite different, the answer is the same.
You can never know if an unmount in the future will succeed or not. You can find out if it would have worked (and hardly even that, because the check action is by definition not equivalent to the unmount action) when you checked it, but that information is useless a nanosecond later.
The only way this could work is if you find some way to lock the mount point, preventing it from being used by other processes, before the unmount.
Upvotes: 2
Reputation: 19313
You can use fuser -m /mountpoint
to see if any processes are using the mount point.
Note that, as Felix noted, it is very possible that some process is going to grab mount point after your check but before you issue umount
.
Upvotes: 1
Reputation:
There isn't a way, AFAIK. And that's ok because your idea is flawed, it's a classic case of a TOCTOU race condition. Between checking whether umount()
would succeed and actually performing it, any other process might change the outcome.
Upvotes: 3