Intelligente Lösungen
in neuer Dimension

LXD/LXC: Installation auf Ubuntu-22.04

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

Aktualisieren

1
2
sudo apt update
sudo apt upgrade

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               ubuntu-vg
...
  VG Size               500 GiB
  PE Size               4,00 MiB
  Total PE              125000
  Alloc PE / Size       61684 / 240,95 GiB
  Free  PE / Size       57366 / 224,09 GiB
  VG UUID               CZfxss-UeDu-a12223
$ sudo lvcreate -n lxclv -L 50G ubuntu-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/ubuntu--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/ubuntu--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:22.04 ubuntu-2204
Creating ubuntu-2204
Retrieving image: Unpack: 100% (2.14GB/s)   
Starting ubuntu-2204

Container sichten

1
2
3
4
5
6
$ sudo lxc list
+-------------+---------+-----------------------+----------------------------------------------+------------+-----------+
|    NAME     |  STATE  |         IPV4          |                     IPV6                     |    TYPE    | SNAPSHOTS |
+-------------+---------+-----------------------+----------------------------------------------+------------+-----------+
| ubuntu-2204 | 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-2204 /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-2204 | STOPPED |      |      | PERSISTENT |           |
+-------------+---------+------+------+------------+-----------+
$ lxc start ubuntu-2204
$ lxc list
+-------------+---------+-----------------------+----------------------------------------------+------------+-----------+
|    NAME     |  STATE  |         IPV4          |                     IPV6                     |    TYPE    | SNAPSHOTS |
+-------------+---------+-----------------------+----------------------------------------------+------------+-----------+
| ubuntu-2204 | RUNNING | 10.239.106.134 (eth0) | fd42:cc27:326:facd:216:3eff:fef6:251f (eth0) | PERSISTENT |           |
+-------------+---------+-----------------------+----------------------------------------------+------------+-----------+
$ lxc stop ubuntu-2204
$ lxc list
+-------------+---------+------+------+------------+-----------+
|    NAME     |  STATE  | IPV4 | IPV6 |    TYPE    | SNAPSHOTS |
+-------------+---------+------+------+------------+-----------+
| ubuntu-2204 | STOPPED |      |      | PERSISTENT |           |
+-------------+---------+------+------+------------+-----------+

Offene Punkte

Nachtrag 2022-12-24: Eine Version dieses Dokumentes mit der Korrektur der offenen Punkte findet sich hier.

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

Links

Änderungen

  • 2022-12-24: Hinweis auf Version ohne offene Punkte hinzugefügt
  • 2022-10-19: Erste Version