19

I would like to be able to merge changes from the upper filesystem of an overlay mount to a lower filesystem.

I am interested both online (i.e. merge while the overlay is mounted) and offline (unmount the overlay and then merge) solutions.

I have found a couple of offline solutions, which I have added as answers.

Does anyone know of any online solutions? It would be good to have a "commit" type command you could run to merge down the layers while the overlay is still mounted.

Something like this has been asked in the following questions with no answer:

Comments in these posts suggest variously mergerfs and bcache, both of which solve specific use cases but not the generic filesytem-agnostic use case that overlays provide.

My goal is to have a safe filesystem sandbox with snapshots that can be used with any Linux application over any (where practical) underlying filesystem, allowing you to roll back changes or manually commit them when you are ready.

I have a suspicion that modern mainline Linux has all the necessary features to do this built-in, thanks to all the sandboxing/virtualization innovations of the last few years, if only I knew how to use them.

5
  • 1
    overlayfs snapshots are coming to a kernel near you Oct 8, 2017 at 17:03
  • I am not that familiar with the intricacies of the kernel and filesystems, but out of curiosity, what would be the problem with something along the lines of rsync -a --delete merged/ lower/ to replicate what is seen inside the overlayfs mountpoint to the lower directory ? Obviously it would be an offline solution, as it's not atomic. But it seems easier than rdiffdir, so I might be missing something.
    – etnbrd
    Jul 19, 2018 at 18:07
  • @AhmedMasud, as I understand, snapshots could freeze the state of the overlayfs. So I guess you mentioned it to resolve the atomic part of an online solution. But I fail to see past this, and how it could be possible to replicate the snapshot onto the lower dir. Did you have an idea about this ? Or maybe I am completely missing your point (I am not that familiar with these kernel features :p )
    – etnbrd
    Jul 19, 2018 at 18:14
  • 1
    Did you ever find something more? I'm on a Raspberry Pi which gets detonated on improper shutdown. Want to use overlayfs to protect it and sync down to lower judiciously
    – Malachi
    Dec 22, 2019 at 9:26
  • @ejm Have you found a satisfactory solution? Jul 3, 2021 at 20:51

4 Answers 4

5

An attempt at an online solution, but its not quite there.

The setup (in e.g. /tmp directory, as root):

LOWER=$HOME
mkdir u1 w1 o1 O
mount -t overlay overlay -o lowerdir=$LOWER,upperdir=u1,workdir=w1 o1
mount --bind o1 O

Then you can work in O directory, which is an overlay over $LOWER. When you want to do the snapshot:

mkdir u2 w2 o2
mount -t overlay overlay -o lowerdir=o1,upperdir=u2,workdir=w2 o2

(Note that nested overlays like this won't work on older kernels).

But then I want some way to atomically change the bind mount at O to point to o2 instead of o1. I don't know how to do this other than:

umount O
mount --bind o2 O

(Not atomic; there is a window where O is unmounted).

Ideally, running processes could continue to run without knowing that the underlying filesystem of O had changed from o1 to o2. I don't know if this is possible, or whether changing the underlying filesystem of O like this will disrupt open applications too much. I need to investigate further.

Then, once O has been redirected to o2, we can remount o1 read-only as a precaution, then perform an offline merge using for example rdiffdir or overlayfs-tools.

Finally, we would want some way to atomically remount o2 as lowerdir=$HOME,upperdir=u2,workdir=w2 so that o1, u1 and w1 (all now empty dirs) could be removed. Again, I don't know if this is possible.

Otherwise, we can achieve snapshots by just nesting overlays deeper and deeper and leaving the overlay and upper dirs for each mounted without attempting to merge or cleanup. But there is probably a limit to the number of nested overlays that can be mounted. And at some point, we still need to merge the layers downwards if we want to persist changes.

4

I have found the overlayfs-tools project which implements a useful set of tools (diff, merge and vacuum). It requires the overlay to be unmounted so is an offline-only solution.

It is a good proof-of-concept but I wouldn't rely on it in its current state as it has only a single maintainer and no activity for several years.

3

Another offline solution I've come up with is using rdiffdir to create a patch with the overlay mounted, then unmount and apply it. This solution requires the intermediate step of storing the patch somewhere in the meantime (either on disk or in a ramdisk/tmpfs).

1

I use it in https://github.com/StuartIanNaylor/zram-config has quite a few installs zram-config merges down on shutdown and seems to work for quite a number of users.

I wish the OverlayFS team would produce some official tools. Aufs has tools but isn't included in many kernels and it seems on oversight for overlayfs not to have snapshot to lower merge down tools as it will greatly enhance use scenario.

You must log in to answer this question.

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