About me: My name is Solène Rapenne, pronouns she/her. I like learning and sharing knowledge. Hobbies: '(BSD OpenBSD Qubes OS Lisp cmdline gaming security QubesOS internet-stuff). I love percent and lambda characters. OpenBSD developer solene@. No AI is involved in this blog.

Contact me: solene at dataswamp dot org or @solene@bsd.network (mastodon).

You can sponsor my work financially if you want to help me writing this blog and contributing to Free Software as my daily job.

How to boot on a BTRFS snapshot

Written by Solène, on 04 January 2023.
Tags: #linux #gentoo #btrfs

Comments on Fediverse/Mastodon

1. Introduction §

I always wanted to have a simple rollback method on Linux systems, NixOS gave me a full featured one, but it wasn't easy to find a solution for other distributions.

Fortunately, with BTRFS, it's really simple thanks to snapshots being mountable volumes.

2. Setup §

You need a Linux system with a BTRFS filesystem, in my examples, the root subvolume (where / is) is named gentoo.

I use btrbk to make snapshots of / directly in /.snapshots, using the following configuration file:

snapshot_preserve_min   30d
volume /
  snapshot_dir .snapshots
    subvolume .

With a systemd service, it's running once a day, so I'll have for 30 days of snapshots to restore my system if needed.

This creates snapshots named like the following:

$ ls /.snapshots/
ROOT.20230102
ROOT.20230103
ROOT.20230104

A snapshot address from BTRFS point of view looks like gentoo/.snapshots/ROOT.20230102.

I like btrbk because it's easy to use and configure, and it creates easy to remember snapshots names.

3. Booting on a snapshot §

When you are in the bootloader (GRUB, systemd-boot, Lilo etc..), edit the command line, and add the new option (replace if already exists) with the following, the example uses the snapshot ROOT.20230102:

rootflags=subvol=gentoo/.snapshots/ROOT.20230103

Boot with the new command line, and you should be on your snapshot as the root filesystem.

4. Be careful §

When you are on a snapshot, this mean any change will be specific to this volume.

If you use a separate partition for /boot, an older snapshot may not have the kernel (or its module) you are trying to boot.

5. Conclusion §

This is a very simple but effective mecanism, more than enough to recover from a bad upgrade, especially when you need the computer right now.

6. Going further §

There is a project grub-btrfs which can help you adding BTRFS snapshots as boot choices in GRUB menus.

grub-btrfs GitHub project page