28

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'
David Parks
  • 1,100
  • 7
  • 21
  • 42

6 Answers6

21

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.

little-dude
  • 518
  • 4
  • 10
  • 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. – 0xC0000022L Jun 02 '14 at 17:27
  • Also not in the [latest `mount(8)`](http://man7.org/linux/man-pages/man8/mount.8.html) – 0xC0000022L Jun 02 '14 at 17:47
  • 2
    indeed... looks like it works with lxc only. See [this thread on lxc-devel ML](https://lists.linuxcontainers.org/pipermail/lxc-devel/2013-December/006444.html) – little-dude Jun 18 '14 at 19:33
18

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

Luc
  • 3,418
  • 3
  • 26
  • 37
Mausy5043
  • 346
  • 3
  • 11
  • 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 Oct 01 '15 at 14:04
12

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

Pierre.Vriens
  • 1,088
  • 21
  • 13
  • 16
biscuitNinja
  • 121
  • 1
  • 2
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
David Parks
  • 1,100
  • 7
  • 21
  • 42
7

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

ekydfejj
  • 171
  • 1
  • 3
  • The ubuntu manpage has a nice example: `lxc config device add [:]container1 disk source=/share/c1 path=opt`, where `source` is on the host machine and `path` lies within the container. – 0_0 Sep 05 '21 at 11:54
1

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>
....
Kevdog777
  • 3,194
  • 18
  • 43
  • 64