Ubuntu – Overlayfs to an existing directory

mountoverlayfs

I have looked at
How do I use OverlayFS? but it doesn't answer my question.

I need to install some third party applications and they require /opt to be writable but the device I am running on has /opt on a readable rootfs. Essentially, I have a directory /opt on a read-only filesystem (lower) and let's say I have a read-write directory /mnt/optw (writable). I would like to merge /opt & /mnt/optw and mount it to /opt.

Is this possible at all?

Best Answer

You could bind /mnt/optw to /opt like so:

sudo mount --bind /mnt/optw/ /opt/

This way /opt will be a reflection of /mnt/optw contents and permissions.


To access the contents of the two directories under /opt, mount with aufs like so:

sudo mount -t aufs -o br=/opt/:/mnt/optw/ none /opt/

This way, the two directories are merged and you'll be able to access their contents under /opt/.

Notice:

If you do not have aufs support available on your system, you can add support by installing aufs-tools like so:

sudo apt install aufs-tools

For further reading man aufs.

Related Question