0

I have an unprivileged lxc container. And I want to mount a directory ~/Project in the host machine to the lxc. I tried to modifying the lxc config file. The following is my lxc config file:

# Template used to create this container: /usr/share/lxc/templates/lxc-download
# Parameters passed to the template: --dist archlinux --release current --arch amd64
# Template script checksum (SHA-1): 1ba3a6d6544626d6e64c7b8f1a51f6022c5e5f8f
# For additional config options, please look at lxc.container.conf(5)

# Uncomment the following line to support nesting containers:
#lxc.include = /usr/share/lxc/config/nesting.conf
# (Be aware this has security implications)


# Distribution configuration
lxc.include = /usr/share/lxc/config/common.conf
lxc.include = /usr/share/lxc/config/userns.conf
lxc.arch = x86_64

# Container specific configuration
#lxc.idmap = u 0 100000 65536
#lxc.idmap = g 0 100000 65536
lxc.idmap = u 0 100000 1000
lxc.idmap = g 0 100000 1000
lxc.idmap = u 1000 1000 1
lxc.idmap = g 1000 1000 1
lxc.idmap = u 1001 101001 64535
lxc.idmap = g 1001 101001 64535

lxc.rootfs.path = dir:/var/lib/lxc/main_dev/rootfs
lxc.uts.name = main_dev

# Network configuration
lxc.net.0.type = veth
lxc.net.0.link = lxcbr0
lxc.net.0.flags = up
lxc.net.0.hwaddr = 00:16:3e:47:f6:98

# Video configuration
lxc.mount.entry = /dev/dri dev/dri none bind,optional,create=dir
lxc.mount.entry = /dev/snd dev/snd none bind,optional,create=dir
#lxc.mount.entry = /tmp/.X11-unix tmp/.X11-unix none bind,optional,create=dir
lxc.mount.entry = /tmp/.X11-unix mnt/x11 none bind,optional,create=dir
lxc.mount.entry = /dev/video0 dev/video0 none bind,optional,create=file


# User
lxc.mount.entry = ~/Project /home/arch/test none bind 0 0

It didn't work. Also is there a way to copy files in an unprivileged lxc? I tried to copy files into my lxc roofts. But it didn't show-up in the container.

Note: the following are the entries in my /etc/subuid and /etc/subgid (in host machine) where.

sahil:1000:1
sahil:100000:65536
Rachid K.
  • 4,490
  • 3
  • 11
  • 30
Md Sahil
  • 3
  • 4
  • When you say it doesn't work, does the container get created ? Does it starts but you don't have the mount in /home/arch/test ? – Rachid K. Oct 29 '20 at 08:11
  • @RachidK. yes, the container is created and runs as usual. The only issue is that I am unable to mount the host directory. – Md Sahil Oct 30 '20 at 11:52

1 Answers1

0

1. Concerning the mount of the host directory

In the configuration file, the "~" notation is not allowed as it is a bash shell specific notation and LXC does not use bash to interpret the pathname. This must be replaced by the full pathname (e.g. /home/sahil/Project).

The mount point should be defined as a relative pathname in the container's rootfs.

If the pathname of the mount point is not yet created in the container's rootfs, it is advised to add the create=dir option.

Hence the mount directive should be something like:

lxc.mount.entry = /home/sahil/Project home/arch/test none bind,create=dir 0 0

2. Concerning the copy of a files from host to container

The configuration file shows the location of the container's rootfs:

lxc.rootfs.path = dir:/var/lib/lxc/main_dev/rootfs

If you need to copy host files into the container, you can copy the files in any (sub)directory from /var/lib/lxc/main_dev/rootfs.

For example:

$ cp /home/sahil/example.txt /var/lib/lxc/main_dev/rootfs/some_place

You may need super user privileges on host side if the "/var/lib/lxc/main_dev/rootfs/" is owned by root. And you will see the copied file on container side.

NB: The location of your container's rootfs is unusual as unprivileged LXC containers have typically their rootfs located in the user's home directory. For example : /home/<login_name>/.local/share/lxc/<container_name>/rootfs

Rachid K.
  • 4,490
  • 3
  • 11
  • 30
  • I tried 1, It didn't work. Is it because of the fact that my container is unpriviledged? Also, in 2, which config file contains that entry? My rootfs is at `~/.local/share/lxc/main_dev/rootfs`. I have already tried copying stuff there from the host machine. But the files don't showup in the container. I am guessing this issue is caused due to lack of permissions maybe. – Md Sahil Oct 31 '20 at 14:32
  • This is the location of your rootfs in the config file that you shared in the question : lxc.rootfs.path = dir:/var/lib/lxc/main_dev/rootfs. So, that is why you can't mount anything into it as you are not the owner of this directory on host side. – Rachid K. Oct 31 '20 at 15:08