Mount container directory in the host

I have installed lxd in an ubuntu 20.04
The storage was created with lxc storage create <name> btrfs.

With the lxd non-snap version I was doing something like this:

sudo mount -o rbind /var/snap/lxd/common/lxd/containers/my-container/rootfs/home/user /path/in/host to mount directory from the container into the host.

This is not working anymore with the lxd snap version because /var/snap/lxd/common/lxd/containers/my-container/rootfs is inside the namespace /run/snapd/ns/lxd.mnt

I can use nsenter --home=/run/snapd/ns/lxd.mnt mount -o rbind /var/snap/lxd/common/lxd/containers/my-container/rootfs/home/user /path/in/host but it is not available outside the namespace.

What can I do?

Hmm, it’s not really something we’d ever recommend doing, in general doing it the other way around works much better and is actually suppported by LXD.

In any case, did you try with /var/snap/lxd/common/mntns/var/snap/lxd/common/lxd/storage-pools/... this should let you see into the mount namespace.

I’m not sure that it will let you bind-mount from that though, Linux has a bunch of security checks in place to prevent cross-mntns bind-mounts in general.

You are right, Linux does not let me bind-mount in this location :frowning_face:

I am mounting the storage in another path and mounting from there.

mount /var/snap/lxd/common/lxd/disks/lxd.img /path/to/lxd/storage
mount  -o rbind /path/to/lxd/storage/containers/my-container/rootfs/home/user /path/in/host

Maybe it is not a good idea :sweat_smile:

Ah, no, that’s actually fine, I forgot that as an option :slight_smile:

The kernel will detect that the loop is already mapped and will re-use the existing loop device, then it will detect the superblock is already mounted and re-use the superblock, that gets you a second mount of the storage pool without any of the nastiness that could happen should those two steps not happen magically :slight_smile:

1 Like