Sometimes I'd like to have a clean home directory that destroys itself automatically. Something like private mode for processes: they can't access the content of my home and stuff that the process saves in my home will get removed automatically.
For this I use a mount namespace and mount a tmpfs over my home:
sudo unshare --mount ${SHELL} -c "
mount -t tmpfs tmpfs '$HOME' &&
chown $(id -u):$(id -g) '$HOME' &&
chmod 0700 '$HOME' &&
cd $(pwd) &&
exec su $(whoami)"
Now I can run anything in that shell and if I close it I'm back to my normal home.
In this shell I don't have any ssh keys or bash profiles or other settings I usually have in my home. So it could be useful to have something similar but with all the stuff of my home still there. Like a simple snapshot that will get rolled back automatically:
sudo unshare --mount ${SHELL} -c "
set -e
dest=\$(mktemp -d /run/home-overlay.XXXXX)
mkdir -p \$dest/{ro,rw,wo}
mount --bind --ro '$HOME' \$dest/ro
mount -t overlay -o lowerdir=\$dest/ro,upperdir=\$dest/rw,workdir=\$dest/wo overlay '$HOME'
cd $(pwd)
exec su $(whoami)"
Now imagine if I want to use a browser for a while, but I don't want it to pollute my homedir. I could just use the first invocation and be happy. But after a while I want to login to a website and stay logged in or change some settings and have them persistent. So instead of mounting a tmpfs over my home I could bind mount a subdir of my home and let the browser pollute over there and delete it when I no longer need it. I'm not sure if this would also work with overlay since this is usually meant for a lowerdir that doesn't change.