Is it possible to union-mount an existing disk image containing a root filesystem to the upperdir of an overlayfs mount for read-write access?

I'm trying to union-mount some old disk images to a /tmp/ directory on an Ubuntu 16.04 machine using pre-existing scripts (originally written on Ubuntu 12.04 and ported to 14.04).

The old automation scripts used overlayfs to create and union-mount writable ext2/3 disk images on top of squashfs images — something like this:

cd /tmp
mkdir lower upper union
losetup /dev/loop1 /tmp/image.sfs
losetup /dev/loop2 /tmp/image.ext2
mount -t squashfs -o ro /dev/loop1 /tmp/lower
mount -t ext2 -o rw /dev/loop2 /tmp/upper
mount -t overlayfs -o rw,lowerdir=/tmp/lower,upperdir=/tmp/upper overlayfs /tmp/union

They were last used on an 3.x kernel (likely pre-3.18), and that method doesn't seem to work anymore. Overlay now requires a workdir option—e.g.:

mount -t overlay -o rw,lowerdir=/tmp/lower,upperdir=/tmp/upper,workdir=/tmp/work overlay /tmp/union

Q: Is it possible to tweak the automation scripts to mount and use the existing images in a writable state on a 4.x kernel? Can I avoid reformulating them to contain something like root and work directories? In some cases that may break their use elsewhere.

Per the documentation, the workdir has to be an empty directory on the same filesystem as the upperdir to allow for atomic writes. That sounds to me like it is impossible to union-mount root filesystem images as read-write. The workdir would have to exist within the images (separate from their data directory) which isn't possible in a root filesystem image.

I've considered loading up stock Ubuntu 14.04 in a VM for a couple one-offs, but it's not a long term solution.

Best Attempt So Far:

The only solution I've come up with that avoids double-writing the same data to disk is to mount both images as lowerdir values, with upperdir and workdir directories sitting on a tmpfs mount. Then I can use rsync to copy the changes from the tmpfs location to the mounted ext2/3 image after the automated writes are complete and the overlay union has been unmounted—i.e.:

mount -t overlay -o rw,lowerdir=/tmp/upper:/tmp/lower,upperdir=/tmp/tmpfs/root,workdir=/tmp/tmpfs/work overlay /tmp/union

...perform automated reads/writes...

umount /tmp/union

...rsync contents from /tmp/tmpfs/root to /tmp/upper...

It's strictly limited to available RAM, though, and it's an ugly hack to add to all of the automation scripts.

  • 1
    @Gilles thanks for the retitle to help with exposure, but I do not want to use overlayfs for my root filesystem. I'm trying to union-mount an overlay disk image (that contains root filesystem changes and whiteouts) to the upperdir position above a read-only lowerdir root filesystem image as a temporary mount under /tmp/. Effectively, the old automation scripts are remotely modifying offline user system images that are based on a shared disk image (conceptually similar to how Docker works). The new title may confuse people. – codewithmichael Sep 11 '16 at 1:56

Create an additional level of nesting in the image file:

mkdir   /tmp/upper/upper
mkdir   /tmp/upper/work
mount -t overlay -o rw,lowerdir=/tmp/lower,upperdir=/tmp/upper/upper,workdir=/tmp/upper/work overlay /tmp/union

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.