# Setting up Btrfs snapshots in Fedora 40
:::danger
Attention!!!
I'm not a computer expert; I'm just a newbie exploring various technologies.
This article is a summary of the information I've gathered from various online sources.
Please don't think of this as a "tutorial." It's just a "sharing post" about how I managed to get these two things working on my system. I can't guarantee it will work on your computer, and all consequences are the user's responsibility.
I might not have the ability to answer all questions. I wish I could, but as I mentioned earlier, I'm a newbie. Sorry for that tho.
:::
## Introduction
<img src="https://hackmd.io/_uploads/rJcXCb2LR.png"
alt="image"
width="411"
height="326"
style="display: block; margin: 0 auto" />
The snapshot feature of Btrfs provides system administrators and users with a flexible and efficient way to protect, manage, and restore data. This technology not only saves storage space but also enhances system stability and availability.
Btrfs Assistant is a GUI management tool to make managing a Btrfs file system easier, while Grub-Btrfs improves the GRUB bootloader by adding a Btrfs snapshots sub-menu, allowing the user to boot into snapshots.
This is a sharing post about how I managed to get Btrfs Assistant and Grub-Btrfs successfully running on my ***Fedora Linux 40 (Workstation Edition) with GNOME Version 46.2 under the X11 window system***. Of course, ***the file system is Btrfs***.
## What is Btrfs Assistant?
[Btrfs Assistant](https://gitlab.com/btrfs-assistant/btrfs-assistant) is a GUI management tool to make managing a Btrfs filesystem easier.
The primary features it offers are:
* An easy to read overview of Btrfs metadata
* A simple view of subvolumes with or without Snapper/Timeshift snapshots
* Run and monitor scrub and balance operations
* A pushbutton method for removing subvolumes
* A management front-end for Snapper with enhanced restore functionality
* View, create and delete snapshots
* Restore snapshots in a variety of situations
* When the filesystem is mounted in a different distro
* When booted off a snapshot
* From a live ISO
* View, create, edit, remove Snapper configurations
* Browse snapshots and restore individual files
* Browse diffs of a single file across snapshot versions
* Manage Snapper systemd units
* A front-end for Btrfs Maintenance
* Manage systemd units
* Easily manage configuration for defrag, balance and scrub settings

## What is Grub-Btrfs?
[Grub-Btrfs](https://github.com/Antynea/grub-btrfs) improves the GRUB bootloader by adding a btrfs snapshots sub-menu, allowing the user to boot into snapshots. It supports manual snapshots as well as snapper, timeshift, and yabsnap created snapshots.
:::warning
This might be helpful when something really bad happens and we can't boot into the system.
:::
The primary features it offers are:
* Automatically lists snapshots existing on the btrfs root partition.
* Automatically detect if `/boot` is in a separate partition.
* Automatically detect kernel, initramfs and Intel/AMD microcode in `/boot` directory within snapshots.
* Automatically create corresponding menu entries in `grub.cfg`
* Automatically detect the type/tags/triggers and descriptions/comments of Snapper/Timeshift/Yabsnap snapshots.
* Automatically generate `grub.cfg` if you use the provided Systemd/ OpenRC service.

## Set up Btrfs Assistant
### Step 1
Btrfs Assistant is available in the Fedora repos as `btrfs-assistant`.
To download it, run:
``` bash
sudo dnf install btrfs-assistant
```
### Step 2
Open Btrfs Assistant.
In the **Snapper Settings** tab, you need to first create a brand new config file.
In my case, I'm going to create snapshots for root only.
Click on **New Config**, then follow the settings shown below.

After that, click on **Apply systemd changes**, then click **Save**.
Now you will see the following screen.

### Step 3
Under the **Snapper Settings** tab, we are going to set the values in **Snapshot Retention**.
Below is the [suggested values](https://github.com/kdave/btrfsmaintenance?tab=readme-ov-file#regularenterprise-distro) I chose to use. With **Enable timeline snapshots** checked, it will automatically create a snapshot for you every hour.

For other use cases, please refer [here](https://github.com/kdave/btrfsmaintenance?tab=readme-ov-file#summary).
### Step 4
Under the **Btrfs maintenance** tab, the [suggested settings](https://github.com/kdave/btrfsmaintenance?tab=readme-ov-file#tasks) are shown below. Don't forget to click **Apply** after changing the settings.

### Step 5 (optional)
To get snapshots automatically created before and after every `dnf` command, you need a package called `python3-dnf-plugin-snapper`.
To install it, run:
``` bash
sudo dnf install python3-dnf-plugin-snapper
```
Here is an example: As you can see, after executing `sudo dnf update`, it automatically created two snapshots. The type `pre` is the snapshot before `sudo dnf update`, and the type `post` is the snapshot after `sudo dnf update`.

Here is another example: After executing `sudo dnf remove htop`, two snapshots are automatically created.

### How to use Btrfs Assistant
Now that you have finished setting up Btrfs Assistant.
If you want to learn how to use other features of Btrfs Assistant that weren't mentioned in here, please refer to [The wiki for Btrfs Assistant](https://gitlab.com/btrfs-assistant/wiki).
## Set up Grub-Btrfs
### Step 1
We can't install Grub-Btrfs because it is not in the Fedora repos. Instead, we should clone the project from GitHub via the command below first.
``` bash
git clone https://github.com/Antynea/grub-btrfs.git
```
After that, run:
``` bash
cd grub-btrfs/
```
### Step 2
Now, to edit the `config` file, run:
``` bash
nano config
```
:::danger
There are quite a few areas that need editing in this section, so please double-check to ensure all changes are correct and error-free.
:::
In the `config` file:
***UNCOMMENT & MODIFY*** the line below
``` bash
#GRUB_BTRFS_SNAPSHOT_KERNEL_PARAMETERS="rd.live.overlay.overlayfs=1"
```
into
``` bash
GRUB_BTRFS_SNAPSHOT_KERNEL_PARAMETERS="systemd.volatile=state"
```
***UNCOMMENT*** the line below
``` bash
#GRUB_BTRFS_GRUB_DIRNAME="/boot/grub2"
```
into
``` bash
GRUB_BTRFS_GRUB_DIRNAME="/boot/grub2"
```
***UNCOMMENT & MODIFY*** the line below
``` bash
#GRUB_BTRFS_MKCONFIG=/usr/bin/grub2-mkconfig
```
into
``` bash
GRUB_BTRFS_MKCONFIG=/sbin/grub2-mkconfig
```
***UNCOMMENT*** the line below
``` bash
#GRUB_BTRFS_SCRIPT_CHECK=grub2-script-check
```
into
``` bash
GRUB_BTRFS_SCRIPT_CHECK=grub2-script-check
```
Now save and exit by pressing `Ctrl O`, `Enter` and `Ctrl X` on your keyboard in sequence.
### Step 3
To install Grub-Btrfs, run:
``` bash
sudo make install
```
### Step 4
To regenerate GRUB config files so that the changes you just made are added to grub.cfg file, run:
``` bash
sudo grub2-mkconfig -o /boot/grub2/grub.cfg
```
### Step 5
`grub-btrfsd` is a daemon that watches the snapshot directory for you and updates the GRUB menu automatically every time a snapshot is created or deleted.
To start the daemon, run:
``` bash
sudo systemctl start grub-btrfsd
```
To activate it during system startup, run:
``` bash
sudo systemctl enable --now grub-btrfsd
```
To see the current status of `grub-btrfsd` daemon, run:
``` bash
sudo systemctl status grub-btrfsd.service
```
If it shows **Active: <font color="#79FF79">active (running)</font>** then the daemon has started successfully.

:::danger
If it shows **Active: <font color="#F00">failed</font>** it is probably because the package `inotify-tools` isn't installed.
:::
To install the package, run:
``` bash
sudo dnf install inotify-tools
```
Restart `grub-btrfsd` after installation progress.
``` bash
sudo systemctl restart grub-btrfsd
```
Check the current status of `grub-btrfsd` daemon again.
``` bash
sudo systemctl status grub-btrfsd.service
```
:::success
The problem should be solved now!
:::
### Step 6
After completing the above steps, you can now reboot.
To reboot, run:
``` bash
sync; sync; sync; systemctl reboot
```
Upon entering Grub, you will find a new entry called "Fedora Linux snapshots." Press `Enter` to access it.

In here, all snapshots will be displayed. You can select the snapshot you want by pressing `Enter`.

Then choose the kernel, and the system will boot accordingly.

Once booted, if you wish to restore the system to the selected snapshot, you can use the mouse to click on the desired snapshot under the **Browse/Restore** tab within the **Snapper** tab of Btrfs Assistant. Then click **Restore** to revert the system to its state at that time. Remember to reboot the system afterwards.

### Step 6.1 (Sol for TPM error)
Solution for TPM error.
:::danger
When you select the snapshot you want by pressing `Enter` in the submenu of GRUB, and you encountered the **TPM error**, you will be unable to boot into your snapshots.
:::
:::warning
While disabling TPM 2.0 in the BIOS can fix this, but it is not recommended.
:::
Instead, do:
``` bash
sudo su
cd /etc/grub.d/
```
Now, we are going to create a file in `/etc/grub.d/`.
``` bash
sudo nano 02_tpm
```
Type all the lines below into it.
``` bash
#!/usr/bin/sh -e
echo "rmmod tpm"
```
Now save and exit by pressing `Ctrl O`, `Enter` and `Ctrl X` on your keyboard in sequence.
To make the `02_tpm` executable, run:
``` bash
sudo chmod +x 02_tpm
```
To regenerate GRUB config files, run:
``` bash
sudo grub2-mkconfig -o /etc/grub.cfg
```
:::success
The problem should be solved now!
:::
## Conclusion
This article has detailed the setup of Btrfs Assistant and Grub-Btrfs on Fedora 40 Workstation Edition. These tools effectively manage the Btrfs snapshots, enhancing system stability and availability.

## References
* :link: [Btrfs Assistant](https://gitlab.com/btrfs-assistant/btrfs-assistant)
* :link: [Typical use cases and Tasks explain](https://github.com/kdave/btrfsmaintenance)
* :link: [python3-dnf-plugin-snapper explain](https://youtu.be/b-M09CsmfN0?t=146)
* :link: [Reddit Post "fedora auto snapshots (snapper)"](https://www.reddit.com/r/Fedora/comments/pfdycf/fedora_auto_snapshots_snapper/)
* :link: [Grub-Btrfs](https://github.com/Antynea/grub-btrfs)
* :link: [Install and Configure Grub-Btrfs](https://youtu.be/dxTjXnw3Eb0?t=1325)
* :link: [Solution for TPM error](https://discussion.fedoraproject.org/t/tpm-error-on-booting/76724/9)