Skip to content

Instantly share code, notes, and snippets.

@major0
Last active April 2, 2025 11:22
Show Gist options
  • Select an option

  • Save major0/a635f14d116d3125d2007e5aa7fb712a to your computer and use it in GitHub Desktop.

Select an option

Save major0/a635f14d116d3125d2007e5aa7fb712a to your computer and use it in GitHub Desktop.
BTRFS Root Snapshot support on Ubuntu
# This is a copy of my /etc/fstab for my Desktop workstation. The goal
# of this design is to allow easy rollbacks of the rootfs via btrfs
# snapshots which are created any time apt modifies the system.
##
# The default rootfs volume needs some special attention in order to "default"
# correctly while also allowing grub-btrfs to select a new snapshot for the
# rootfs:
# - use btrfs subvol set-default @ instead of specifying the rootfs volume on the kernel CLI.
# - modify /etc/grub.d/[12]0_linux* and remove references to the `rootsubvol`
# from GRUB_CMDLINE_LINUX
UUID=f9dc9ccb-941c-4b87-a1c1-5ad898c064cc / btrfs defaults,noatime,compress=zstd,space_cache=v2 0 1
##
# Mount our global pool (subvolid=5) on `/btr` for easy management; similar to
# old-school `/afs`, `/dfs` mounts. This allows for easy browsing of all btrfs
# subvolumes by simply doing an `ls` into `/btr. This also allows for easy
# copying of `/a/path/in/root/.` to `/btr/@some-subvol/` w/out relying on random
# mv/rename trickery of the source while converting the fileystem.
#
# Note: this is _always_ subvolid 5
UUID=f9dc9ccb-941c-4b87-a1c1-5ad898c064cc /btr btrfs defaults,subvolid=5,noatime,compress=zstd,space_cache=v2 0 2
##
# Split out subvolumes such that we isolate rootfs changes. The goal is to
# make certain that all snapshots of the rootfs reflect "system configuration"
# changes, and do not include random changes related to the running state, or
# logging, or some other random application cache/temp data. We want the
# rootfs snapshots to only include changes to the rootfs config. E.g. package
# changes, package configs, etc.
#
# Notes:
# - when backing up old paths use `cp -a --reflink=never` to prevent `cp` from
# automagically making a CoW copy on us.
# - mv /var/mail into /var/spool/mail and symlink /var/mail to point at it.
# - /var/{run,lock} are already symlinks pointing into `tmpfs` mounts in the
# rootfs setup by initramfs
# - enable smart monitoring.
# See: https://edafe.de/2024/01/monitoring-storage-devices-with-smartmontools-on-debian-or-ubuntu/
# - install btrfsd (apt install btrfsd)
# - install postfix, even for local-only delivery and set the `root` mail alias
# to forward to the primary admin users.
# - install grub-btrfs: https://github.com/Antynea/grub-btrfs
# - consider installing bees (write-behind block deduplication).
# See: https://github.com/Zygo/bees
UUID=f9dc9ccb-941c-4b87-a1c1-5ad898c064cc /home btrfs defaults,subvol=@home,noatime,compress=zstd,space_cache=v2 0 2
UUID=f9dc9ccb-941c-4b87-a1c1-5ad898c064cc /opt btrfs defaults,subvol=@opt,noatime,compress=zstd,space_cache=v2 0 2
UUID=f9dc9ccb-941c-4b87-a1c1-5ad898c064cc /root btrfs defaults,subvol=@root,noatime,compress=zstd,space_cache=v2 0 2
UUID=f9dc9ccb-941c-4b87-a1c1-5ad898c064cc /srv btrfs defaults,subvol=@srv,noatime,compress=zstd,space_cache=v2 0 2
UUID=f9dc9ccb-941c-4b87-a1c1-5ad898c064cc /tmp btrfs defaults,subvol=@tmp,noatime,compress=zstd,space_cache=v2 0 2
##
# Consider disabling CoW on the following `/var` subvolumes.
# E.g. `chattr +C /var/<path>`
UUID=f9dc9ccb-941c-4b87-a1c1-5ad898c064cc /var/cache btrfs defaults,subvol=@var-cache,noatime,compress=zstd,space_cache=v2 0 2
UUID=f9dc9ccb-941c-4b87-a1c1-5ad898c064cc /var/crash btrfs defaults,subvol=@var-crash,noatime,compress=zstd,space_cache=v2 0 2
UUID=f9dc9ccb-941c-4b87-a1c1-5ad898c064cc /var/log btrfs defaults,subvol=@var-log,noatime,compress=zstd,space_cache=v2 0 2
UUID=f9dc9ccb-941c-4b87-a1c1-5ad898c064cc /var/opt btrfs defaults,subvol=@var-opt,noatime,compress=zstd,space_cache=v2 0 2
UUID=f9dc9ccb-941c-4b87-a1c1-5ad898c064cc /var/spool btrfs defaults,subvol=@var-spool,noatime,compress=zstd,space_cache=v2 0 2
UUID=f9dc9ccb-941c-4b87-a1c1-5ad898c064cc /var/tmp btrfs defaults,subvol=@var-tmp,noatime,compress=zstd,space_cache=v2 0 2
##
# Docker has special needs for the btrfs storage driver.
# See: https://docs.docker.com/engine/storage/drivers/btrfs-driver/
UUID=f9dc9ccb-941c-4b87-a1c1-5ad898c064cc /var/lib/docker btrfs defaults,subvol=@var-lib-docker,noatime,compress=zstd,space_cache=v2 0 2
##
# Flatpak isn't special, it just has a really long path name.
# Also a candidate for `chattr +C /var/lib/flatpak`
UUID=f9dc9ccb-941c-4b87-a1c1-5ad898c064cc /var/lib/flatpak btrfs defaults,subvol=@var-lib-flatpak,noatime,compress=zstd,space_cache=v2 0 2
##
# Snapper automatically creates this subvolume when run for the first time.
# `snapper -c root create-config /`
UUID=f9dc9ccb-941c-4b87-a1c1-5ad898c064cc /.snapshots btrfs defaults,subvol=@/.snapshots,noatime,compress=zstd,space_cache=v2 0 2
##
# The EFI mount uses softraid w/ v0.90 metadata, so there is no UUID.
# This is the only softraid option that is compatible with keeping EFI
# partitions in-sync.
#
# mdadm.conf example:
# ARRAY /dev/md/efi metadata=0.90 UUID=9003d76b:bf3a2484:61ab8d08:140db6f9 devices=/dev/nvme0n1p1,/dev/nvme1n1p1,/dev/sda1,/dev/sdb1
/dev/md/efi /boot/efi vfat umask=0077 0 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment