====== A "perfect" btrfs server ====== This is nearly the title of a fine [[https://seravo.fi/2016/perfect-btrfs-setup-for-a-server|article]] (signed by Mr Technology...) and my notes of its implementation. It is tested only on legacy boots, and is known not to work as is on some UEFI boots. The interest of btrfs is big, and many good articles explain it, but I have not found good articles on its implementation on debian. The server is based on a debian with 2 RAID1 2T disks. ===== Install basic debian ===== Let us start to install the system from debian-9.1.0-amd64-netinst.iso (on a flash USB key in this case). I skip here standard install procedures but,on the partition menu, create a main btrfs partition on the whole first disk, add the boot flag, mounted on /. On the second disk create a partition as on the 1rst disk but declare it of type "not in use",but with the boot flag. Do not declare any other partition. Finish the installation normally. As is, my system refused to boot, but with the USB key mounted, the system boots... And I had to : grub-install /dev/sdX1 apt install --reinstall grub-pc and then it boots. We have now a basic server with btrfs file system. ===== Build the RAID1 ===== We note /dev/sda and /dev/sdb the two disks, but it may differ on other systems. If not soon done, add the boot flag //now// with fdisk to the second disk. Then add the 2nd disk to / and build the RAID: apt update apt install btrfs-tools btrfs device add /dev/sdb1 / btrfs balance start -v -mconvert=raid1 -dconvert=raid1 / You can use btrfs display commands to see the sitation all along : btrfs fi show sudo btrfs fi usage / ===== Various inits ===== I suggest to modify les logs dans /etc/systemd/journald.conf : Storage=persistent See : https://guillaume.fenollar.fr/blog/journald-tutoriel-journald-journalctl/ ==== Add zram based swap ==== Ubuntu policy is to assign half of the memory to swap and divide it by the the number of CPU hearts. apt install zram-tools You should modify ALLOCATION to 33-50% or total RAM in /etc/default/zramswap ===== Install lxd ===== The interest of "containers" is great to isolate problems. I have choosen [[https://linuxcontainers.org/lxd/|LXD]]. To say it simply, LXD is magic... Thanks to the developper team. ==== Prepare the bridge ==== Create a simple bridge : apt install bridge-utils In /etc/network interfaces, //here// based on enp5s0 (not //wan//) interface : # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto enps5s0 iface enp5s0 inet manual auto br0 iface br0 inet static address 192.168.163.1 netmask 255.255.255.0 gateway 192.168.163.254 network 192.168.163.0 broadcast 192.168.163.255 bridge_ports enp5s0 bridge_stp on bridge_maxwait 0 iface enp5s0 inet6 auto ==== Install lxd from snap ==== apt install snapd snap install lxd Logout, then log in again, then init lxd. ==== Initialize LXD ==== Default values are usually good (btrfs...), but //do not declare any network// but add br0 "manually" after, then add your user to lxd group : lxd init [...] lxc network attach-profile br0 default eth0 adduser myuser lxd Now myuser can manipulate containers : lxc launch images:debian/stretch debian lxc config device override debian eth0 hwaddr=02:1E:62:00:00:36 (recommended) lxc config set debian volatile.eth0.hwaddr 00:1e:62:00:00:43 (not recommended) lxc start debian lxc exec debian bash ... It is possible to choose to work as root in the containers as they are isolated from the host. ==== Containers LXD ==== === Backup === lxc export pgsql /media/lxd_containers/pgsql2020_01_12.tar.gz After import, the container is on stopped state : lxc import /media/lxd_containers/pgsql2020_01_12.tar.gz === Use simple mail in server or containers === == nullmailler == Bien == msmtp == apt install msmtp msmtp-mta Et /etc/msmtprc : account default host 192.168.13.36 auto_from off from container_name@couderc.eu syslog LOG_MAIL == Data access == /var/snap/lxd/common/mntns/var/snap/lxd/common/lxd/storage-pools/default/containers/ ===== Maintenance ===== In case of failure, prevent the problem by having a 3rd empty disk ready... To replace : btrfs fi show btrfs replace start -rf /dev/sdb2 /dev/sda2 /