在 Ubuntu 14.04 上,我创建了一个可以手动启动和停止的非特权容器。
但我希望它与系统一起启动和停止。
我已将以下内容添加到容器的配置中:
lxc.start.auto = 1
lxc.start.delay = 5
但是,系统脚本似乎没有选择非特权容器。
在 linuxcontainers.org 上有一个与此相关的线程,但该解决方案似乎仅限于root
用户。
对于非 root 用户(经 root 用户同意),是否有一种干净的方法可以做到这一点?
小智 8
我建议@reboot
在Ubuntu 的 cron 中使用方便的别名来运行lxc-autostart
.
作为拥有非特权容器的用户,运行crontab -e
并添加以下行:
@reboot lxc-autostart
我想我找到了一个比目前这里介绍的更好的解决方案。部分是因为据我所知 cgmanager 已经死了,部分是因为我的解决方案感觉不像是一个黑客的解决方法,但主要是因为在寻找问题的解决方案时仍然会出现这种讨论。实际上非常简单:使用 systemd 用户模式。
当然,如果您不使用 systemd,则此解决方案无济于事。在这种情况下,我建议您弄清楚您的 init 系统是否有某种方式允许非特权用户在启动时运行服务并将其用作起点。
我假设您有无特权的 lxc 容器正常工作,并且lxc-autostart
在容器的用户工作时运行。如果是这样,请执行以下操作:
~/.config/systemd/user/lxc-autostart.service
在任何拥有 lxc 容器的用户的主目录中创建文件:[Unit]
Description="Lxc-autostart for lxc user"
[Service]
Type=oneshot
ExecStart=/usr/bin/lxc-autostart
ExecStop=/usr/bin/lxc-autostart -s
RemainAfterExit=1
[Install]
WantedBy=default.target
Run Code Online (Sandbox Code Playgroud)
systemctl --user enable lxc-autostart
Run Code Online (Sandbox Code Playgroud)
(注意,该--user
选项告诉 systemctl 您在用户模式下使用它。我通常使用 systemctl、启动、停止、statuc、启用等所做的所有事情都与 --user 一起使用。)
$user
是拥有 lxc 容器的用户名:sudo loginctl enable-linger $user
Run Code Online (Sandbox Code Playgroud)
这对于 systemd$user
在引导时启动systemd 用户实例是必要的。否则它只会在$user
登录时启动一个。
有关更多信息,我建议使用 archlinux wiki systemd/timer page 和systemd man pages。
您实际上可以以 root 身份启动/停止/任何用户的 systemd 服务,但这需要您设置XDG_RUNTIME_DIR
环境变量。假设$user
是您要访问其实例的用户并且$uid
它是 uid,那么这就是您启动上面定义的 lxc-autostart.service 的方式:
sudo -u $user XDG_RUNTIME_DIR=/run/user/$uid systemctl --user start lxc-autostart
Run Code Online (Sandbox Code Playgroud)
您甚至可以使用systemd-run
该用户以不破坏 lxc 的方式运行任意命令。我正在使用以下命令在备份之前/之后停止/启动我的容器,其中$name
正在备份的 lxc 容器的名称是:
sudo -u $user XDG_RUNTIME_DIR=/run/user/$uid systemd-run --user --wait lxc-stop -n $name
sudo -u $user XDG_RUNTIME_DIR=/run/user/$uid systemd-run --user --scope lxc-start -n $name
Run Code Online (Sandbox Code Playgroud)
(请注意,没有--wait
systemd-run 不会阻塞,直到容器停止。)
归档时间: |
|
查看次数: |
7982 次 |
最近记录: |