On Linux, as an alternative to bind mount, you can also use overlay mount to link one file with another temporarily. The advantage of an overlay is that you can change overlay without needing root privilege, unlike bind mount which will require root privilege to shadow and unshadow files.
Demonstration:
Create a test partition with some test files (note that you need root to create a mountpoint that isn't listed in /etc/fstab, but you won't need root for any of the other steps):
$ mkdir one two work mpnt
$ sudo mount -t overlay none -olowerdir=one,upperdir=two,workdir=work mpnt
$ echo "hello world" > one/abcd
$ echo "another" > one/xyz
We created the test files in folder one
above, these files are also visible in the mount point mpnt
.
$ ls -lh mpnt
total 8.0K
-rw-r--r-- 1 lieryan lieryan 12 Jun 11 03:33 abcd
-rw-r--r-- 1 lieryan lieryan 8 Jun 11 03:33 xyz
Let's modify the files in mpnt:
$ ln -sf abcd mpnt/xyz
We modified mpnt/xyz above to be a symlink, but overlayfs redirects all writes to mpnt to the upperdir, so the symlink we created are actually written to two
, we can see the symlink in folder two
as well as mpnt
:
$ ls -lh two
total 0
lrwxrwxrwx 1 lieryan lieryan 4 Jun 11 03:34 xyz -> abcd
$ ls -lh mpnt
total 4.0K
-rw-r--r-- 1 lieryan lieryan 12 Jun 11 03:33 abcd
lrwxrwxrwx 1 lieryan lieryan 4 Jun 11 03:34 xyz -> abcd
The symlink works as expected when used from mpnt
:
$ cat mpnt/xyz
hello world
If we delete something from mpnt, overlayfs will also redirect the write to upperdir:
$ rm mpnt/xyz
In this case though, overlayfs actually creates a special character device file (notice the c attribute), called a whiteout file. When overlayfs sees this type of file in the upperdir, overlayfs pretends that the file don't exist in mpnt
:
$ ls -lh mpnt
total 4.0K
-rw-r--r-- 1 lieryan lieryan 12 Jun 11 03:33 abcd
$ ls -lh two
total 4.0K
c--------- 1 lieryan lieryan 12 Jun 11 03:33 xyz
We can remove the special character file from the upperdir to restore xyz from lowerdir:
$ rm two/xyz # delete the "whiteout" file
rm: remove write-protected character special file 'two/xyz'? y
Now xyz
is visible again in mpnt, and has the same file content as one/xyz.
$ ls -lh mpnt
total 8.0K
-rw-r--r-- 1 lieryan lieryan 12 Jun 11 03:33 abcd
-rw-r--r-- 1 lieryan lieryan 8 Jun 11 03:33 xyz
$ cat mpnt/xyz
another
mv xyz xyz.orig
– n.caillou Jun 10 '17 at 16:34