# LXD/LXC cheat sheet I've installed LXD on my home server and have found a lot of syntax and one-liners that I've yet to commit to memory. So I'll put them here. ## Install LXD ```shell snap install lxd sudo apt install -y git build-essential libssl-dev python3-venv python3-pip python3-dev zfsutils-linux bridge-utils ``` ## General links * [How to initialize LXD again](https://blog.simos.info/how-to-initialize-lxd-again/) ## Install lxdMosaic [link](https://github.com/turtle0x1/LxdMosaic) ```shell # Launch an ubuntu container lxc launch ubuntu: lxdMosaic # Connect to ubuntu console lxc exec lxdMosaic bash # Download the script curl https://raw.githubusercontent.com/turtle0x1/LxdMosaic/master/examples/install_with_clone.sh >> installLxdMosaic.sh # Then give the script execution permissions chmod +x installLxdMosaic.sh # Then execute the script ./installLxdMosaic.sh ``` ## Create zsf pool image file and add it to lxc ```shell # bs = blocksize, count = number of blocks # create the image file - 250GB dd if=/dev/zero of=/mnt/data1/overlook-zfs-pool02 bs=1GB count=250 # Create the loop device (check `df -h` first for available names) sudo losetup /dev/loop6 /mnt/data1/overlook-zfs-pool02 # Create zfs pool sudo zpool create overlook-zfs-pool02 /dev/loop6 # View existing zpool list zpool list # Add new zpool to lxc storage lxc storage create overlook-zfs-pool02 zfs source=overlook-zfs-pool02 ``` * [block sizes and multiples](https://www.linuxnix.com/what-you-should-know-about-linux-dd-command/) * [How to use a file as a zpool](https://serverfault.com/questions/583733/how-to-use-a-file-as-a-zpool) ## How to move containers to a new storage pool on the same host [link](https://discuss.linuxcontainers.org/t/how-to-move-containers-to-a-new-storage-pool-on-the-same-host/2798) ```shell lxc stop container_name lxc move container_name temp_container_name -s new_storage_pool lxc move temp_container_name container_name lxc start container_name ``` ## Changing existing containers to use the bridge profile Suppose we have an existing container that was created with the default profile, and got the LXD NAT network. Can we switch it to use the bridge profile? Here is the existing container. ```shell lxc launch ubuntu:x mycontainer Creating mycontainerStarting mycontainer ``` Let’s assign mycontainer to use the new profile "bridgeprofile". ```shell lxc profile assign mycontainer bridgeprofile ``` Now we just need to restart the networking in the container. ```shell lxc exec mycontainer -- systemctl restart networking.service ``` * [Change lxc profile for container](https://blog.simos.info/how-to-make-your-lxd-containers-get-ip-addresses-from-your-lan-using-a-bridge/) ## /etc/netplan/ for containers ```shell network: ethernets: eth0: addresses: - 192.168.0.206/24 gateway4: 192.168.0.1 nameservers: addresses: [ 1.1.1.1, 8.8.8.8 ] version: 2 ``` ## backup (export) containers to a file ```shell bdate=$(date +"%Y-%m-%d") && for ct in $(lxc list -c n --format csv); do lxc export $ct /mnt/data2/container-backup/$bdate-$ct.tgz; done ``` ## restore container from backup ``` lxc import /.tgz ```