Simple mount from snap based LXD container to host filesystem


(Bob Brown) #1

Hi all,

There is much information already about how to mount a directory from the LXD host into a container, thus allowing access to external resources. I cannot though figure out how to do this from a snap based LXD.

root@dev5:~# lxc version
Client version: 3.7
Server version: 3.7

All documentation I have found (such as https://gist.github.com/julianlam/07abef272136ea14a627) talk about adding an entry to the container config file, however I cannot find where these are located.

I did find one here: /var/snap/lxd/common/mntns/var/snap/lxd/common/lxd/logs/containername/lxc.conf but it did not seem to have any effect.

Cheers,

  • Bob -

#2

Hi!

Here we go,

$ lxc --version
3.7
$ mkdir ~/LXDSHARED
$ lxc config device add mycontainer mydirectory disk source=/home/myusername/LXDSHARED path=/LXDSHARED
Device mydirectory added to mycontainer
$ lxc ubuntu mycontainer
ubuntu@mycontainer:~$ cd /LXDSHARED/
ubuntu@mycontainer:/LXDSHARED$ touch myfiles.txt
ubuntu@mycontainer:/LXDSHARED$ logout
myusername@mycomputer:~$ ls -l LXDSHARED/
total 0
-rw-rw-r-- 1 myusername myusername 0 Nov  14 10:24 myfiles.txt
myusername@mycomputer:~$ 

The gist (no pun intended) is that with LXD, unlike LXC, the bind-mount is done for you by LXD.

Check at https://github.com/lxc/lxd/blob/master/doc/containers.md#type-disk for more options on the disk LXD device.


(Bob Brown) #3

Awesome, thank you.


(Andrew Wilson) #4

Hi: I was trying to mount a directory into my container using lxc version 3.0.3 and this was very helpful for me. But just in case anyone else comes here looking, there’s another step that I had to do to get this to work, as I was otherwise getting ‘touch: cannot touch ‘myfiles.txt’: Permission denied’ error from within the container, and a quick ls -la showed me why:

drwxr-xr-x 2 nobody nogroup 4096 Jun 3 02:35 .
drwx------ 5 root root 8 Jun 3 02:36 …

  • the container user lacked the permission to create a file (you can read, but not write)!

So, since I wanted to be able to write, I fixed this with permission changes as follows (you might want to use less liberal permissions, but you get the idea I trust). On the host:-

mkdir ~/LXDSHARED
chmod -R 0777/LXDSHARED

Then you can execute the ‘lxc config …’ as shown and gain read AND write-access to the directory.

V/R

Andrew