2

From man mount_unionfs:

If a file exists in the upper layer then there is no way to access a file
with the same name in the lower layer.  If  necessary, a combination of
loopback and union mounts can be made which will still allow the lower
files to be accessed by a different pathname.

How do I do this? I can't find a reference for file system loopback other than mounting .ISO and other block images which is clearly not the right meaning, and nullfs which just mentions the term in passing (but no concrete information).

My guess as to what's involved:

I think it might involve first mounting the "lower" layer (possibly twice, to 2 different mountpoints?), then mounting a unionfs over that mountpoint (or over one of the two mountpoints) to a second mountpoint. The idea being that the same file system is exposed unmodified through the first mountpoint and also exposed as a lower layer of a union via the second mountpoint. This is based off a hint at this web-page.

Update, clarifying the aim in response to @arrowd:

The man page says one can access the lower files (implied: the entire set of lower files) via loopback, and that this can makes the lower layer accessible in parallel both (A) as a lower layer of the unionfs, and (B) via a different "loopback" mount-point/pathname as a file system in its own right. I understand the concept of a loopback layer so this makes sense, and the man page says it's doable, but I can't find the commands that will let me do it.

Being more specific, I want to overlay a file system in the usual way as a unionfs, but I also want a separate mountpoint to expose the lower layer by itself, to allow some tasks to read/write/search the entire "lower" file system directly as well, without being obscured/masked/overlaid by the union "overlay fs". So it isn't just about a single file - I need the entire "lower" layer also accessible via loopback and a separate mountpoint, not just accessible through the unionfs layer or on a file-by-file basis. It probably means I need to mount it twice in some way or another, first via loopback and then via unionfs, at the point where I initially set up/mount the unionfs layer, but I don't know how to do it.

The aim is that some cron tasks (and occasionally I as well) will need to update the files within the lower layer (without unmounting/disturbing the unionfs overlay), and not not just copy and update them within the "upper" layer as would happen with simple use of unionfs. So I need to have two mountpoints in use - one exposing the lower layer overlaid with a unionfs, and the other exposing the lower layer only without the unionfs overlay, and be able to use both in parallel in different tasks.

Also see comment I added above - I think it might involve mounting the lower file system twice (once directly, once as union), or mounting it once and remounting from that mountpoint a second time (as union). But I'm not sure and also unclear how safe it is.

Hopefully this is more clear as to the aim of the question.

1
  • 1
    Have you figured it out? I am at the exact same problem. I cannot mount the same file system twice. FreeBSD (12.0) won't let me do it. I got my question here: stackoverflow.com/questions/59762819/… The link you shared above was to some Linux thing, mount --bind and something about ext3fs. Not applicable for FreeBSD. Jan 16, 2020 at 16:16

2 Answers 2

2

I finally found the solution! I tried many tricks, mdconfig -f over the block device under a different name, none of it working. But then I found it!

What you need to do is create a nullfs mount of the directory which will become your union mount point before you mount the union. In fact, this is completely independent of the union question.

Example:

# echo "Hello World!" >/mnt/hello.txt
# ls /mnt/hello.txt
hello.txt
# cat /mnt/hello.txt
Hello World!
# mount -t nullfs /mnt /mnt2
# ls /mnt2/hello.txt
hello.txt
# cat /mnt/hello.txt
Hello World!
# mount /dev/ad0s1a /mnt
# ls /mnt/hello.txt
ls: hell: No such file or directory
# ls /mnt2/hello.txt
hello.txt
# cat /mnt2/hello.txt
Hello World!
# echo "Hello Universe!" >/mnt2/hello.txt
# umount /mnt
# ls /mnt/hello.txt
hello.txt
# cat /mnt/hello.txt
Hello Universe!

See how you can still access everything that was shadowed by the mount. Now if you combine that with the union you can do all sorts of stuff, like a 3 layered union, where you mount the nullfs of the /mnt2 over the /mnt as a 3rd layer, now this means you can write the files of the original /mnt. However, it also means that whatever new filed you write into the directory will now put on the original file system that has the /mnt mountpoint, not on the device you mounted over it.

Problem solved! I am so happy!

1

I think this paragraph talks about nullfs.

If you have a file (let it be foo) that gets overlayed by unionfs mount, you can mount_nullfs it first to a some other file (say, bar) and then do mount_unionfs. You will be able then to access foo file via bar filename.

2
  • When I read it, it only seems to talk about specific individual block devices or individual files. The page says one can access the entire "hidden" set of lower files via a different mount-point/pathname, but no information how to do it. I've updated the question to be more specific on the usage desired. Can you comment further?
    – Stilez
    Mar 17, 2018 at 12:34
  • Hum. Well, you can mount directories using nullfs_mount too, they just don't blend with existing files from mountpoint. So you just mount_nullfs foodir /mnt/foo and then mount_unionfs otherdir foodir. Now you have blended files in foodir and only foo files in /mnt/foo. This is how I understand it.
    – arrowd
    Mar 17, 2018 at 13:58

You must log in to answer this question.

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