Skip to content

Instantly share code, notes, and snippets.

@linux4life798
Last active July 21, 2024 00:57
Show Gist options
  • Save linux4life798/72674b986b69cf66dfb125e715094f7d to your computer and use it in GitHub Desktop.
Save linux4life798/72674b986b69cf66dfb125e715094f7d to your computer and use it in GitHub Desktop.
Resources for BTRFS Setup and Management

BTRFS Notes

Packages

  • btrfs-progs
  • duperemove

Suggested BTRFS subvol as / layout for Snapper

https://wiki.archlinux.org/title/Snapper#Suggested_filesystem_layout

This allows for rolling the rootfs back to a snapshotted version, since the rootfs isn't tied to vol-id 5, anymore.

Debian installer uses /@rootfs as the subvol /, instead of @. See https://wiki.debian.org/Btrfs%20migration for more information.

When you need to make /@ level changes, you will still need to mount subvol-id 5 in /mnt.

sudo mount /dev/nvme0n1p3 /mnt

Resources

Modifications

  • Label the main/root btrfs filesystem root-btrfs and the boot filesystem boot-btrfs.
sudo chattr +C  /var/lib/libvirt/images
sudo chattr +C ~/.local/share/libvirt/images
sudo chattr +C ~/.local/share/gnome-boxes/images

Swap

sudo mount /dev/disk/by-label/root-btrfs -osubvolid=5 /mnt
sudo btrfs subvolume create /mnt/@swap
sudo chmod u=rwx,g=,o= /mnt/@swap
sudo umount /mnt
sudo mkdir /var/swap
sudo chmod u=rwx,g=,o= /var/swap

# Edit /etc/fstab to mount @swap to /var/swap.
sudo systemctl daemon-reload
sudo mount /var/swap

sudo btrfs -v filesystem mkswapfile -s 8g /var/swap/swapfile1
sudo ls -al /var/swap

# Edit /etc/fstab and add the following line:
# /var/swap/swapfile1 none swap defaults 0 0
sudo swapon -a
sudo swapon

# Example mount line for the swap subvol:
# sudo mount /dev/disk/by-label/root-btrfs -osubvol=@swap /var/swap

Add Mirrored Drive

sudo btrfs device add -f /dev/mapper/root2_crypt /
sudo btrfs balance start -dconvert=raid1 -mconvert=raid1 /

Maintenance

sudo btrfs scrub start /
sudo btrfs scrub status /
sudo btrfs filesystem defragment -r /

sudo duperemove -rdh /
# Or, you can speed things up with a hashfile, which will then
# use the save hash if the timestamp matches.
sudo duperemove  -rdh -A --hashfile=/duperemove_hashfile /

The -A allow it to work over readonly snapshots.

Warning: Running duperemove cannot effectively remege/resnapshot two unrelated subvolumes/snapshots. I did the following experiment, to test this. Restore root and home from a backup, manually using rsync. I then used btrfs send/receive to send the same backup as a subvolume to the machine being restored. I ran duperemove many times, but the most storage space I could recover/merge was about 200GB of 800GB. I still had a massive 600GB of undedupeable additional storage being consumed by this subvol that contains identical data to my main subvols.

Interesting BTRFS Related Packages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment