如何将主机目录安装到我的非特权LXC

问题描述

我有一个非特权 lxc容器。我想在主机上将目录~/Project挂载到lxc。我试图修改lxc配置文件。以下是我的lxc配置文件

# 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,create=dir
#lxc.mount.entry = /tmp/.X11-unix tmp/.X11-unix none bind,create=dir
lxc.mount.entry = /tmp/.X11-unix mnt/x11 none bind,create=dir
lxc.mount.entry = /dev/video0 dev/video0 none bind,create=file


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

它没有用。 还有没有办法在非特权 lxc中复制文件?我试图将文件复制到我的Lxc Roofts中。但是它没有出现在容器中。

注意:以下是我的/ etc / subuid和/ etc / subgid(在主机中)中的条目。

sahil:1000:1
sahil:100000:65536

解决方法

1。关于主机目录的装载

在配置文件中,不允许使用“〜”表示法,因为它是bash shell专用表示法,并且LXC不使用bash解释路径名。必须将其替换为完整路径名(例如/ home / sahil / Project)。

安装点应在容器的rootfs中定义为相对路径名。

如果尚未在容器的rootfs中创建安装点的路径名,则建议添加create=dir选项。

因此,mount指令应类似于:

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

2。关于文件从主机到容器的复制

配置文件显示了容器的rootfs的位置:

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

如果需要将 host 文件复制到容器中,则可以从/ var / lib / lxc / main_dev / rootfs复制任何(子)目录中的文件。

例如:

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

如果root拥有“ / var / lib / lxc / main_dev / rootfs /”,则可能需要在主机端具有超级用户特权。您将在容器端看到复制的文件。

注意::容器的rootfs位置是不寻常的,因为没有特权的LXC容器的rootfs通常位于用户的主目录中。例如:/ home / /。local / share / lxc / / rootfs