This guide aims to assist anyone interested in exploring the compression and deduplication features of Btrfs within Qubes OS, while also serving as my personal reference.
The description about btrfs and Qubes OS are deliberately incomplete and biased and only summarize the required aspect to achieve effective compression & deduplication effects. Refer to the real documentation if you have any question on it.
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.
Template VM store their root.img
and private.img
under /var/lib/qubes/vm-templates/<vm_name>/
; AppVM store their private.img
under /var/lib/qubes/appvms/<vm_name>/
.
Usually you do not only see root.img
, private.img
and some necessary config file, but other versions of corresponding images.
The files ending in Z
like private.img.23@2024-05-06T07:08:09Z
are revisions (that can be restored later, with qvm-volume revert
in standard ways, and mv
in non-standard ways 😃 ).
The files ending in -precache.img
are image file used in Qubes OS VM start at runtime. They are safe to remove when no VM is up, and should be removed if you manually edited the base image (for example when you changed private.img
, you should remove private-precache.img`).
In my case no version tracking is required, and I will remove both two types of files before compression and deduplication.
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”.
The step is important because:btrfs filesystem defragment
undo deduplication.More file cause duperemove
to take longer time on deduplication, and might cause a larger hash file that takes more space and hurts disk further.
Fill compression of filesystem. Now you are familiar to the compression steps. Compress the filesystem entirely with:
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 /
.
e4defrag
; further testing on this is pending: is a template requiring ext4 defragmentation? is ext4 defragmentation more friendly to btrfs 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