1

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?

2
  • Do you need any other content of /opt/, /opt/dir1/ or /opt/dir1/dir2/ in that namespace? Commented May 20 at 18:30
  • I do need to preserve the contents of /opt, but I can assume that dir1 (and obviously everything else underneath that) do not already exist in /opt. Commented May 20 at 20:23

1 Answer 1

1

Content of parent directories NOT required

unshare -Urm
mount -t tmpfs tmpfs /opt
mkdir -p /opt/dir1/dir2/
touch /opt/dir1/dir2/file
mount --bind /home/user/path/to/file /opt/dir1/dir2/file

Content of parent directories required

In this case something like overlayfs would have to be used. Due to the permission problem that requires a user namespace - which is used here anyway.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.