Wednesday 20 November 2013

How I can mount one file (not an folder) to another?

Create file with content:
echo "my important data" > /tmp/shadow

Check it content:
cat  /tmp/shadow
my important data

Create blank file, it will be mount point, it content can be any (we need only inode, right):
rm -f /tmp/shadowcopy
touch /tmp/shadowcopy

This file instantly blank:
cat /tmp/shadowcopy|wc -l

Mount file /tmp/shadow to /tmp/shadowcopy:
mount --bind /tmp/shadow /tmp/shadowcopy 

Welcome, all forks fine:
cat /tmp/shadow
my important data

cat /tmp/shadowcopy
my important data

You can add line into source file:
echo "new line" >> /tmp/shadow

And this line can be found in both files (source and mounted):
cat /tmp/shadow
my important data
new line

cat /tmp/shadowcopy
my important data
new line
 If you add any content to destination file, it will show in source. This mechanic looks like hard links. 

At finish you MUST unmount destination file:
umount /tmp/shadowcopy


  1. This comment has been removed by the author.

    1. Например, для подмены файлов поверх read only fs.


Note: only a member of this blog may post a comment.