lxc.rst
author Oleksandr Gavenko <gavenkoa@gmail.com>
Thu, 03 Jan 2019 22:03:33 +0200
changeset 2332 fc34d7c084ba
parent 2317 897bb1696e5f
child 2345 cb168a3d27ec
permissions -rw-r--r--
plpgsql

.. -*- coding: utf-8; -*-

=====
 LXC
=====
.. contents::
   :local:

LXC releases
============

https://discuss.linuxcontainers.org/tags/release
  Announces of LXC & LXD releases.
https://discuss.linuxcontainers.org/t/lxc-3-0-0-has-been-released/1449
  LXC 3.0.0 has been released.

Supported templates
===================

https://github.com/lxc/lxc-templates
  Old style template scripts for LXC (prefer distrobuilder).
https://github.com/lxc/distrobuilder
  System container image builder for LXC and LXD.
https://brauner.github.io/2018/02/27/lxc-removes-legacy-template-build-system.html
  Details about replacing template stripts.

Install LXC under Debian
========================
::

  $ sudo apt-get install lxc

Verify your host/kernel satisfy LXC requirements::

  $ sudo lxc-checkconfig

To make network bridge install supplement packages::

  $ sudo apt-get install bridge-utils dnsmasq

To bootstrap Debian dostro into container install::

  $ sudo apt-get install debootstrap

To bring up network bridge edir ``/etc/default/lxc-net``::

  USE_LXC_BRIDGE="true"

  LXC_ADDR="192.168.99.1"
  LXC_NETMASK="255.255.255.0"
  LXC_NETWORK="192.168.99.1/24"
  LXC_DHCP_RANGE="192.168.99.100,192.168.99.200"
  LXC_DHCP_MAX="20"

and start service::

  $ sudo service lxc-net start

Create new container in LXC
===========================

Create container from template::

  $ sudo lxc-create -t $TMPL -n $NAME -- $EXTRA_ARGS

.. note::
   * ``-t`` defines distro name
   * ``-n`` gives name for container for further referencing
   * everything after ``--`` is passed to template script

Template name is based on file name from ``/usr/share/lxc/templates`` directory without ``lxc-``
prefix.

List available templates with::

  $ ls -alh /usr/share/lxc/templates/

Examples of container creation command::

  $ sudo lxc-create -t debian -n deb-sid --  -r sid --enable-non-free
  $ sudo lxc-create -t debian -n deb-testing --  -r testing
  $ sudo lxc-create -t debian -n deb-stable --  -r stable -a amd64

  $ sudo lxc-create -t alpine -n alpine-3.7 --  -r 3.7

Each template has own options, which can be passed after ``--``. To get help on template specific
options run::

  $ /usr/share/lxc/templates/lxc-ubuntu -h
  $ /usr/share/lxc/templates/lxc-debian -h
  $ /usr/share/lxc/templates/lxc-alpine -h
  ...

For Debian in order to use another miror::

  $ MIRROR=http://httpredir.debian.org/debian sudo lxc-create -t debian -n debtest -- -r sid

Since LXC v3.0 sh-templates moved to separate project and only 4 are left supported::

  $ lxc-create my-busybox -t busybox
  $ lxc-create my-x -t download
  $ lxc-create c1 -t local -- --metadata /path/to/meta.tar.xz --fstree /path/to/rootfs.tar.xz
  $ lxc-create c2 -t oci -- --url docker://alpine

Put ``veth`` network configuration into container config ``/var/lib/lxc/$NAME/config``::

  lxc.network.type = veth
  lxc.network.flags = up
  lxc.network.link = lxcbr0
  lxc.network.veth.pair = veth-stretch
  lxc.network.name = veth-stretch
  lxc.network.ipv4 = 192.168.99.12/24
  lxc.network.ipv4.gateway = 192.168.99.1

Fix DNS resolution in container::

  $ echo nameserver 8.8.8.8 | sudo tee /var/lib/lxc/$NAME/rootfs/etc/resolv.conf

LXC Container management
========================

List available VMs::

  $ sudo lxc-ls

Start VM::

  $ sudo lxc-start -n $NAME

Safely stop VM (honoring init system)::

  $ sudo lxc-halt -n $NAME

Urgently stop VM::

  $ sudo lxc-stop -n $NAME

Mark container to start on boot in ``/var/lib/lxc/$NAME/config``::

  lxc.start.auto = 1

Link containers in non standard location to take them in account::

  $ ln -s /opt/lxc/$NAME /var/lib/lxc/$NAME

Run command in rinning container::

  $ sudo lxc-attach -n $NAME-get update

Making snapshot
===============

See ``lxc-snapshot(1)``.

List of snapshots::

  $ sudo lxc-snapshot --list
  $ sudo lxc-snapshot -n $NAME --list

Make a snapshot::

  $ sudo lcx-halt -n $NAME
  $ sudo lcx-snapshot -n $NAME

Restore from snapshot::

  $ sudo lcx-halt -n $NAME
  $ sudo lcx-snapshot -n $NAME -r $SNAPNAME