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.
This was a surprising finding, and one I'd like to see a solution for. I've tried some variations with bind mounts, but not getting the results I hoped for.
But I saw another question which related to nested overlays, that apparently should work, but there was a regression in Linux kernel 4.2 that prevented it from working in that version. Luckily I am on 4.15 so I tried this method;
mount -o ro,loop base.squash /media/foo/myimage
mount -o ro,loop home.squash /media/foo/myhome
mount -t overlay overlay -o lowerdir=/media/foo/myimage,upperdir=/var/lib/myimage,workdir=/var/chache/myimage /media/bar
mount -t overlay overlay -o lowerdir=/media/foo/myhome,upperdir=/var/lib/myhome,workdir=/var/chache/myhome /media/bar/home
That seemed to have the desired effect.
I tried not specifying the "upperdir" and "workdir" parts as it was stated somewhere that it would work but create a read-only overlay. But it didn't work. Not for me at least.
Best Answer
You're missing
workdir=
: