LXC 环境变量

LXC environment variables

我是 LXC 容器的新手,正在使用 LXC v2.0。我想将设置传递给容器内的进程 运行(特别是 Systemd 服务文件的命令行参数。

我正在考虑通过配置文件 lxc.environment = ABC=DEF 将环境变量传递给容器。 (我打算使用 SALT Stack 来操作这些变量)。我是否必须手动解析 /proc/1/environ 才能访问这些变量,还是我缺少更好的方法?

documentation 说:

If you want to pass environment variables into the container (that is, environment variables which will be available to init and all of its descendents), you can use lxc.environment parameters to do so.

我假设,由于所有进程(包括 shell)都是 init 进程的后代,因此环境应该在每个 shell 中可用。不幸的是,这似乎不是真的。在 discussion on linuxcontainers.org 中,有人说:

That’s not how this works unfortunately. Those environment variables are passed to anything you lxc exec and is passed to the container’s init system.

Unfortunately init systems usually don’t care much for those environment variables and never propagate them to their children, meaning that they’re effectively just present in lxc exec sessions or to scripts which directly look at PID 1’s environment.

所以是的,显然解析 /proc/1/environ 似乎是这里唯一的可能性。