29

I'm trying to mount a folder on the host to an LXC container.

The host has a folder /mnt/ssd/solr_data created (this is currently on the root filesystem, but later I'll mount an SSD drive there, so I'm prepping for that).

I want that folder to mount as /data in the container.

So in the containers fstab file I have the following:

/mnt/ssd/solr_data      /var/lib/lxc/Solr4StandAlone/rootfs/data        ext4    defaults,noatime        0       0

But that's a no-go, I get this error starting the container:

lxc-start: No such file or directory - failed to mount '/mnt/ssd/solr_data' on '/usr/lib/x86_64-linux-gnu/lxc//data'
lxc-start: failed to setup the mounts for 'Solr4StandAlone'
lxc-start: failed to setup the container
lxc-start: invalid sequence number 1. expected 2
lxc-start: failed to spawn 'Solr4StandAlone'

6 Answers 6

20

To create the directory automatically in the container, you can also add the create=dir option in the fstab :

/mnt/ssd/solr_data      /var/lib/lxc/Solr4StandAlone/rootfs/data        none   bind,create=dir

Edit : this is specific to LXC. See this thread

Just like we already had "optional", this adds two new LXC-specific mount flags:

  • create=dir (will do a mkdir_p on the path)

  • create=file (will do a mkdir_p on the dirname + a fopen on the path)

This was motivated by some of the needed bind-mounts for the unprivileged containers.

3
  • 1
    What version of mount does this apply to? I couldn't find the option described in mount(8) on Ubuntu 14.04, for example. Commented Jun 2, 2014 at 17:27
  • Also not in the latest mount(8) Commented Jun 2, 2014 at 17:47
  • 3
    indeed... looks like it works with lxc only. See this thread on lxc-devel ML Commented Jun 18, 2014 at 19:33
20

In /var/lib/lxc/Solr4StandAlone/config add a line reading:

lxc.mount.entry = /mnt/ssd/solr_data  /var/lib/lxc/Solr4StandAlone/rootfs/data none bind 0 0

Then lxc-stop stop your container and lxc-start your container again.

That is all that is needed.

ref: reference link

1
  • 3
    Your solution should get better review as it works with unprivileged LXC containers tool. The other ones won't work in this case. And probably selinux/apport would need to be tweaked to allow their method. +1 for your solution!
    – Huygens
    Commented Oct 1, 2015 at 14:04
14

As of 2015/09/30 a change resulting from a security patch breaks mounting to an absolute path with lxc.mount.entry in the config file.

Instead you can use a relative path

 lxc.mount.entry = /mnt/ssd/solr_data data none bind 0 0

See: https://wiki.debian.org/LXC#Bind_mounts_inside_the_container

1
  • 1
    Great addition, it works as an answer, it'll get upvoted so people see it soon. Thanks for adding to it! Commented Nov 11, 2015 at 19:22
11

I had to create the /data folder in the local container before the mount would work properly.

I also used this fstab entry:

/mnt/ssd/solr_data      /var/lib/lxc/Solr4StandAlone/rootfs/data        none   bind     0       0
1
  • 1
    bind mount option is something that was missing in the OP. I believe it is the most important correction (present also in all the other working answers). Commented Aug 16, 2016 at 9:16
8

As LXC has changed over time this can be very simple, but it stumped me for a bit, so wanted to contribute. I also created a gist for this so i can remember myself, but simply using lxc config device will do the trick.

sudo lxc config device add Solr4StandAlone sdb disk source=/var/lib/lxc/Solr4StandAlone/rootfs/data path=mnt/ssd/solr_data

Note It is important to leave the front slash off the path argument due to a change mentioned by @biscuitNinja

Mounting directories from container to host

1
  • 1
    The ubuntu manpage has a nice example: lxc config device add [<remote>:]container1 <device-name> disk source=/share/c1 path=opt, where source is on the host machine and path lies within the container.
    – 0_0
    Commented Sep 5, 2021 at 11:54
2

If you are using libvirt to create your lxc container, you can make the directory on the host to be passthrough as shown here:

root@localhost:/# cat /etc/libvirt/lxc/my_container.xml
...
 <filesystem type='mount' accessmode='passthrough'>
    <source dir='<dir on host>'/>
    <target dir='<dir on container>'/>
 </filesystem>
....

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .