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.
如何在Linux文件系统中创建循环?我想打破Linux文件系统的有向无环图(DAG)属性。这可能吗?当我在我的Ubuntu上安装了scratchbox交叉编译器时,我曾经看到过这种情况。
I don't know how to reproduce it now.
我现在不知道如何重现它。
4 个解决方案
#1
6
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:
其他一些响应者已经回答了如何使用loopback设备设置挂载,但是您特别询问了绑定挂载,这有点不同。如果要使用绑定装载,只需在mount命令中指定--bind即可。例如:
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:
这将使/ original / path中可访问的文件系统位置也可通过/ new / path访问。请注意,这不会跟随挂载点!例如,假设我有以下挂载点:
/something
/something/underneath/that
Now suppose I make a bind
mount for /something
:
现在假设我为/ 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:
我将能够通过路径/ new_something / myfile访问/ something / myfile等文件。但是我无法通过路径/ new_something / under / that / otherfile访问/ something / under / that / otherfile等文件。您必须为每个文件系统设置单独的绑定装载;或者如果你有一个相对较新的内核,你可以使用rbind挂载,它遵循挂载点:
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:
关于rbind挂载的一个警告:它们不处理在设置rbind之后安装文件系统的情况。也就是说,假设我有这样的坐骑:
/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.
然后我按上面的方式设置了我的rbind,然后我挂载/下面/下面的那个:rbind不会神奇地通过rbind位置看到新的挂载。还要注意,显然由于内核中的错误,您无法卸载rbind挂载。
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
).
另外,如果您的意思是“如何使用mount(2)系统调用设置绑定挂载?”:当您为常规绑定调用mount(2)时,必须指定MS_BIND标志(在mount.h中定义)安装。对于rbind挂载,必须指定MS_BIND和undocument MS_REC标志(在linux / fs.h中定义)。
Hope that helps,
希望有所帮助,
Eric Melski
埃里克·梅尔斯基
#2
1
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.
您所要求的是不可能的,因为绑定装载不会跨越装载点。如果您真的希望模拟文件系统循环,请尝试使用像localfs这样的伪绑定安装。我没有试过自己,它可能会在尝试读取自己提供的文件系统时锁定。刚才,我尝试使用crossmnt导出一个NFS树并将其安装在自己的下面,但由于类似的原因而失败。
#3
-1
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
其中/ path / to / device是要安装的分区的路径,或者是磁盘映像的路径,/ path / to / mount / location是要安装设备/映像的文件夹的路径下
you may also need to include the type of the file system like so (which uses fat16/fat32):
您可能还需要包含文件系统的类型(使用fat16 / fat32):
mount /path/to/device /path/to/mount/location -o loop -t vfat
#4
-1
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
接下来,使用您选择的FS类型将其设置为有效分区
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!
#1
6
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:
其他一些响应者已经回答了如何使用loopback设备设置挂载,但是您特别询问了绑定挂载,这有点不同。如果要使用绑定装载,只需在mount命令中指定--bind即可。例如:
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:
这将使/ original / path中可访问的文件系统位置也可通过/ new / path访问。请注意,这不会跟随挂载点!例如,假设我有以下挂载点:
/something
/something/underneath/that
Now suppose I make a bind
mount for /something
:
现在假设我为/ 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:
我将能够通过路径/ new_something / myfile访问/ something / myfile等文件。但是我无法通过路径/ new_something / under / that / otherfile访问/ something / under / that / otherfile等文件。您必须为每个文件系统设置单独的绑定装载;或者如果你有一个相对较新的内核,你可以使用rbind挂载,它遵循挂载点:
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:
关于rbind挂载的一个警告:它们不处理在设置rbind之后安装文件系统的情况。也就是说,假设我有这样的坐骑:
/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.
然后我按上面的方式设置了我的rbind,然后我挂载/下面/下面的那个:rbind不会神奇地通过rbind位置看到新的挂载。还要注意,显然由于内核中的错误,您无法卸载rbind挂载。
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
).
另外,如果您的意思是“如何使用mount(2)系统调用设置绑定挂载?”:当您为常规绑定调用mount(2)时,必须指定MS_BIND标志(在mount.h中定义)安装。对于rbind挂载,必须指定MS_BIND和undocument MS_REC标志(在linux / fs.h中定义)。
Hope that helps,
希望有所帮助,
Eric Melski
埃里克·梅尔斯基
#2
1
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.
您所要求的是不可能的,因为绑定装载不会跨越装载点。如果您真的希望模拟文件系统循环,请尝试使用像localfs这样的伪绑定安装。我没有试过自己,它可能会在尝试读取自己提供的文件系统时锁定。刚才,我尝试使用crossmnt导出一个NFS树并将其安装在自己的下面,但由于类似的原因而失败。
#3
-1
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
其中/ path / to / device是要安装的分区的路径,或者是磁盘映像的路径,/ path / to / mount / location是要安装设备/映像的文件夹的路径下
you may also need to include the type of the file system like so (which uses fat16/fat32):
您可能还需要包含文件系统的类型(使用fat16 / fat32):
mount /path/to/device /path/to/mount/location -o loop -t vfat
#4
-1
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
接下来,使用您选择的FS类型将其设置为有效分区
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!