====== Installation von LXC auf Ubuntu 14.04 ======
Dokumentation+Beispiele:
* [[https://docs.oracle.com/cd/E37670_01/E37355/html/ol_app_containers.html]]
* [[https://www.thomas-krenn.com/de/wiki/Linux_Containers_LXC]]
* [[http://www.linux-community.de/Internal/Artikel/Print-Artikel/LinuxUser/2016/08/Getrennte-Welten#article_twichtige_lxc-kommandos]]
==== Installation von LXC ====
> aptitude install lxc debootstrap bridge-utils
==== allgemeine Infos zu LXC ====
Der Parameter ''"-n lxctest"'' beschreibt den Namen des Containers und der Parameter ''"-t sshd"'' wählt die Vorlage (''/usr/share/lxc/templates/lxc-sshd'') aus.
Alle Vorlagen liegen hier: ''/usr/share/lxc/templates/''
/usr/share/lxc/templates/lxc-alpine
/usr/share/lxc/templates/lxc-altlinux
/usr/share/lxc/templates/lxc-archlinux
/usr/share/lxc/templates/lxc-busybox
/usr/share/lxc/templates/lxc-centos
/usr/share/lxc/templates/lxc-cirros
/usr/share/lxc/templates/lxc-debian
/usr/share/lxc/templates/lxc-download
/usr/share/lxc/templates/lxc-fedora
/usr/share/lxc/templates/lxc-gentoo
/usr/share/lxc/templates/lxc-openmandriva
/usr/share/lxc/templates/lxc-opensuse
/usr/share/lxc/templates/lxc-oracle
/usr/share/lxc/templates/lxc-plamo
/usr/share/lxc/templates/lxc-sshd
/usr/share/lxc/templates/lxc-ubuntu
/usr/share/lxc/templates/lxc-ubuntu-cloud
#
# lxc: linux Container library
# Authors:
# Serge Hallyn
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
# This file contains helpers for the various lxc shell scripts
globalconf=/etc/lxc/lxc.conf
bindir=/usr/bin
templatedir=/usr/share/lxc/templates
lxcinitdir=/usr/lib/x86_64-linux-gnu
lxc_path=`lxc-config lxc.lxcpath`
lxc_vg=`lxc-config lxc.bdev.lvm.vg`
lxc_zfsroot=`lxc-config lxc.bdev.zfs.root`
dem Standard nach liegen die "Root-Dateisystembäume" der Container hier:
> ls -lha /var/lib/lxc/
==== Test-Container erstellen ====
> lxc-create -n lxctest -t sshd
> lxc-ls
lxctest
> lxc-info -n lxctest
Name: lxctest
State: STOPPED
> lxc-start -dn lxctest
> lxc-info -n lxctest
Name: lxctest
State: RUNNING
PID: 26638
IP: 10.0.3.45
CPU use: 0.07 seconds
BlkIO use: 0 bytes
Memory use: 4.15 MiB
KMem use: 0 bytes
Link: vethP8GV61
TX bytes: 1.16 KiB
RX bytes: 873 bytes
Total bytes: 2.02 KiB
Dieser Container hat die IP ''10.0.3.45'' bekommen.
==== eine eigene Vorlage für einen individuellen Container erstellen ====
In diesem Beispiel ist es unser Anliegen, dass wir eine Container-Vorlage erstellen, die praktisch eine bessere ChRoot-Umgebung darstellt.
Also in dem alle Programme aus dem Host-System vorhanden sind und nicht extra installiert werden müssen.
Das macht auch die Wartung einfacher.
Der Vorteil einer solchen Konstruktion ist, dass man ein Programm mehrfach starten kann.
Wenn man zu dem Programm noch unterschiedliche Konfigurationsdateien an Orten ablegt, auf die der entsprechende Container Zugriff hat, dann man auf einem Host das gleiche Programm mit unterchiedlichen Einstellungen fahren.
Leider gibt es soeine Vorlage nicht.
Die Vorlage "sshd" kommt dem schon am nächsten, die werden wir erweitern.
#!/bin/bash
#
# lxc: linux Container library
VERSION="v2017092100"
#==============================================================================#
### hiermit wird bereits ein funktionstüchtiger Container angelegt,
### allerdings kann der noch nicht genug
. /usr/share/lxc/templates/lxc-sshd $@
#==============================================================================#
### hier kommen die individuellen Änderungen am bestehenden
### sshd-Container, der für unsere Zwecke zu minimalistisch ist
#------------------------------------------------------------------------------#
### in das "/" des Containers wechseln
date > /tmp/lxc.log
if [ -z "$rootfs" ] ; then
echo "Es ist die Variable '$rootfs' nicht gesetzt." | tee -a /tmp/lxc.log
echo 'Es kann nicht in das RootFS gewechselt werden!' | tee -a /tmp/lxc.log
echo 'ABBRUCH!' | tee -a /tmp/lxc.log
else
if [ -d "$rootfs" ] ; then
cd $rootfs/
rm -f /tmp/lxc.log
else
echo "Die Variable '$rootfs' enthält kein Verzeichnis." | tee -a /tmp/lxc.log
echo 'Es kann nicht in das RootFS gewechselt werden!' | tee -a /tmp/lxc.log
echo 'ABBRUCH!' | tee -a /tmp/lxc.log
fi
fi
#------------------------------------------------------------------------------#
###
### fehlendes an der Basis
###
chmod 1777 tmp
### /etc
mv ../config ../config_sshd
cat ../config_sshd | grep -Fv 'etc/' > ../config
echo "lxc.mount.entry = /etc etc none ro,bind 0 0" >> ../config
rm -f ../config_sshd
### mknod: '/dev/xconsole': Read-only file system
cp ../config ../config_SiK
cat ../config_SiK | sed -e 's|dev dev none ro,bind|dev dev none rw,bind|' > ../config
rm -f ../config_SiK
### Sep 21 15:15:07 lxctest rsyslogd-2181: $WorkDirectory: /var/spool/rsyslog can not be accessed, probably does not exist - directive ignored [try http://www.rsyslog.com/e/2181 ]
### Sep 21 15:15:07 lxctest rsyslogd-2182: $WorkDirectory: trailing slashes removed, new value is '/var/spool/rsyslog' [try http://www.rsyslog.com/e/2182 ]
mkdir -p var/spool/rsyslog
# rsyslogd: run failed with error -2103 (see rsyslog.h or try http://www.rsyslog.com/e/2103 to learn what that number means)
mkdir -p var/log
chown root:adm var/log
chmod 0775 var/log
#------------------------------------------------------------------------------#
### SSH-Schlüssel für root hinterlegen
#
# dann sollte, nach dem Start des Containers,
# ein solcher Login beispielsweise funktionieren:
# ssh -i /etc/ssh/ssh_host_ed25519_key 10.0.3.174
#
cp -ar /root/.ssh root/
mkdir -p root/.ssh
chmod 0755 root/.ssh
touch root/.ssh/authorized_keys
chmod 0644 root/.ssh/authorized_keys
cat /etc/ssh/ssh_host_ed25519_key.pub >> root/.ssh/authorized_keys
cat /etc/ssh/ssh_host_rsa_key.pub >> root/.ssh/authorized_keys
#------------------------------------------------------------------------------#
### /root/bin - Skripte übernehmen
cp -ar /root/bin root/
#------------------------------------------------------------------------------#
### DB-Verzeichnisse
cp -a /root/.my.cnf root/
mkdir -p var/lib/mysql
mkdir -p var/log/mysql
chown -R mysql:mysql var/lib/mysql
chown -R mysql:mysql var/log/mysql
echo "lxc.mount.entry = /var/log/mysql var/log/mysql none rw,bind 0 0" >> ../config
#------------------------------------------------------------------------------#
### Test-DB-Verzeichnis (nur ein Beispiel, z.Z. deaktiviert)
mkdir -p var/lib/mysql/${name}
echo "#lxc.mount.entry = /var/lib/mysql/${name} var/lib/mysql none rw,bind 0 0" >> ../config
> lxc-create -n lxctest -t chroot ; lxc-start -dn lxctest ; sleep 5 ; lxc-info -n lxctest ; cd /var/lib/lxc/lxctest/rootfs/
Generating public/private rsa key pair.
Your identification has been saved in /var/lib/lxc/lxctest/rootfs/etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /var/lib/lxc/lxctest/rootfs/etc/ssh/ssh_host_rsa_key.pub.
The key fingerprint is:
52:d1:57:30:96:9f:5e:1d:b5:21:1d:2e:d5:f0:56:b3 root@tcdb07
The key's randomart image is:
+--[ RSA 2048]----+
| .. +=+**|
| ...o.++B|
| . . o E=|
| . +.o|
| . S . . |
| . . |
| |
| |
| |
+-----------------+
Generating public/private dsa key pair.
Your identification has been saved in /var/lib/lxc/lxctest/rootfs/etc/ssh/ssh_host_dsa_key.
Your public key has been saved in /var/lib/lxc/lxctest/rootfs/etc/ssh/ssh_host_dsa_key.pub.
The key fingerprint is:
e4:21:e4:ee:05:65:70:c1:12:26:fe:58:c2:14:c2:54 root@tcdb07
The key's randomart image is:
+--[ DSA 1024]----+
| oo.E.=+=. |
| .= =.+. |
| + =.o |
| * = . |
| . o S |
| . . |
| . |
| |
| |
+-----------------+
Name: lxctest
State: RUNNING
PID: 10762
IP: 10.0.3.168
CPU use: 0.07 seconds
BlkIO use: 0 bytes
Memory use: 4.39 MiB
KMem use: 0 bytes
Link: vethTGN7YW
TX bytes: 1.16 KiB
RX bytes: 914 bytes
Total bytes: 2.06 KiB
den öffentlichen Schlüssel hinterlegen:
> mkdir -p /var/lib/lxc/lxctest/rootfs/root/.ssh
> cat /etc/ssh/ssh_host_ed25519_key.pub >> /var/lib/lxc/lxctest/rootfs/root/.ssh/authorized_keys
Jetzt können wir uns so passwortlos vom Host-System aus einloggen:
> ssh -i /etc/ssh/ssh_host_ed25519_key 10.0.3.168
===== erweiterte Einstellungen =====
soll ein Container beim booten automatisch gestartet werden, dann muss man in dieser Datei folgende Einstellung vornehmen:
> vi /etc/default/lxc
...
LXC_AUTO="true"
...
...ist das erledigt, kann man in diesem Verzeichnis ein SymLink zum container ablegen und dann wird er bei booten automatisch gestartet:
/etc/lxc/auto
===== Netzwerkkonfiguration =====
==== Standard: lxc-net ====
Die Voreinstellungen für das Netzwerk sind hier zu finden:
> vi /etc/default/lxc-net
...
LXC_BRIDGE="lxcbr0"
LXC_ADDR="10.0.3.1"
LXC_NETMASK="255.255.255.0"
LXC_NETWORK="10.0.3.0/24"
LXC_DHCP_RANGE="10.0.3.2,10.0.3.254"
LXC_DHCP_MAX="253"
...
nach dem dort Änderungen vorgenommen wurden, muss der Dienst neu gestartet werden:
> service lxc-net restart
=== lxc-net deaktivieren ===
//Unter anderem arbeitet LXC-Net auch mit IP-Forwarding, was bei uns unerwünscht ist.//
die //Bridge// für den LXC-DHCP-Server runter fahren:
> service lxc-net stop
den DHCP-Server von LXC beenden (manchmal wird er nicht sauber gestoppt, wenn lxc-net runter fährt):
> killall dnsmasq
die //Bridge// für den LXC-DHCP-Server deaktivieren:
> echo 'USE_LXC_BRIDGE="false"' >> /etc/default/lxc-net
**//Zu beachten ist, dass im LXC-Container nach der Umstellung des Netzwerkes, der SSH-Dienst nicht mehr automatisch startet! Dafür sind weitere Maßnahmen erforderlich.//**"
==== eine eigene Bridge für die LXC-Container anlegen ====
* [[http://linuxwiki.de/EthernetBridge]]
* [[http://www.zdnet.de/41533156/einfuehrung-containervirtualisierung-mit-lxc/?inf_by=59c9f239681db848158b4662]]
* [[http://www.linux-community.de/Internal/Artikel/Print-Artikel/LinuxUser/2016/08/Getrennte-Welten#article_xnetzwerkkonfiguration]]
**LXC sagen, dass er in Zukunft unsere Bridge verwenden soll:**
> sed -ie 's/lxc.network.link/#&/' /etc/lxc/default.conf && rm -f /etc/lxc/default.confe
> echo 'lxc.network.link = br0' >> /etc/lxc/default.conf
''eth0'' steht hier für das NIC im Host-System und die IP-Adresse ''192.168.2.1/16'' war bisher auf dem Host-NIC:
> brctl addbr br0
> ifconfig eth0 0.0.0.0 promisc
> brctl addif br0 eth0
> ip a add 192.168.2.1/16 dev br0
> ifconfig br0 up
===== einen LXC-Container im Vordergrund starten =====
das wird meistens nur für Testzwecke so gemacht:
> lxc-execute -n lxctest -- /bin/bash
===== LXC-Container beim booten automatisch starten =====
> mkdir -p /etc/lxc/auto/lxctest/
> ln -s /var/lib/lxc/lxctest/config /etc/lxc/auto/lxctest/
===== Dienste im LXC-Container starten =====
LXC-Container (mit dem Namen ''lxctest'') starten:
> lxc-start -dn lxctest
die drei Dienste starten, die uns am Herzen liegen:
> lxc-attach -n lxctest -- /etc/init.d/rsyslog start &
> lxc-attach -n lxctest -- /etc/init.d/ssh start &
> lxc-attach -n lxctest -- /etc/init.d/mysql start &