User:S0x9v/Installation/Root on btrfs layout

From ArchWiki

Create subvolumes

Some explanation of the following layout

Containers

Subvolume Mountpoint Notes
Filesystem layout
@var no mountpoint
@usr no mountpoint

Root filesystem

Filesystem layout
Subvolume Mountpoint Notes
@/0/snapshot /
@/ /.snapshots
@home /home
@root /root
@srv /srv
@usr/local /usr/local
@swap /swap nodatacow, for swapfile
@var/{tmp,spool,log,cache,games,www,lib,lib/libvirt,lib/docker,lib/AccountsService,lib/NetworkManager} ... Install_Arch_Linux_on_ZFS#System_datasets
@var/mail /var/mail doesn't work with pacstrap

Commands

cd $MOUNTPOINT

# create system subvolume 
btrfs subvolume create @
mkdir @/0
btrfs subvolume create @/0/snapshot

# exclude these dirs from system snapshot
for i in {home,root,srv,usr,usr/local,swap,var};
do btrfs subvolume create @$i;
done

# exclude these dirs under /var from system snapshot
for i in {tmp,spool,log,cache,games,www,lib,lib/libvirt,lib/docker,lib/AccountsService,lib/NetworkManager};
do btrfs subvolume create @var/$i;
done

Mount subvolumes

# mount system subvolume and snapper snapshot subvolume, create mountpoints
cd ~
umount $MOUNTPOINT
mount $mapper_path $MOUNTPOINT -o subvol=/$DISTRO/@/0/snapshot,compress-force=zstd,noatime,space_cache=v2
mkdir -p $MOUNTPOINT/{.snapshots,home,root,srv,tmp,usr/local,swap}
mkdir -p $MOUNTPOINT/var/{tmp,spool,log,cache,games,www,lib/libvirt,lib/docker,lib,lib/AccountsService,lib/NetworkManager}
mount $mapper_path $MOUNTPOINT/.snapshots/ -o subvol=$DISTRO/@,compress-force=zstd,noatime,space_cache=v2

# mount subvolumes
for i in {home,root,srv,swap,usr/local};
do mount $mapper_path $MOUNTPOINT/$i -o subvol=$DISTRO/@$i,compress-force=zstd,noatime,space_cache=v2;
done
# don't mount /var itself, only selected subfolders
for i in {tmp,spool,log,cache,games,www,lib/libvirt,lib/docker,lib/AccountsService,lib/NetworkManager};
do mount $mapper_path $MOUNTPOINT/var/$i -o subvol=$DISTRO/@var/$i,compress-force=zstd,noatime,space_cache=v2;
done

Disable Copy-on-Write on directories

for i in {swap,var/lib/libvirt};
do chattr +C $MOUNTPOINT/$i;
done