What does Linux do with existing files in a mount point?

52

13

If I attempt to mount a folder that already has files in it, does linux give me an error message or go ahead and show both the mounted filesystem and the files that were already in the folder?

slim

Posted 2010-10-18T14:06:12.470

Reputation: 767

Is there a way to make the folder unwriteable so that files can't exist there? – endolith – 2016-09-26T23:57:28.497

2Could always try it out with some test files, no? – Chris – 2010-10-18T14:35:32.463

I would of if I could. It just worked out that I didn't have anything to test with. I tried unmounting and mounting the drive in question but the results were inconclusive because they both had the same files. – slim – 2010-10-18T14:39:01.553

Answers

33

It will just be mounted, and the files disappear, coming back when the folder is umounted.

Azz

Posted 2010-10-18T14:06:12.470

Reputation: 3 777

Is this the same if I remount instead of explicitly mounting? – CMCDragonkai – 2015-08-03T10:43:38.700

4I'm confused by how someone who said "coming back when the folder is unmounted" was able to say 3 minutes later "I think they are deleted". Thankfully for everyone else, the former is the reality here. – underscore_d – 2015-10-06T00:24:49.400

1What do you mean by disappear? They continue to exist on the server and are just not shown or are the deleted? – slim – 2010-10-18T14:08:37.037

I'll go have a quick check, but I think they are deleted. – Azz – 2010-10-18T14:10:04.817

My bad, they go away while it's mounted, but come back when it's umounted. – Azz – 2010-10-18T14:11:46.577

17+1 The files are simply invisible while the directory is mounted "over" them. They never really go away, the are just inaccessible... – sleske – 2010-10-18T14:50:04.910

10It works like a stack, if you mount something else, it hides the previous content. When you unmount, the previous stuff becomes visible again. – vtest – 2010-10-18T15:09:29.183

112

When you mount a filesystem on a directory /mount-point, you can no longer access files under /mount-point directly. They still exist, but /mount-point now refers to the root of the mounted filesystem, not to the directory that served as a mount point, so the contents of this directory cannot be accessed, at least in this way. For example:

# touch /mount-point/somefile
# ls /mount-point/somefile
/mount-point/somefile
# mount /dev/something /mount-point
# ls /mount-point/somefile
ls: cannot access /mount-point/somefile: No such file or directory

There are ways to get a merged view of the mounted filesystem and the data that was already present, but you need an extra layer called a union filesystem.

Under Linux, there is a way to see the hidden files. You can use mount --bind to get another view of the filesystem where the mount point is. For example

mount --bind / /other-root-view

You'll see all the files in the root filesystem under /other-root-view.

# cat /other-root-view/etc/hostname 
darkstar

In particular, /mount-point will now be accessible as /other-root-view/mount-point, and since /other-root-view/mount-point is not a mount point, you can see its contents there:

# ls /mount-point/somefile
ls: cannot access /mount-point/somefile: No such file or directory
# ls /other-root-view/mount-point/somefile
/other-root-view/mount-point/somefile

Gilles 'SO- stop being evil'

Posted 2010-10-18T14:06:12.470

Reputation: 58 319

What about directories? If I mounted at /mount-point/1/ then mounted another filesystem on /mount-point/, can I still access /mount-point/1/? – CMCDragonkai – 2015-11-18T12:57:14.227

@CMCDragonkai Yes, indirectly by using a bind mount as described in my answer. – Gilles 'SO- stop being evil' – 2015-11-18T17:46:52.120

Giles, this is a brilliant technique and has helped me analyze what’s on my own system. It has also helped with another question, which is how to check usage of all of the root directories without traversing mount points. Solution: mkdir /r; mount --bind / /r; du -sh /r/*. Thanks – Manngo – 2016-05-31T23:52:45.887

@Manngo for future reference, this is not necessary. du -x (equivalent to du --one-file-system) would have done that without the need for --bind shenanigans. – Darael – 2017-02-21T12:57:27.863

@Darael mount --bind makes a difference: if a filesystem is mounted on a non-empty directory in /, then du -x / will not count what's in that directory, because it's hidden by the mounted filesystem. With mount --bind you can get an unobstructed view of a filesystem. – Gilles 'SO- stop being evil' – 2017-02-21T13:00:23.540

@Gilles That's a very good point, though in general for "how to check usage of all the root directories without traversing mount points" I'd imagine it's unlikely there's much if anything being shadowed by mounts. – Darael – 2017-02-21T13:01:54.780

5Gilles, this answer just saved my butt when I needed to get some asterisk recordings that got saved underneath an NSF mount point! I always thought --bind had the same perspective as the user. Thank you! – andyortlieb – 2011-07-19T14:55:01.050