User:Tsu2/LXC mount shared directory

Jump to: navigation, search

LXC - Mount Shared Directory

More precisely, this page describes how to mount any Host directory within a LXC container.
BEWARE: All guides warn about creating a circular mount, eg mounting a Host mount within a Container mount instead of using a Bind Mount.

Supported OS and LXC

This page describes a method on openSUSE 12.3 Host with an openSUSE 12.3 Container, but should work on <any> combination of Linux running a current version of LXC. Note that there are many ways to implement Linux Containers and there can be significant differences. As an example Docker was built on top of LXC up to Feb 2014. From March 2014 onwards, significant changes were made and the methods described here are not guaranteed to work on Docker.

Overall Description

Although there are a number of ways to mount a Host directory within a Container, this method is probably the best in that it doesn't require configuration options within the Container, the configuration is made in the Container Configuration (on the Host) and is passed into the container.

Host System Shared Directory (Source) - /home/username/virtual_share
Container (Target) - /root/virtual_share

Although both the Host and Container directories can be different than the example directories created in this guide, there are few restrictions except for the warning above to avoid a circular mount.

Creating the Host directory

Skip this if already prepared.
The following creates a directory and sets the permissions to "Everyone Read/Write" assuming you'd like to RW from any virtual guest as well as the Host. Modify "username" or the entire path according to your own situation.

mkdir /home/username/virtual_share && chmod 7777 /home/username/virtual_share

Create the target directory

Start the Container. If you created the Container using the YAST LXC Create applet, you will already have a /root directory separate from a /home directory. You can mount anywhere including /mnt, just modify the following accordingly.

mkdir /root/virtual_share

Shutdown and Stop the Container.

Edit the Container Config file

The Container config file can be located in a number of places. A common location is /etc/lxc/. But, if you created your Container using the YAST LXC Create applet, the file will be at

/var/lib/lxc/containername/config

Open this config file in a text editor with root permissions (required for editing)

Find any existing "lxc.mount.entry" (not simply "lxc.mount"), you will create a new line near it. If you cannot find any existing "lxc.mount.entry" in the config, you must add the following line <before> any "lxc.mount" entries, I recommend immediately before the Container name (lxc.utsname)

Note the format, lxc command | Host source directory | Container target directory | mount options

lxc.mount.entry = /home/username/virtual_share root/virtfs none bind.ro 0.0

Save your config file

Check the Results

Start your Container and browse to your shared directory. Add some files both from your Host and Container, then view from the other to verify all is working properly.

Reference

The following describes several references but may be incomplete (eg my contribution to place the lxc.mount.entry in the correct place) http://docs.oracle.com/cd/E37670_01/E37355/html/ol_app_containers.html