Lxd: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
F (Diskussion | Beiträge) |
F (Diskussion | Beiträge) |
||
| (20 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt) | |||
| Zeile 1: | Zeile 1: | ||
= Allgemein = | |||
* Orte | |||
** /var/snap/lxd/common/lxd/storage-pools/default/containers | |||
= Installation = | = Installation = | ||
<source lang=bash> | <source lang=bash> | ||
echo 1 > /proc/sys/net/ipv4/ip_forward | |||
/etc/sysctl.conf: | |||
net.ipv4.ip_forward = 1 | |||
/etc/netplan/01-netcfg.yaml | /etc/netplan/01-netcfg.yaml | ||
</source> | </source> | ||
| Zeile 92: | Zeile 105: | ||
lxc image list images: |egrep focal| egrep amd64 | lxc image list images: |egrep focal| egrep amd64 | ||
lxc image list | lxc image list | ||
</source> | |||
== lxd sql == | |||
<source lang=Bash> | |||
lxd sql global "SELECT name FROM sqlite_master WHERE type='table';" | |||
</source> | |||
== Container Configuration zeigen == | |||
<source lang=Bash> | |||
lxc config show containername | |||
</source> | |||
== Festplatten/Container Größe anzeigen == | |||
<source lang=bash> | |||
lxc storage list | |||
lxc storage volume list default | |||
lxc storage info default | |||
zfs list | |||
lxc info $containername | |||
zfs list -t snapshot | |||
zfs list -o space | |||
</source> | |||
* https://discuss.linuxcontainers.org/t/how-to-check-lxd-container-size-and-how-much-space-they-are-tacking/4770/3 | |||
== snapshot == | |||
<source lang=bash> | |||
zfs list -t snapshot | |||
zfs list -o space | |||
</source> | |||
=== snapshot löschen === | |||
==== lxc ebene ==== | |||
* anzeige und xargs -I {} lxc delete {} dann auskomentiert, löscht | |||
<source lang=bash> | |||
lxc info <containername> | grep snap | awk '{print "<containername>/"$1}' # | xargs -I {} lxc delete {} | |||
</source> | |||
==== zfs ebene ==== | |||
Achtung: er macht zwar was er soll, lxc bekommt es aber nicht mit !!! , sprich lxc info/ls zeigt immer noch snapshots (die nicht mehr existieren) an | |||
* snapshots anzeigen und löschen | |||
** zeigt so ausgeführt, alle snapshots vom container an, mit dem auskommentierten 'xargs -n1 zfs destroy' löscht er sie dann | |||
<source lang=bash> | |||
zfs list -t snapshot | |||
zfs list -t snapshot -H -o name | grep CONTAINERNAME@snap #| xargs -n1 zfs destroy | |||
</source> | |||
* automatische snapshots löschen | |||
<source lang=bash> | |||
# anzeigen ob automatisches snapshotten aktiviert ist | |||
zfs get com.sun:auto-snapshot | |||
# deaktivieren | |||
zfs set com.sun:auto-snapshot=false default | |||
zpool get listsnapshots default | |||
</source> | |||
** https://docs.oracle.com/cd/E19253-01/820-2313/gbiqe/index.html | |||
== Backup Script LXD Container mit Rsync == | |||
* erstelle ein backup der mysql datenbanken der container | |||
* erstelle snapshot | |||
* rsync die container festplatten | |||
<source lang=bash> | |||
#!/bin/bash | |||
today=$(date "+%A") | |||
month=$(date "+%B") | |||
echo "" | |||
echo $0 | |||
date | |||
echo | |||
backupPath='/media/ancdata/2024backup' | |||
backupPathRsyncBackup='/media/ancdata/2024backup/rsyncOld' | |||
mountPath='/media/snapshot' | |||
for container in `lxc ls -c ns --format=csv|grep RUNNING|cut -f1 -d,`; do | |||
echo "" | |||
echo "* backup $container" | |||
date | |||
echo "** DB" | |||
lxc exec $container -- /usr/bin/mysqldump --all-databases | gzip > $backupPath/backup.$container.sql.gz | |||
cp $backupPath/backup.$container.sql.gz $backupPath/backup.$container.$month.sql.gz | |||
mv $backupPath/backup.$container.sql.gz $backupPath/backup.$container.$today.sql.gz | |||
echo "** Container" | |||
lxc snapshot $container | |||
snapshotPath=$(zfs list -t snapshot | grep anc | tail -n 1 | awk '{print $1}') | |||
mkdir -p $mountPath | |||
mount -t zfs $snapshotPath $mountPath | |||
mkdir -p $backupPath/$container | |||
rsync -ahb --stats --delete --numeric-ids $mountPath/* $backupPath/$container/ --backup-dir=$backupPathRsyncBackup | |||
umount $mountPath | |||
rmdir $mountPath | |||
done | |||
echo "" | |||
echo -- stop | |||
date | |||
echo "" | |||
</source> | |||
* https://pieterbakker.com/how-to-backup-lxd-containers-to-a-remote-host-with-rsync/ | |||
== in den lxd container dateien oder verzeichnisse kopieren (anders geht nicht!!! Permission denied) == | |||
<source lang=bash> | |||
lxc file push -r dir containername/var/www/ | |||
lxc file push file.txt containername/var/www/ | |||
</source> | </source> | ||
| Zeile 113: | Zeile 249: | ||
</source> | </source> | ||
= Container | = Container = | ||
== Container Installation == | |||
<source lang=bash> | <source lang=bash> | ||
lxc launch ubuntu:20.04 ubuntu2020 | lxc launch ubuntu:20.04 ubuntu2020 | ||
| Zeile 121: | Zeile 258: | ||
</source> | </source> | ||
* | * falls zb docker im lxc container laufen soll, muss nesting angestellt werden | ||
<source lang=bash> | <source lang=bash> | ||
lxc config set ubuntu2024 security.nesting true | |||
</source> | |||
** lxd - docker (proc Permission Errors) | |||
*** https://discuss.linuxcontainers.org/t/lxd-nesting-containers-with-docker/13713 | |||
*** https://www.dedoimedo.com/computers/docker-lxd-container-nesting.html | |||
*** Fehlermeldungen | |||
<source lang=Bash> | |||
Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "proc" to rootfs at "/proc": mount proc:/proc (via /proc/self/fd/6), flags: 0xe: permission denied: unknown | |||
I | |||
sda: Failed to write 'change' to '/sys/devices/pci0000:00/0000:00:17.0/ata2/host1/target1:0:0/1:0:0:0/block/sda/uevent': Permission denied | |||
</source> | |||
== Container Netzwerk == | |||
* /etc/netplan/50-cloud-init.yaml mit nach folgendem ersetzen | |||
<source lang=bash> | |||
network: | network: | ||
version: 2 | version: 2 | ||
| Zeile 138: | Zeile 291: | ||
nameservers: | nameservers: | ||
addresses: [8.8.8.8] | addresses: [8.8.8.8] | ||
</source> | |||
<source lang=bash> | |||
exit | |||
lxc stop ubuntu2020 | |||
lxc list | |||
lxc start ubuntu2020 | |||
</source> | </source> | ||
| Zeile 151: | Zeile 309: | ||
#!/bin/bash | #!/bin/bash | ||
if [ "$1" == "" ]; then | if [ "$1" == "" ]; then | ||
echo Beispiel: $0 containername betriebsystem | |||
exit 1 | |||
fi | fi | ||
if ! [ -d "/var/lib/lxc/$1" ] ; then | if ! [ -d "/var/lib/lxc/$1" ] ; then | ||
echo /var/lib/lxc/$1 existiert nicht! | |||
exit | |||
fi | fi | ||
if [ "$2" == "" ]; then | if [ "$2" == "" ]; then | ||
echo Beispiel: $0 containername betriebsystem | |||
echo lxc image list images: | |||
echo es wird ubuntu:20.04 genomme | |||
besys='ubuntu:20.04' | |||
else | else | ||
besys=$2 | |||
fi | fi | ||
| Zeile 176: | Zeile 334: | ||
STORAGEPATH=/var/snap/lxd/common/lxd/storage-pools/${STORAGE_POOL}/containers/ | STORAGEPATH=/var/snap/lxd/common/lxd/storage-pools/${STORAGE_POOL}/containers/ | ||
lxc-stop -n ${CONTAINER_NAME} | #lxc-stop -n ${CONTAINER_NAME} | ||
if lxc list | grep ${CONTAINER_NAME} | |||
then | |||
echo -e "\n ${CONTAINER_NAME} darf nicht existieren!" | |||
echo -e "\n wenn gewuenscht\n lxc delete ${CONTAINER_NAME} " | |||
exit | |||
fi | |||
lxc init $besys ${CONTAINER_NAME} -c security.privileged=true -c boot.autostart=true | lxc init $besys ${CONTAINER_NAME} -c security.privileged=true -c boot.autostart=true | ||
#lxc stop ${CONTAINER_NAME} | #lxc stop ${CONTAINER_NAME} | ||
| Zeile 183: | Zeile 350: | ||
# zfs list | # zfs list | ||
## Benutze NAME z.B default/containers/ncsync | ## Benutze NAME z.B default/containers/ncsync | ||
zfs mount | zfs set mountpoint=${STORAGEPATH}${CONTAINER_NAME}/ ${STORAGE_POOL}/containers/${CONTAINER_NAME} | ||
zfs mount ${STORAGE_POOL}/containers/${CONTAINER_NAME} | |||
if df -h | grep ${STORAGE_POOL}/containers/${CONTAINER_NAME} | |||
then | |||
echo -e "\nOK: Installation und mounten des Containers erfolgreich!!!!"; | |||
df -h | grep ${STORAGE_POOL}/containers/${CONTAINER_NAME} | |||
echo -e "\nverschiebt den orginalen rootfs" | |||
mv ${STORAGEPATH}${CONTAINER_NAME}/rootfs{,.org} | mv ${STORAGEPATH}${CONTAINER_NAME}/rootfs{,.org} | ||
rsync -ah --stats | rsync -ah --stats --numeric-ids /var/lib/lxc/${CONTAINER_NAME}/rootfs ${STORAGEPATH}${CONTAINER_NAME}/ | ||
mv ${STORAGEPATH}${CONTAINER_NAME}/rootfs/dev{,.org} | mv ${STORAGEPATH}${CONTAINER_NAME}/rootfs/dev{,.org} | ||
rsync -ah --stats | rsync -ah --stats ${STORAGEPATH}${CONTAINER_NAME}/rootfs.org/dev ${STORAGEPATH}${CONTAINER_NAME}/rootfs/ | ||
rm -r ${STORAGEPATH}${CONTAINER_NAME}/rootfs.org | rm -r ${STORAGEPATH}${CONTAINER_NAME}/rootfs.org | ||
umount ${STORAGEPATH}${CONTAINER_NAME} | umount ${STORAGEPATH}${CONTAINER_NAME} | ||
| Zeile 208: | Zeile 384: | ||
lxc config set ${CONTAINER_NAME} security.privileged false | lxc config set ${CONTAINER_NAME} security.privileged false | ||
lxc start ${CONTAINER_NAME} | lxc start ${CONTAINER_NAME} | ||
sleep 5 | |||
lxc list | |||
echo "wenn der container in der liste auftaucht hat erstmal alles geklappt" | |||
echo "jetzt muss im Zweifel noch /etc/netplan/50-cloud-init.yaml konfiguriert werden!!!!" | |||
lxc | echo -e "\n\n lxc exec ${CONTAINER_NAME} -- su --login root" | ||
lxc exec ${CONTAINER_NAME} -- su --login root | |||
else | |||
echo "Fehler: Installation und mount des Containers haben nicht geklappt"; | |||
fi | |||
</source> | </source> | ||
Aktuelle Version vom 5. April 2025, 13:05 Uhr
Allgemein
- Orte
- /var/snap/lxd/common/lxd/storage-pools/default/containers
Installation
echo 1 > /proc/sys/net/ipv4/ip_forward
/etc/sysctl.conf:
net.ipv4.ip_forward = 1
/etc/netplan/01-netcfg.yaml
network:
version: 2
renderer: networkd
ethernets:
enp0s31f6:
addresses:
- X.X.X.X/32 # IP vom SERVER
- 0x00:0x0:0x:0000::0/64
routes:
- on-link: true
to: 0.0.0.0/0
via: XX.XXX.XX.XX
gateway6: fe80::1
nameservers:
addresses:
- 213.133.XX.XX
- 213.133.XXX.XXX
- 213.133.XXX.XXX
- 2a01:4f8:X:X::add:XXXX
- 2a01:4f8:X:X::add:XXXX
- 2a01:4f8:X:X::add:XXXX
bridges:
vmbr0:
addresses: [ X.X.X.X/28 ] #IP von der Bridge
parameters:
stp: false
forward-delay: 4
netplan apply
apt install lxd
adduser nc
adduser lxd
adduser nc lxd
lxd init
# /media/data/serverconf/lxd.yaml
config: {}
networks: []
storage_pools:
- config:
source: /dev/md2
description: ""
name: default
driver: zfs
profiles:
- config: {}
description: ""
devices:
eth0:
name: eth0
nictype: bridged
parent: vmbr0
type: nic
root:
path: /
pool: default
type: disk
name: default
cluster: null
Befehle
lxc list
lxc profile list
lxc network list
lxc storage list
lxc image list images:
lxc image list images: |egrep focal| egrep amd64
lxc image list
lxd sql
lxd sql global "SELECT name FROM sqlite_master WHERE type='table';"
Container Configuration zeigen
lxc config show containername
Festplatten/Container Größe anzeigen
lxc storage list
lxc storage volume list default
lxc storage info default
zfs list
lxc info $containername
zfs list -t snapshot
zfs list -o space
snapshot
zfs list -t snapshot
zfs list -o space
snapshot löschen
lxc ebene
- anzeige und xargs -I {} lxc delete {} dann auskomentiert, löscht
lxc info <containername> | grep snap | awk '{print "<containername>/"$1}' # | xargs -I {} lxc delete {}
zfs ebene
Achtung: er macht zwar was er soll, lxc bekommt es aber nicht mit !!! , sprich lxc info/ls zeigt immer noch snapshots (die nicht mehr existieren) an
- snapshots anzeigen und löschen
- zeigt so ausgeführt, alle snapshots vom container an, mit dem auskommentierten 'xargs -n1 zfs destroy' löscht er sie dann
zfs list -t snapshot
zfs list -t snapshot -H -o name | grep CONTAINERNAME@snap #| xargs -n1 zfs destroy
- automatische snapshots löschen
# anzeigen ob automatisches snapshotten aktiviert ist
zfs get com.sun:auto-snapshot
# deaktivieren
zfs set com.sun:auto-snapshot=false default
zpool get listsnapshots default
Backup Script LXD Container mit Rsync
- erstelle ein backup der mysql datenbanken der container
- erstelle snapshot
- rsync die container festplatten
#!/bin/bash
today=$(date "+%A")
month=$(date "+%B")
echo ""
echo $0
date
echo
backupPath='/media/ancdata/2024backup'
backupPathRsyncBackup='/media/ancdata/2024backup/rsyncOld'
mountPath='/media/snapshot'
for container in `lxc ls -c ns --format=csv|grep RUNNING|cut -f1 -d,`; do
echo ""
echo "* backup $container"
date
echo "** DB"
lxc exec $container -- /usr/bin/mysqldump --all-databases | gzip > $backupPath/backup.$container.sql.gz
cp $backupPath/backup.$container.sql.gz $backupPath/backup.$container.$month.sql.gz
mv $backupPath/backup.$container.sql.gz $backupPath/backup.$container.$today.sql.gz
echo "** Container"
lxc snapshot $container
snapshotPath=$(zfs list -t snapshot | grep anc | tail -n 1 | awk '{print $1}')
mkdir -p $mountPath
mount -t zfs $snapshotPath $mountPath
mkdir -p $backupPath/$container
rsync -ahb --stats --delete --numeric-ids $mountPath/* $backupPath/$container/ --backup-dir=$backupPathRsyncBackup
umount $mountPath
rmdir $mountPath
done
echo ""
echo -- stop
date
echo ""
in den lxd container dateien oder verzeichnisse kopieren (anders geht nicht!!! Permission denied)
lxc file push -r dir containername/var/www/
lxc file push file.txt containername/var/www/
- container spezifisches
* login
lxc exec containername -- su --login root
* mount
zfs mount default/containers/containername
Begrenzungen
lxc config set ancontainer limits.cpu 0,3
lxc config set ancontainer limits.memory 1GB
# Überprüfen
cat /proc/cpuinfo | grep processor
free -h
Container
Container Installation
lxc launch ubuntu:20.04 ubuntu2020
lxc list
lxc exec ubuntu2020 -- su --login root
- falls zb docker im lxc container laufen soll, muss nesting angestellt werden
lxc config set ubuntu2024 security.nesting true
- lxd - docker (proc Permission Errors)
Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "proc" to rootfs at "/proc": mount proc:/proc (via /proc/self/fd/6), flags: 0xe: permission denied: unknown
I
sda: Failed to write 'change' to '/sys/devices/pci0000:00/0000:00:17.0/ata2/host1/target1:0:0/1:0:0:0/block/sda/uevent': Permission denied
Container Netzwerk
- /etc/netplan/50-cloud-init.yaml mit nach folgendem ersetzen
network:
version: 2
ethernets:
eth0:
addresses:
- 192.168.0.247/32
routes:
- to: 0.0.0.0/0
via: 192.168.0.1
on-link: true
dhcp4: no
dhcp-identifier: mac
nameservers:
addresses: [8.8.8.8]
exit
lxc stop ubuntu2020
lxc list
lxc start ubuntu2020
Migration lxc Container zu lxd Container
Migrations Skript
- mit lsb_release -a kann mensch im original container noch mal schauen, was für ein betriebsystem überhaupt installiert ist
#!/bin/bash
if [ "$1" == "" ]; then
echo Beispiel: $0 containername betriebsystem
exit 1
fi
if ! [ -d "/var/lib/lxc/$1" ] ; then
echo /var/lib/lxc/$1 existiert nicht!
exit
fi
if [ "$2" == "" ]; then
echo Beispiel: $0 containername betriebsystem
echo lxc image list images:
echo es wird ubuntu:20.04 genomme
besys='ubuntu:20.04'
else
besys=$2
fi
echo $0 $1 $besys
CONTAINER_NAME=$1
STORAGE_POOL=default
STORAGEPATH=/var/snap/lxd/common/lxd/storage-pools/${STORAGE_POOL}/containers/
#lxc-stop -n ${CONTAINER_NAME}
if lxc list | grep ${CONTAINER_NAME}
then
echo -e "\n ${CONTAINER_NAME} darf nicht existieren!"
echo -e "\n wenn gewuenscht\n lxc delete ${CONTAINER_NAME} "
exit
fi
lxc init $besys ${CONTAINER_NAME} -c security.privileged=true -c boot.autostart=true
#lxc stop ${CONTAINER_NAME}
#lxc config set ${CONTAINER_NAME} boot.autostart true
###
# zfs list
## Benutze NAME z.B default/containers/ncsync
zfs set mountpoint=${STORAGEPATH}${CONTAINER_NAME}/ ${STORAGE_POOL}/containers/${CONTAINER_NAME}
zfs mount ${STORAGE_POOL}/containers/${CONTAINER_NAME}
if df -h | grep ${STORAGE_POOL}/containers/${CONTAINER_NAME}
then
echo -e "\nOK: Installation und mounten des Containers erfolgreich!!!!";
df -h | grep ${STORAGE_POOL}/containers/${CONTAINER_NAME}
echo -e "\nverschiebt den orginalen rootfs"
mv ${STORAGEPATH}${CONTAINER_NAME}/rootfs{,.org}
rsync -ah --stats --numeric-ids /var/lib/lxc/${CONTAINER_NAME}/rootfs ${STORAGEPATH}${CONTAINER_NAME}/
mv ${STORAGEPATH}${CONTAINER_NAME}/rootfs/dev{,.org}
rsync -ah --stats ${STORAGEPATH}${CONTAINER_NAME}/rootfs.org/dev ${STORAGEPATH}${CONTAINER_NAME}/rootfs/
rm -r ${STORAGEPATH}${CONTAINER_NAME}/rootfs.org
umount ${STORAGEPATH}${CONTAINER_NAME}
#sed -i 's|lxc.start.auto = .*|lxc.start.auto = 0|g' /var/lib/lxc/${CONTAINER_NAME}/config
lxc config set ${CONTAINER_NAME} volatile.eth0.hwaddr $(grep lxc.net.0.hwaddr /var/lib/lxc/${CONTAINER_NAME}/config | cut -d"=" -f2)
lxc config show ${CONTAINER_NAME}
#lxc-ls -n ${CONTAINER_NAME} -f
lxc start ${CONTAINER_NAME}
lxc stop ${CONTAINER_NAME}
lxc config set ${CONTAINER_NAME} security.privileged false
lxc start ${CONTAINER_NAME}
lxc stop ${CONTAINER_NAME}
lxc config set ${CONTAINER_NAME} security.privileged true
lxc start ${CONTAINER_NAME}
lxc stop ${CONTAINER_NAME}
lxc config set ${CONTAINER_NAME} security.privileged false
lxc start ${CONTAINER_NAME}
sleep 5
lxc list
echo "wenn der container in der liste auftaucht hat erstmal alles geklappt"
echo "jetzt muss im Zweifel noch /etc/netplan/50-cloud-init.yaml konfiguriert werden!!!!"
echo -e "\n\n lxc exec ${CONTAINER_NAME} -- su --login root"
lxc exec ${CONTAINER_NAME} -- su --login root
else
echo "Fehler: Installation und mount des Containers haben nicht geklappt";
fi
Weitergehende Möglichkeiten
lxd sql global "Select * FROM storage_pools"
lxd sql global "Select * FROM profiles_devices_config"
# Zum Beispiel könnten Storages so umbenannt werden (evtl keine gute Idee!!!! )
lxd sql global "UPDATE storage_pools SET name='newname' WHERE name='oldname';"
lxd sql global "UPDATE profiles_devices_config SET value='newname' WHERE value='oldname' AND key='pool';"
# Datenbank Tabellen und Inhalte
lxd sql <local|global> [.dump|.schema]
lxd sql global ".dump" >lxcinfo.txt
lxd sql global ".schema" >lxcschema.txt
https://lxd.readthedocs.io/en/latest/database/
Probleme
- irgendwie mit symlinks
Quellen
- verwendete links