3

/dev/sda5 is mounted at / and it's my filesystem

piotr@thinkpad:~$ sudo mkdir /home/mpoint
piotr@thinkpad:~$ sudo mount /dev/sda5 /home/mpoint

so now I can do:

piotr@thinkpad:~$ cd /home/mpoint/home/mpoint

and when I'm in second mpoint directory, it's empty.

My questions are:

  1. Why is it possible to "loop" a filesystem by mounting it to one of its folders?

  2. Why this second mpoint dir is empty?

4
  • What happens if you run sudo mount /dev/sda5 /home/mpoint/home/mpoint ? Commented Aug 12, 2021 at 14:39
  • 1
    sure... so, why shouldn't you be able to mount to any directory even on the same drive? The second mpoint is empty because if it's not a mount point, it's just an empty directory. Would be interesting to see what happens if you try to create files in that empty directory Commented Aug 12, 2021 at 14:43
  • @pLumo it works and I can go to /home/mpoint/home/mpoint/home/mpoint Commented Aug 12, 2021 at 14:43
  • I assume that is then empty (as it is an empty directory where nothing is mounted. @pLumo: I assume files created there would work as expected for files in a directory where you mount stuff, i.e. be hidden by the mount. Commented Aug 12, 2021 at 14:48

2 Answers 2

3

When mounting a file system one add an extra layer for the system. The mount-point is an absolute path that normally hides the contents of the target. To have a closer look using stat can be of help. I.e:

First prepare a test case:

# mkdir /mnt/other
# echo hi > /mnt/other/hello.txt
# cat /mnt/other/hello.txt
hi
# stat -c %i /mnt/other
6424680

Then mount

# mount / /mnt/other
# cat /mnt/other/hello.txt
cat: cannot access '/mnt/other/hello.txt': No such file or directory
# stat -c %i /mnt/other
2
# stat -c %i /
2
# stat -c %i /mnt/other/mnt/other
6424680
# cat /mnt/other/mnt/other/hello.txt
hi

As you can see the inode for the mount-point changes from inode 6424680 to 2 (which is the inode for root directory1). The contents of that directory is hidden as far as the mount point is concerned, but still present on the disk. It is a layer above the file-system.

1 and parent to root, . and ..


The path /mnt/other/mnt/other is not a mount-point and holds the original content for the inode of that directory. In your case it is empty, in my case it has one file; Though you could make it a mount point as well:

# mount / /mnt/other/mnt/other
# stat -c %i /mnt/other/mnt/other
2
# stat -c %i /mnt/other/mnt/other/mnt/other
6424680
# cat /mnt/other/mnt/other/mnt/other/hello.txt
hi

As for why, then why not. Unless it damages the system let the user do what ever they want. That's not saying one can not do things that damages the system albeit in this case the incentive to add a blocker is not present from what I can see. For what ever reason people might even want to create another view at a mount-point even of the root tree itself.

Further *nix systems have a single directory tree as opposed to e.g. Windows with drives that has letters A:, B:, C:, . As per start of man mount:

All files accessible in a Unix system are arranged in one big tree, the file hierarchy, rooted at /. These files can be spread out over several devices. The mount command serves to attach the filesystem found on some device to the big file tree.

8
  • No, a mount point is not an "absolute path". See mkdir -p foo/bar quux; mount -B / foo/bar; mv foo quux/baz: At the end of that, qqux/baz/bar is the same mount point foo/bar was and you can unmount it with umount quux/baz/bar (bind-mount used for convenience, it makes no difference) Commented Aug 15, 2021 at 12:14
  • Even if it damages the system. Unix/Linux is a very powerful system. Shooting oneself in the foot is allowed, but avoidable, if one doesn't commit UTBLT (Using Tool Before Learning Tool). Commented Aug 15, 2021 at 15:40
  • @Căcărău Sure one can mount, move tings around or navigate using relative paths - but the mount-point when accessed is absolute. If not one could cd /mnt/other/mnt/other/mnt/other/mnt/other ... ad infinity after first mount. Commented Aug 16, 2021 at 21:37
  • @waltinator: Point I was trying to make is that if it does not damage the system it's even less incentive to add a blocker. It could be better formulated I guess. I'll try to re-phrase. Right now its pizza. Commented Aug 16, 2021 at 21:43
  • @ibuprofen 1. from the point of view of the OS (the entity which is managing mounts), any path is absolute. But a mount point is not a "path", as my example demonstrates. 2 "If not one could cd .. ad infinity" yet they don't, despite mounts NOT being implemented as you suggest ;-) Commented Aug 18, 2021 at 11:41
3

I'll answer your questions in reverse order.

  1. Why this second mpoint dir is empty?

Because nothing is there - the directory is empty and noting is mounted on that location.

  1. Why is it possible to "loop" a filesystem by mounting it to one of its folders?

Probably because it would be a lot of work to prevent it and nothing bad can really come from it. And to address your comment: No directory gets more than one parent by this, /home/mpoint is not the same directory as /home/mpoint/home/mpoint.

3
  • Exactly. Why should it not be possible? Commented Aug 12, 2021 at 14:45
  • right, my question about beeing empty was stupid one... but @pLumo I think it should not be possible because in Linux fs each directory can have only one parent directory, I studied this while I was wondering why I can't use hard links to directories Commented Aug 12, 2021 at 14:50
  • But, each directory has only one parent. Commented Aug 12, 2021 at 14:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.