I would like to use unshare to create a new unprivileged user/mount namespace, with the goal of making a specific file appear at a specific location inside the child namespace.
For example, assume that I would like /home/user/path/to/file to appear as /opt/dir1/dir2/file. However, /opt already exists in the parent namespace and is not writable by the user that I'm starting with. This does not work:
user $ unshare -Urm
root # mount --bind /home/user/path/to/file /opt/dir1/dir2/file
mount: /opt/dir1/dir2/file: mount point does not exist.
       dmesg(1) may have more information after failed mount system call.
I think the underlying issue here is that /opt/dir1/dir2 does not exist ahead of the attempt to make the bind mount. However, I'm not able to create that directory since /opt is not writable in the parent:
root # mkdir -p /opt/dir1/dir2
mkdir: cannot create directory ‘/opt/dir1’: Permission denied
Is there a way to make this work so that the changes to /opt are only visible inside my child namespace, allowing me to work around the permission issue?
/opt/,/opt/dir1/or/opt/dir1/dir2/in that namespace?/opt, but I can assume thatdir1(and obviously everything else underneath that) do not already exist in/opt.