Files
wiki/boot-from-snapshot.md

69 lines
2.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Restoring Root Filesystem from BtrFS Snapshot
description: Undo your grave mistakes quickly and efficiently. Yay!
published: true
date: 2022-04-30T20:10:46.302Z
tags: btrfs, recovery, rootfs
editor: markdown
dateCreated: 2021-10-20T05:15:59.558Z
---
If you frigged up *really* bad, you likely cannot read this, which is ironic.
However, I'll write it down so it hopefully sinks in the ol' noggin.
# Booting into snapshots
I configured the system to use `snapper`, a tool from OpenSUSE, which takes and manages BtrFS snapshots. Thanks to the `grub-btrfs` plugin, these root filesystem snapshots are accesable from the GRUB2 boot menu.
First, select whichever snapshot (they are neatly dated) by following the `Gentoo snapshots` option in the boot menu.
# Making snapshots writable
By default, snapshots are read only. They should still boot neatly, but lots of things will not work (like NTP and most compiles).
Take a good look around - do you like this snapshot? is this a nice snapshot? would you like to settle down with this snapshot?
If so, run:
`btrfs property set -ts / ro false`
when `/` is the path the snapshot is currently mounted at (use `findmnt -nt btrfs` to find out).
> Once a snapshot is writeable, consider it compromised - booting from it again if you change your mind may not be as smooth!
{.is-warning}
# Setting snapshot as new root
First, find the snapshot ID by runnnig `findmnt -nt btrfs` - which will give you something like this:
```
root@Takahe] ~ (takahe-root)
findmnt -nt btrfs
/ /dev/nvme0n1p2[/.snapshots/232/snapshot] btrfs rw,noatime,compress=zstd:11,ssd,d
```
Now, `grep` the path in the square brackets from the output of `btrfs snapshots list /`:
```
[root@Takahe] ~ (takahe-root)
btrfs subvolume list / | grep .snapshots/232/snapshot
ID 8624 gen 2199024063871 top level 2740 path .snapshots/232/snapshot
```
Noticed how I removed the leading `/`.
Now, take the `ID` of the snapshot, and set it as the new default:
```
[root@Takahe] ~ (takahe-root)
btrfs subvolume set-default 8624 /
```
> The BtrFS default is built into the filesystem - it overrides the value on `/etc/fstab`.
{.is-info}
Finally, update the bootloader to enable the default entry to boot into your snapshot:
`grub-mkconfig -o /boot/grub/grub.cfg`
This should be it - but you can double check if you're really keen by reading that file and looking for something like this in the kernel parameters:
```
linux /vmlinuz-5.14.10-gentoo-x86_64 root=UUID=d9699f1e-6825-4b38-9e3a-c228763b07cc ro rootflags=subvol=.snapshots/232/snapshot
```
See the `rootflags`? that's us, baby.
Happy compiling!