Reclaiming Disk Space from Unused Snapshot Copies on Btrfs Filesystems

Introduction to Btrfs Snapshots

I’ve worked with Btrfs for a while now, and one of its most useful features is snapshotting. This allows you to create a read-only copy of a subvolume at a given point in time, which is perfect for backups, testing, and rolling back changes. However, over time, these snapshot copies can accumulate and consume significant disk space. In practice, this can become a real issue if you’re not careful.

Understanding Btrfs Snapshots

Before we dive into reclaiming disk space, let’s take a look at how Btrfs snapshots work. A snapshot is essentially a read-only copy of a subvolume, which is a self-contained filesystem within the Btrfs filesystem. To create a snapshot, you can use the btrfs subvolume snapshot command. For example:

btrfs subvolume snapshot /mnt/btrfs/subvol /mnt/btrfs/snapshot

This command creates a snapshot of the subvol subvolume and stores it in the snapshot subvolume. Don’t bother with trying to create snapshots manually all the time, though - it’s better to automate the process.

Identifying Unused Snapshots

To reclaim disk space, we need to identify unused snapshots. The real trick is figuring out which snapshots are still useful and which ones can be safely deleted. We can use the btrfs subvolume list command to list all subvolumes, including snapshots:

btrfs subvolume list /mnt/btrfs

This command will output a list of subvolumes, including their IDs, generation numbers, and parent IDs. I usually start with this command to get an idea of what’s going on with my snapshots.

Deleting Unused Snapshots

Once we have identified unused snapshots, we can delete them using the btrfs subvolume delete command:

btrfs subvolume delete /mnt/btrfs/snapshot

This command will delete the snapshot subvolume and all its contents. Be careful with this command, though - once you delete a snapshot, it’s gone for good.

Reclaiming Disk Space

After deleting unused snapshots, we need to reclaim the disk space they occupied. We can use the btrfs balance command to rebalance the filesystem and reclaim disk space:

btrfs balance start /mnt/btrfs

This command will rebalance the filesystem and reclaim disk space from deleted snapshots. In my experience, this can take a while to complete, so be patient.

Automating Snapshot Management

To avoid accumulating unused snapshots in the future, we can automate snapshot management using tools like snapper. Snapper is a tool that allows you to manage Btrfs snapshots and configure automatic snapshot creation and deletion. You can install snapper on most Linux distributions, including openSUSE and Arch Linux. This is where people usually get burned - they don’t set up automation and end up with a huge mess of snapshots.

Security Considerations

When managing Btrfs snapshots, we should consider security implications. Snapshots can contain sensitive data, so we should ensure that they are properly secured. We can use tools like btrfs subvolume set-default to set the default subvolume to a secure location, and btrfs subvolume snapshot -r to create read-only snapshots. I’ve seen this go wrong when people don’t take the time to secure their snapshots - it’s not worth the risk.

Troubleshooting

If we encounter issues while managing Btrfs snapshots, we can use the btrfs command with the --help option to get more information:

btrfs --help

We can also check the Btrfs wiki for more information on troubleshooting and best practices.

Best Practices

To get the most out of Btrfs snapshots, we should follow best practices like regularly cleaning up unused snapshots, using tools like snapper to automate snapshot management, ensuring that snapshots are properly secured, and monitoring disk space usage to avoid running out of space.


See also