Mount namespaces allow to setup a different view of the filesystem private to all processes run within that namespace. You can then use mount --bind
within that namespace to map directories.
For example, on user login you can create a namespace dedicated to that user. Within that namespace, you can use mount --bind
to mount the directory /opt/data/$USER
on top of data
. You can then run the user shell in that namespace. For that shell and any other process started within that shell, any read or write in /data/
will end up reading and writing from /opt/data/$USER
instead.
To automate the setup, you can use the pam_namespace pam module. A configuration file /etc/security/namespace.conf similar to this:
/data /opt/data/$USER level root,adm
could be all you need to make this work.
Alternatively, you could use an utility like faketree
to do this interactively from the shell or in your CI/CD pipelines:
faketree --mount /opt/data/$USER:/data -- /bin/bash
(does not require root, uses namespaces)
You can read more about faketree in the main repository for the tool or in this blog post.