如何在 debian 主机内的 LXC 容器中运行 openvpn?

如何在 debian 主机内的 LXC 容器中运行 openvpn?

我在我的主机上运行着 debian stretch,并尝试在 Ubuntu xenial LXC 客户机内配置 openvpn。

由于 openvpn 需要 tun 设备,因此我遵循了以下指南 http://heider.io/blog/2013/10/26/openvpn-in-a-lxc-container/ 允许在容器内创建 tun 设备。

不幸的是,lxc.cgroup.devices.allow = c 10:200 rwm在容器的配置文件中设置会出现这个错误:

  lxc-start ERROR    lxc_cgfsng - cgroups/cgfsng.c:cgfsng_setup_limits:1949 - No such file or directory - Error setting devices.allow to c 10:200 rwm for ubuntu
  lxc-start ERROR    lxc_start - start.c:lxc_spawn:1236 - Failed to setup the devices cgroup for container "ubuntu".
  lxc-start ERROR    lxc_start - start.c:__lxc_start:1346 - Failed to spawn container "ubuntu".

编辑

我正在尝试在非特权 LXC 容器中实现这一点,以下是该容器的完整配置:

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

# Container specific configuration
lxc.id_map = u 0 100000 65536
lxc.id_map = g 0 100000 65536
lxc.rootfs = /home/myuser/.local/share/lxc/ubuntu/rootfs
lxc.rootfs.backend = dir
lxc.utsname = ubuntu

# Network configuration
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
lxc.network.hwaddr = 00:11:22:aa:bb:cc
lxc.network.ipv4 = 192.168.1.101/24
lxc.network.ipv4.gateway = 192.168.1.1

# trying to get /dev/net/tun inside container
lxc.cgroup.devices.allow = c 10:200 rwm

当我没有设置 devices.allow 选项以便能够启动容器时,我可以看到容器内部的进程在 cgroup 中 /sys/fs/cgroup/devices/user.slice(从容器外部查看它们的 PID 实际上是在cgroup.procs这个子目录的文件中),从这里:

$ cat devices.list 
a *:* rwm

但从容器内部

# mknod /dev/net/tun c 10 200
mknod: /dev/net/tun: Operation not permitted

答案1

添加

lxc.mount.entry = /dev/net/tun dev/net/tun none bind,create=file

在容器的配置文件中绑定挂载容器内的 tun char 设备,即可解决问题。

相关内容