JMM’s

btrfs notes

How do I delete a snapshot, again?

Mounting

Mount subvolume

Here’s how to mount a subvolume. You may need to enable compression at mount time, as (I think) compression isn’t declared when you create a subvolume.

mount -t btrfs -o subvol=some/subvol,compress=lzo /dev/mapper/somedisk /mnt/point

Mount snapshot

mount -t btrfs -o subvol=snapshots/home-2023-11-10-100011 /dev/mapper/somedisk ~/mntpoint

Assuming the snapshot is read-only, it seems like you don’t need to add “ro” to the “-o” options.

Create snapshot

Here’s how you’d create a snapshot if, say, /home/ was a btrfs subvolume:

btrfs subvolume snapshot /mnt/home /mnt/snapshots/home-20231217

You’d probably want to make it read-only, though. You’d just add the “-r” flag like so:

btrfs subvolume snapshot -r /mnt/home /mnt/snapshots/home-20231217

Delete snapshot

Deleting a snapshot is the same as deleting a subvolume:

btrfs subvolume delete /some/snapshot-2023-11-12

Restoring a directory from a snapshot

On 2023-12-17, I somehow f’d up a Git index. I decided I basically needed to recover the entire git repository.

rm -rf /home/user/some-repo
cp -a --reflink=always /mnt/snapshots/home-20231217/user/some-repo /home/user/some-repo

The “-a” preserves attributes/ownership. The “--reflink=always” hardlinks, and will error out if this isn’t done.

Misc commands

Show a list of subvolumes
btrfs subvolume list /
Use a path for where the filesystem is mounted.
Get info about a subvolume
btrfs subvolume show ~/mnt/some-subvol

Issues

Some problems I’ve experienced

High CPU load with btrfs-cleaner or btrfs-transaction

Sometimes after I delete a bunch of old snapshots, my disk with lock up and it’ll take a while for some programs to load. What I notice is that btrfs-cleaner or btrfs-transaction can take a lot of CPU (just by looking at top). This can get especially annoying when I’m trying to do some actual work.

I need to figure out a way to either delay the cleaner or perform all cleaning/defragmenting at once. I notice that the cleaner doesn’t just keep going, but actually starts up again when I either try to write or read something. Also, I was trying to see if it’s part of a cgroup (so I could adjust its allowed disk bandwidth with systemd), but it seems to just be part of the root cgroup.

Some reading suggests disabling btrfs quotas (see btrfs-cleaner high CPU utilization and severe performance issues | Support | SUSE and 119541 – btrfs-cleaner causes processes to hang for a long time)

You can show quotas with:

btrfs qgroup show /mnt/somefilesystem

And you can disable quotas with:

btrfs quota disable /mnt/somefilesystem

After that, trying to show quotas will return an error. I’ve just tried this (on 2024-02-02), so hopefully this’ll help.