| Title: How to boot on a BTRFS snapshot | |
| Author: Solène | |
| Date: 04 January 2023 | |
| Tags: linux gentoo btrfs | |
| Description: In this article, you will learn how to boot from BTRFS | |
| snapshots | |
| # 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. | |
| # 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: | |
| ```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: | |
| ```script | |
| $ 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. | |
| # 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. | |
| # 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. | |
| # 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. | |
| # 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 |