I am working on a safe and race-condition-free alternative to umount -l
with removable devices:
I'm planning on:
umount --move
under a000
permissions directory so no more files can be opened by absolute path- Interactively kill (or gracefully shutdown) processes with files open for writing
- Atomically remount read-only only if step (2) is complete
- Interactively kill / close read-only processes that may cause cause issues
- Finally have
umount
succeed
There is a race condition in step (3) where a file with relative path could be opened rw
after the last interactive kill and before the mount -o remount,ro
.
Is mount -o remount,ro
guaranteed to fail if there is any file on the filesystem opened for writing?
The manual page is silent on this, and I'm a bit paranoid after finding out that devices are writable even after blockdev --setro
.
umount
will only succeed if all open files are closed. I'm generally ok in killing all ro processes, but I first want to investigate and gracefully close anyrw
processes so as to avoid in-file corruption. I want to avoid newrw
processes being created while all that is going on. As it turns out,umount -f
won't work on a non-NFS filesystem, sofuser -kM
seems to be required for remainingro
processes. – Tom Hale Aug 14 '17 at 07:40umount
. That does leave the race with new processes, but it’s not as if there’s a gremlin constantly starting processes writing to your removable drive, or at least there shouldn’t be... – Stephen Kitt Aug 14 '17 at 08:08umount
... Do you mean afuser -kiM /mountpoint && umount /mountpoint
? I am trying to get the advantages ofumount --lazy
without the non-determinism. – Tom Hale Aug 14 '17 at 08:21--lazy
is a safe option that you can use for some imagined convenience, when it's really an option that you shouldn't use except as a last resort (when nothing else works and you really have to get that fs unmounted NOW before anything new writes to it), because it is inherently dangerous to the data and/or metadata on the fs. You can not use it routinely and expect not to have race-conditions and potential fs corruption to deal with. – cas Aug 14 '17 at 08:59umount
” in my mind just means “runumount
, if it succeeds, good, if it doesn’t, figure out why and try again”. Your putative approach still involves the figuring out part, so I don’t see much point in making it any more complex. – Stephen Kitt Aug 14 '17 at 09:50umount -l
. My proposed approach in the question is precisely because I don't want to use it but still want to prevent writes based on absolute filenames by moving the namespace. – Tom Hale Aug 17 '17 at 07:22