Intelligente Lösungen
in neuer Dimension

LXD/LXC: Installation auf KDE Neon 18.04

Hier beschreibe ich, wie ich LXD/LXC auf KDE Neon 18.04 in Betrieb nehme. Die ganze Aktion dauert grob 1 Stunde, wenn man halbwegs weiß, was man zu erledigen hat.

Aktualisieren

1
sudo pkcon update

LXD installieren

1
sudo snap install lxd

Plattenbereich für LXC anlegen

1
2
3
4
5
6
7
8
9
10
11
12
$ sudo vgdisplay
  --- Volume group ---
  VG Name               neon-vg
...
  VG Size               <465,04 GiB
  PE Size               4,00 MiB
  Total PE              119050
  Alloc PE / Size       61684 / 240,95 GiB
  Free  PE / Size       57366 / <224,09 GiB
  VG UUID               CZfxss-UeDu-s1pZ-dwb6-Iffa-pOuO-84otnm
$ sudo lvcreate -n lxclv -L 50G neon-vg
 Logical volume "lxclv" created.

LXD initialisieren

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$ sudo lxd init
Would you like to use LXD clustering? (yes/no) [default=no]: 
Do you want to configure a new storage pool? (yes/no) [default=yes]: 
Name of the new storage pool [default=default]: 
Name of the storage backend to use (btrfs, ceph, dir, lvm, zfs) [default=zfs]: btrfs
Create a new BTRFS pool? (yes/no) [default=yes]: yes
Would you like to use an existing block device? (yes/no) [default=no]: yes
Path to the existing block device: /dev/mapper/neon--vg-lxclv
Would you like to connect to a MAAS server? (yes/no) [default=no]: no
Would you like to create a new local network bridge? (yes/no) [default=yes]: 
What should the new bridge be called? [default=lxdbr0]: 
What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: 
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: 
Would you like LXD to be available over the network? (yes/no) [default=no]: no
Would you like stale cached images to be updated automatically? (yes/no) [default=yes] 
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]: yes
config: {}
networks: []
storage_pools:
- config:
    source: /dev/mapper/neon--vg-lxclv
  description: ""
  name: default
  driver: btrfs
profiles:
- config: {}
  description: ""
  devices:
    eth0:
      name: eth0
      nictype: bridged
      parent: lxdbr0
      type: nic
    root:
      path: /
      pool: default
      type: disk
  name: default
cluster: null

Container anlegen

1
2
3
4
$ sudo lxc launch ubuntu:18.04 ubuntu-1804
Creating ubuntu-1804
Retrieving image: Unpack: 100% (2.14GB/s)   
Starting ubuntu-1804                        

Container sichten

1
2
3
4
5
6
$ sudo lxc list
+-------------+---------+-----------------------+----------------------------------------------+------------+-----------+
|    NAME     |  STATE  |         IPV4          |                     IPV6                     |    TYPE    | SNAPSHOTS |
+-------------+---------+-----------------------+----------------------------------------------+------------+-----------+
| ubuntu-1804 | RUNNING | 10.239.106.134 (eth0) | fd42:cc27:326:facd:216:3eff:fef6:251f (eth0) | PERSISTENT |           |
+-------------+---------+-----------------------+----------------------------------------------+------------+-----------+

Container-Kommandos ohne SUDO

1
2
3
4
sudo usermod -aG lxd ${USER}
# Abmelden und wieder anmelden falls ${USER} dein eigenes Benutzerkonto ist
# Nachkontrolle - muß "lxd" ausgeben
id ${USER}|grep -o lxd

Basisimage für Uli

Prompt vom Container holen mit sudo lxc exec ubuntu-1804 /bin/bash und dann im Container:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Aktualisieren
apt-get -y update
apt-get -y upgrade
apt-get -y dist-upgrade
apt-get -y autoremove
apt-get -y clean
# Zusatzpakete
apt-get -y install joe
apt-get -y install apt-transport-https
apt-get -y install openssh-server
apt-get -y install net-tools # ... provides ifconfig and netstat
apt-get -y clean
# Autostart für SSH
systemctl enable ssh
# PubKey eintragen
mkdir /root/.ssh
chmod 700 /root/.ssh
touch /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
cat >>/root/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA1cd... Uli's SSH Key
^D
cat >>/etc/bash.bashrc
HISTFILESIZE=
HISTSIZE=
HISTTIMEFORMAT="[%F %T] "
# Change the file location because certain bash sessions truncate .bash_history file upon close.
# http://superuser.com/questions/575479/bash-history-truncated-to-500-lines-on-each-login
HISTFILE=~/.bash_eternal_history
# Force prompt to write history after every command.
# http://superuser.com/questions/20900/bash-history-loss
PROMPT_COMMAND="history -a; ${PROMPT_COMMAND:-true}"
# Log last command to syslog
log_command () {
 echo "${USER} $(HISTTIMEFORMAT='' builtin history 1|cut -c8-)" |  logger -t shell -p user.info
}
PROMPT_COMMAND="${PROMPT_COMMAND:-true};log_command"
^D
for f in /etc/skel/.bashrc /root/.bashrc /home/ubuntu/.bashrc; do\
  sed -i 's/^\(HISTSIZE\|HISTFILESIZE\)/#\1/' "${f}"
done
timedatectl set-timezone Europe/Berlin

Schlusstests

Nach einem Reboot teste ich dies:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ lxc list
+-------------+---------+------+------+------------+-----------+
|    NAME     |  STATE  | IPV4 | IPV6 |    TYPE    | SNAPSHOTS |
+-------------+---------+------+------+------------+-----------+
| ubuntu-1804 | STOPPED |      |      | PERSISTENT |           |
+-------------+---------+------+------+------------+-----------+
$ lxc start ubuntu-1804
$ lxc list
+-------------+---------+-----------------------+----------------------------------------------+------------+-----------+
|    NAME     |  STATE  |         IPV4          |                     IPV6                     |    TYPE    | SNAPSHOTS |
+-------------+---------+-----------------------+----------------------------------------------+------------+-----------+
| ubuntu-1804 | RUNNING | 10.239.106.134 (eth0) | fd42:cc27:326:facd:216:3eff:fef6:251f (eth0) | PERSISTENT |           |
+-------------+---------+-----------------------+----------------------------------------------+------------+-----------+
$ lxc stop ubuntu-1804
$ lxc list
+-------------+---------+------+------+------------+-----------+
|    NAME     |  STATE  | IPV4 | IPV6 |    TYPE    | SNAPSHOTS |
+-------------+---------+------+------+------------+-----------+
| ubuntu-1804 | STOPPED |      |      | PERSISTENT |           |
+-------------+---------+------+------+------------+-----------+

Offene Punkte

  • Ansprechen der Container via Containername, d.h. ich hätte gerne mit ssh ubuntu-1804 eine SSH-Session in den Container hinein
  • Separierung: Container mit und ohne Internet-Zugriff

Links

Änderungen

  • 2019-05-24: Offene Punkte hinzugefügt
  • 2019-05-20: Erste Version