I had some success with this - if not with an lxc container, I did manage to make it work for an otherwise private mount namespace. Because lxc is built on the underlying linux namespaces that I was also using I don't see any reason why it shouldn't work for you.
In the first place I setup the namespace like:
sudo unshare -m sh -c '
mount -ttmpfs none /tmp
echo x > /tmp/mytmp
findmnt -o+PROPAGATION /tmp
echo "$$"
cd /tmp
exec "$0" -i
TARGET SOURCE FSTYPE OPTIONS PROPAGATION
/tmp tmpfs tmpfs rw private
/tmp none tmpfs rw,relatime private
29384
$
...and I got an interactive shell. The next thing I did in a separate terminal session was...
sudo sh -c ' { cd /dev/fd/0 ; mkdir mnt
ls -l; cat mytmp
} 3<$0/ns/mnt <$0/29384/cwd
' /proc/29384
drwxr-xr-x 2 root root 40 Jan 4 02:52 mnt
-rw-r--r-- 1 root root 2 Jan 4 02:38 mytmp
x
...which was very encouraging!
But I couldn't get a mount in there - every time I tried to mount
a parent ns directory over one in the child ns it failed - miserably. Some research suggests this is by design (in particular: see the caveats in man 7 user_namespaces
regarding PROPAGATION flags). What did work, though, was (in a new namespace):
sudo unshare --propagation slave -m sh -c '
mount -ttmpfs none /tmp; cd /tmp
exec "$0" -i'
And then in the parent namespace session...
sudo mount --bind / /mnt
sudo mount --bind / /tmp
sudo mount --bind /tmp /mnt/img/tmp
Now the above works in the first case but not in the second. Because the child ns does not propagate fs changes upwards the parent won't affect changes it has made to its fs view. And so because the child has its own mount on /tmp
anything the parent does is irrelevant there. However, if there is some common hierarchy and the child ns is configured to receive filesystem changes then it will see changes the parent propagates downward.
In the child ns after running the above...
ls /tmp /mnt /mnt/tmp
/mnt:
bin dev etc lib mnt proc run srv tmp var
boot esp home lib64 opt root sbin sys usr
/mnt/tmp:
serverauth.FT3Z6IFyWW
systemd-private-...systemd-timesyncd.service-YUkVU6
/tmp:
And so I guess to answer the question - yes, I believe it is possible. But, I'm also fairly sure you'd need to arrange for it to be so ahead of time.
nsenter
get you an closer to your goal? – mikeserv Jan 03 '16 at 06:30nsenter
'ed into the processes namespace you have no access to the outside directory you want to mount. – user2059857 Jan 04 '16 at 02:10