I have a configuration file config_file
that is stored on an NFSv3 network mount, and I would like to update that file safely.
Here are the conditions and restrictions that I'm working under:
- There must always be a
config_file
available to clients that want to read it - The clients must never read corrupted data from a partially rewritten file
- The clients do not need
config_file
updated immediately. Waiting a minute or two for caches to update is perfectly fine so long as restrictions #1 and #2 are met
The NFSv3 implementations on both ends are RHEL (Red Hat Enterprise Linux).
Here's what I've come up with so far:
The client applications read from /mount/path/config_file
, which is really a symlink to /mount/path/config_file_current
Let:
NEW = Path to the new configuration file that I'm try to install ( /path/to/my/workspace/config_file )
SYM = The path to the symlink ( /mount/path/config_file )
CURRENT = The path to the current version of config_file ( /mount/path/config_file_current )
TMP = The path to a temporary version of config_file ( /mount/path/config_file_tmp )
cp ${NEW} ${TMP} # Initial copy of the new configuration file over the temporary file
ln -f ${TMP} ${SYM} # Point the symlink to the temporary file [1]
rm ${CURRENT} # To prevent torn reads, delete the previous file so that the file handle is "stale"
cp ${TMP} ${CURRENT} # Copy the newest version of config_file back to ${CURRENT}
ln -f ${CURENT} ${SYM} # Point the symlink back to ${CURRENT}
[1] Symlink
creation is one of the only two NFSv3 operations that are defined to be atomic in RFC 1813 (the NFSv3 specification file)
This procedure should be safe on a RHEL system operating on a local filesystem but not across any and all implementations of NFSv3 because a client could attempt to read from the stale file handle.
My question for the Server Fault community is this: Is this procedure safe for an NFSv3 implementation where both client and server are running RHEL?
I'm basing much of this on these two articles:
https://utcc.utoronto.ca/~cks/space/blog/sysadmin/SafelyUpdatingUnixFiles
https://utcc.utoronto.ca/~cks/space/blog/sysadmin/SafelyUpdatingNFSFiles