lxc auto start on boot

最後更新: 2020-07-20

目錄

 


Auto Start

 

"lxc-autostart" processes containers with lxc.start.auto & -g "onboot,"

Contrainer Setting

  • lxc.start.auto
  • lxc.start.order
  • lxc.start.delay
  • lxc.group (要小心設定)

# Whether the container should be auto-started

i.e. The auto-starting a series of containers at once.

lxc.start.auto = 1

i.e. The startup will be in order of 2 (沒有設定 order 的會在 group 內最後 start)

lxc.start.order = 2

# 延遲多久才啟動下一個 vps => give the current container time to begin initialization (# Unit: seconds)

lxc.start.delay = 30

# Two groups are special: "onboot" group, "NULL" group (Default 只有此兩個 Group)

# 設定此 vps 是屬於 web Group
# 由於 BOOTGROUPS 只有 "onboot,", 所以它是不會 auto-start
lxc.group = web

BOOTGROUPS

/etc/init/lxc.conf 的 code

# These can be overridden in /etc/default/lxc
...
[ "x$LXC_AUTO" = "xtrue" ] || exit 0

if [ -n "$BOOTGROUPS" ]
then
        BOOTGROUPS="-g $BOOTGROUPS"
fi
...

/etc/default/lxc

# start containers at boot
LXC_AUTO="true"

# Leading comma, trailing comma => NULL group
BOOTGROUPS="onboot,"

Remark: MySetting

LXC_AUTO="true"
BOOTGROUPS="onboot,,web"

Checking

lxc-ls -f

NAME  STATE   AUTOSTART GROUPS IPV4           IPV6
lamp  RUNNING 1         web    192.168.123.11 -
proxy RUNNING 1         web    192.168.123.14 -
sshgw RUNNING 1         -      192.168.123.13 -
tools RUNNING 1         web    192.168.123.15 -

-L,--list

Rather than performing the action, just print the container name and wait delays until starting the next container.

 * Container 在 Stop 時才 list 到

lxc-autostart -L

lamp 5

OS Setting

# C8

 


U16 Enable Service

 

systemctl status lxc

● lxc.service - LXC Container Initialization and Autoboot Code
   Loaded: loaded (/lib/systemd/system/lxc.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:lxc-autostart
           man:lxc

systemctl enable lxc

Created symlink from /etc/systemd/system/multi-user.target.wants/lxc.service to /lib/systemd/system/lxc.service.

systemctl status lxc

● lxc.service - LXC Container Initialization and Autoboot Code
   Loaded: loaded (/lib/systemd/system/lxc.service; enabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:lxc-autostart
           man:lxc

reboot 後

● lxc.service - LXC Container Initialization and Autoboot Code
   Loaded: loaded (/lib/systemd/system/lxc.service; enabled; vendor preset: enabled)
   Active: active (exited) since Tue 2020-11-17 10:02:24 HKT; 3min 33s ago
     Docs: man:lxc-autostart
           man:lxc
  Process: 1531 ExecStart=/usr/lib/x86_64-linux-gnu/lxc/lxc-containers start (code=exited, status=0/SUCCESS)
  Process: 1453 ExecStartPre=/usr/lib/x86_64-linux-gnu/lxc/lxc-apparmor-load (code=exited, status=0/SUCCESS)
 Main PID: 1531 (code=exited, status=0/SUCCESS)
    Tasks: 1
   Memory: 1.3M
      CPU: 22ms
   CGroup: /system.slice/lxc.service
           └─1543 [lxc monitor] /var/lib/lxc lamp

Nov 17 10:02:18 lxc systemd[1]: Starting LXC Container Initialization and Autoboot Code...
Nov 17 10:02:24 lxc systemd[1]: Started LXC Container Initialization and Autoboot Code.