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'

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.

|improve this answer|||||

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

|improve this answer|||||
  • 2
    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 Oct 1 '15 at 14:04

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
|improve this answer|||||
  • 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). – imz -- Ivan Zakharyaschev Aug 16 '16 at 9:16

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

|improve this answer|||||
  • Great addition, it works as an answer, it'll get upvoted so people see it soon. Thanks for adding to it! – David Parks Nov 11 '15 at 19:22

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

|improve this answer|||||

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>'/>
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.