6

I am attempting to create a virtual mount using bind mounts in combination with OverlayFS. What would be most optimal is as follows:

diagram

The root folder is bind-mounted to a new sub-folder, and used as the lower directory. A new upper directory is created to save the differences of the overlay, a work_dir directory for the workdir component and finally a mnt directory to mount the overlay to.

The issue I am having, is that once this is set-up, the contents of the bind mount do not appear in the overlay.

I have researched different properties of the mounts, including the use of recursive mounts and shared/non-shared mounts. Alternatively, I tried bind mounting the individual folders on the root to respective folders under the lowerdir; still no files. Am I missing a setting in either of the mounts, or is this simply not possible?

1

1 Answer 1

1

It is possible to create bind mounts within the mountpoint of an overlayfs. I was only able to accomplish this with C code since the mount(8) command does not provide this option.

<fd> = openat(AT_FDCWD, <merge-dir>, O_RDONLY|O_CLOEXEC|O_PATH)
mount(<source-dir>, "/proc/self/fd/<fd>", "", MS_BIND|MS_REC, NULL)
close(<fd>)

<merge-dir> is the directory to be mounted into relative to the host system, mnt/lower in your case

<source-dir> is the directory to be mounted from, <root directory> in your case. It is also relative to the host system.

<fd> is the file descriptor int returned by openat, it needs to be concated with the /proc/self/fd/ string.

This process can be done immediately after the overlayfs is mounted or anytime after.

I found this by looking at system calls from 'docker run' when executed with the option --mount type=bind when seems to do exactly what you describe in your diagram. Since overlay mounts do not implicitly include child mounts (even with MS_REC), openat(2) can be used to open a file descriptor corresponding to the directory to be mounted into. The file descriptor device file in /proc/self/fd/ can then be used as the target of a bind mount which will stay open after the descriptor is closed.

2
  • This does not work. I can bind into the lower directory but it still won't appear in the overlay destination, even after remount. One can mount into the destination directory, but then the benefits of the overlay (modification without modification) get lost. Nov 6, 2022 at 23:32
  • 1
    @Victor - Reinstate Monica, the intention of my answer was for the case of mounting a directory which will receive changes written directly to it. If you do not want this behavior then you are probably not looking for a bind mount and are instead interested in the multiple lowerdirs feature which can do something similar to what you want kernel.org - multiple lower layers
    – 1lumin
    Nov 13, 2022 at 2:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .