How do I create a loop in the Linux filesystem? I want to break the directed acyclic graph (DAG) property of the Linux filesystem. Is this possible? I have seen this condition once when I installed the scratchbox cross compiler on my Ubuntu.
I don't know how to reproduce it now.
Some other responders have already answered how to set up a mount using the loopback device, but you specifically asked about bind
mounts, which are a little bit different. If you want to use a bind mount, you just specify --bind
in the mount command. For example:
mount --bind /original/path /new/path
This will make the filesystem location accessible at /original/path
also accessible through /new/path
. Note that this will not following mountpoints! For example, suppose I have the following mountpoints:
/something
/something/underneath/that
Now suppose I make a bind
mount for /something
:
mount --bind /something /new_something
I will be able to access files like /something/myfile
via the path /new_something/myfile
. But I will not be able to access files like /something/underneath/that/otherfile
via the path /new_something/underneath/that/otherfile
. You must set up a separate bind
mount for each filesystem; or if you have a relatively new kernel, you can use rbind
mounts, which do follow mountpoints:
mount --rbind /something /new_something
One caveat about rbind
mounts: they do not handle the case where a filesystem is mounted after the rbind
is setup. That is, suppose I have a mount like this:
/something
Then I set up my rbind
as above, and then I mount /something/underneath/that
: the rbind
will not magically make the new mount visible through the rbind
location. Also be aware that apparently due to a bug in the kernel, you cannot unmount an rbind
mount.
Also, just in case you meant "How do I set up bind mounts using the mount(2) system call?": you must specify the MS_BIND
flag (defined in mount.h
) when you call mount(2)
for a regular bind
mount. For an rbind
mount, you must specify MS_BIND
and the undocument MS_REC
flag (defined in linux/fs.h
).
Hope that helps,
Eric Melski
It looks like all the answers so far are about mounting on loopback devices, and not creating a loop using bind mounts.
As you've probably discovered,
$ mkdir -p test/test
$ mount --bind test test/test
only allows you to access test/test/test
, and no further. Not even
$ mount --rbind test test/test
works, because the recursive bind-mount effectively goes through finding existing mounts on the source and binding them in the target.
What you've asked for isn't possible, since bind mounts don't cross mount points. If you really wish to simulate a filesystem loop, try use a pseudo-bind mount like localfs. I haven't tried myself, it may lock up when trying to read a filesystem provided by itself. Just now, I tried exporting a NFS tree with crossmnt
and mounting it under itself, but fails for similar reasons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With