This guide aims to assist anyone interested in exploring the compression and deduplication features of Btrfs within Qubes OS, while also serving as a personal reference.
Please ensure you back up all data before experimenting.
Btrfs, despite its numerous features, is more akin to ext4 than LVM. It organizes files and directories in a tree structure, rooted at /, and can be mounted almost anywhere.
Compression and deduplication in Btrfs can be enabled at runtime, rather than during filesystem creation.
Btrfs supports compression, with zstd being the most efficient method. By default, compression is disabled and must be activated via a mount flag.
For offline deduplication, Btrfs does not require a mount flag. Instead, a userspace tool (such as duperemove
) is necessary to identify duplicate blocks across files using sophisticated hashing algorithms and data structures. This tool then informs the Btrfs module of any duplicates, which, after verification, may be deduplicated, potentially reducing disk usage.
(Primarily based on my experiences with R4.2)
Qubes OS's default partition setup with Btrfs includes:
/boot/efi
, vfat)/boot
, ext4)/
, luks+btrfs)Dom0 utilizes the entire Btrfs partition.
VM images (root.img, private.img) are stored as regular, albeit typically large, files under /var/lib/qubes and are in raw format.
Unlike with LVM, where dom0 root and VM images are separate volumes, here they are indistinguishable from other regular files in dom0. This distinction makes compression and deduplication in Qubes OS using Btrfs largely similar to their use in other OSes.
compress=zstd
or compress-force=zstd
(the former is recommended; if unsatisfied, you can later use chattr -m file; chattr +c file
to force compression on specific files), then reboot.
Note:sudo qubes-dom0-update
, followed by sudo qubes-dom0-update compsize duperemove
to install utilities for deduplication (with duperemove) and to assess the effectiveness of compression and deduplication (with compsize).sudo btrfs filesystem defragment -v -r -czstd /usr /etc /srv /var/lib/dnf /var/log /var/lib/qubes/vm-kernels
We deliberately excludes VM images for now due to their size.
sudo lsattr <path>
. A file marked with the “m” flag indicates it was deemed incompressible after the initial compression attempt.sudo chattr -m <path> && sudo chattr +c <path>
.btrfs filesystem defragment
with specific directorysudo compsize -x /
, which reveals the actual disk usage, including both compressed and uncompressed portions, and indicates any deduplication achieved./var/lib/qubes/{appvms,vm-templates}/*/*{Z,-precache.img}
. Usually the only large files there should be “root.img” and “private.img”.sudo btrfs filesystem defragment -v -r -czstd /
If VM images remain uncompressed, force their compression as step 5 says, and repeat the process. 9. Deduplication. Proceed with deduplication:
sudo duperemove -r -d -v --hashfile=/var/duperemove.hashfile --dedupe-options=same,partial --exclude=/var/duperemove.hashfile /
Note:
-A
option as it does not work.same
option is crucial, while partial
is more time-consuming but can enhance deduplication.The hashfile acts as a cache, useful for subsequent deduplications.
Review deduplication results with sudo compsize -x /
or sudo btrfs filesystem df /
.
After rebooting, install and update new templates, then deduplicate again (compression is automatic).
Consider defragmenting your template VM roots with e4defrag
; further testing on this is pending.
Finally, restore your backups and perform another round of deduplication.
This guide should be updated later with more experiment.
https://forum.qubes-os.org/t/about-btrfs-with-compression-in-qubes-os/19707 https://forum.qubes-os.org/t/pool-level-deduplication/12654 https://btrfs.readthedocs.io/en/latest/Introduction.html https://wiki.archlinux.org/title/Btrfs