views:

1314

answers:

4

how to create a loop in linux filesystem ? I want to break the directed acyclic graph(DAG) property of linux file system.. Is it possible?... I have seen this condition(loop in filesystem) once when I installed scratchbox cross compilier in my ubuntu linux distro... .. I dunno how to reproduce it now...

A: 
mount /path/to/device /path/to/mount/location -o loop

where /path/to/device is the path to either the partition you want to mount, or the path to a disk image, and /path/to/mount/location is the path to the folder you want to mount the device/image under

you may also need to include the type of the file system like so (which uses fat16/fat32):

mount /path/to/device /path/to/mount/location -o loop -t vfat
Jiaaro
+2  A: 

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

Eric Melski
+1  A: 

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.

ephemient
A: 

You may also want to create one from scratch:

First create the image file and initialize it

dd if=/dev/zero of=/tmp/loop.img bs=1024k count=$IMG_SIZE

Next, make it a valid partition using an FS type of your choice

mkfs.ext3 -F /tmp/loop.img

Mount your new image

mkdir -p /mnt/image
mount /tmp/loop.img /mnt/image -o loop

You can now create/copy files and directories in your new image.

Have fun,

Jeach!

Jeach