TranslateProject/translated/tech/20170219 How to auto start LXD containers at boot time in Linux.md

74 lines
2.6 KiB
Markdown
Raw Normal View History

2017-12-25 08:56:10 +08:00
如何在 Linux 启动时自动启动 LXD 容器
======
2017-12-25 08:56:10 +08:00
我正在使用基于 LXD“Linux 容器”)的虚拟机。如何在 Linux 系统中启动时自动启动 LXD 容器?
你可以在 LXD 启动后启动容器。你需要将 boot.autostart 设置为 true。你可以使用 boot.autostart.priority默认值为 0选项来定义启动容器的顺序从最高开始。你也可以使用 boot.autostart.delay默认值0选项定义在启动一个容器后等待几秒后启动另一个容器。
2017-12-25 08:56:10 +08:00
### 语法
2017-12-25 08:56:10 +08:00
上面讨论的关键字可以使用 lxc 工具用下面的语法来设置:
```
$ lxc config set {vm-name} {key} {value}
$ lxc config set {vm-name} boot.autostart {true|false}
$ lxc config set {vm-name} boot.autostart.priority integer
$ lxc config set {vm-name} boot.autostart.delay integer
```
2017-12-25 08:56:10 +08:00
### 如何在 Ubuntu Linux 16.10 中让 LXD 容器在启动时启动?
2017-12-25 08:56:10 +08:00
输入以下命令:
`$ lxc config set {vm-name} boot.autostart true`
2017-12-25 08:56:10 +08:00
设置一个 LXD 容器名称 “nginx-vm” 以在启动时启动
`$ lxc config set nginx-vm boot.autostart true`
2017-12-25 08:56:10 +08:00
你可以使用以下语法验证设置:
```
$ lxc config get {vm-name} boot.autostart
$ lxc config get nginx-vm boot.autostart
```
2017-12-25 08:56:10 +08:00
示例输出:
```
true
```
2017-12-25 08:56:10 +08:00
你可以使用下面的语法在启动容器后等待 10 秒钟后启动另一个容器:
`$ lxc config set nginx-vm boot.autostart.delay 10`
2017-12-25 08:56:10 +08:00
最后,通过设置最高值来定义启动容器的顺序。确保 db_vm 容器首先启动,然后再启动 nginx_vm。
```
$ lxc config set db_vm boot.autostart.priority 100
$ lxc config set nginx_vm boot.autostart.priority 99
```
2017-12-25 08:56:10 +08:00
使用[下面的 bash 循环在 Linux 上查看所有][1]值:
```
#!/bin/bash
echo 'The current values of each vm boot parameters:'
for c in db_vm nginx_vm memcache_vm
do
echo "*** VM: $c ***"
for v in boot.autostart boot.autostart.priority boot.autostart.delay
do
echo "Key: $v => $(lxc config get $c $v) "
done
echo ""
done
```
2017-12-25 08:56:10 +08:00
示例输出:
![Fig.01: Get autostarting LXD containers values using a bash shell script][2]
--------------------------------------------------------------------------------
via: https://www.cyberciti.biz/faq/how-to-auto-start-lxd-containers-at-boot-time-in-linux/
作者:[Vivek Gite][a]
2017-12-25 08:56:10 +08:00
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.cyberciti.biz
[1]:https://www.cyberciti.biz/faq/bash-for-loop/
[2]:https://www.cyberciti.biz/media/new/faq/2017/02/Autostarting-LXD-containers-values.jpg