Timeshift and grub-btrfs in Ubuntu

UPDATED 22/Dec/2022, ChangeLog:

  • 22/Dec/2022: added the flag “-czstd” for defragmentation and compression.
  • 20/Nov/2022: documented the new version of grub-btrfs and its new grub-btrfsd daemon; the configuration for Timeshift is much simpler, but you have to install another package: inotify-tools.
  • 17/Nov/2022: documented that I could also create additional subvolumes and move existing contents from the running system

In this post, I’ll describe the same procedure I described in one of my previous posts but applied to Ubuntu (in particular, Ubuntu Jammy 22.04). I’m not describing an autosnap mechanism when performing package updates in Ubuntu, though you might want to try timeshift-autosnap-apt for that.

If you choose the BTRFS filesystem when you install Ubuntu, you get the two subvolumes “@” for “/” and “@home” for “/home”, which will make Timeshift work since that’s what Timeshift expects to find to take BTRFS snapshots.

However, some adjustments are still worth making to avoid useless data ending in the snapshots and to be able to boot snapshots directly (with the help of grub-btrfs).

Adjust compression

Ubuntu does not mount BTRFS subvolumes with compression. Instead, I prefer to have compression on BTRFS.

To do that, right after the installation, I change /etc/fstab by adding compression and “noatime” to the existing entries for “/” and “/home” (as usual, UUID must be replaced with the UUID of your disk partition):

Save the file, reboot, and perform “defragment” so that the existing data gets compressed:

It’s also best to create /var/lib/portables and /var/lib/machines if not already there

I’m not using these directories, but it looks like systemd creates them automatically as nested subvolumes. Nested subvolumes will force you to do some manual removal after restoring a snapshot and removing old snapshots.

The current situation of subvolumes should be as follows:

Create additional subvolumes

With the current subvolume layout, things like logs and caches will end up in snapshots, giving you problems when you try to boot a snapshot (which we want to be able to do).

Creating a subvolume for the whole /var directory is not ideal because we lost the contents of essential things like /var/lib in the snapshot, so we could not restore a snapshot correctly. Instead, we create subvolumes to mount /var/log, /var/cache, and /var/tmp.

Since we have to move the existing contents of such directories into the new subvolume, it’s better to operate from a Live ISO.

UPDATE 17/Nov/2022: I also tried to create the new subvolumes and move the existing contents in the running system, following the steps described below. It worked! However, I’d still suggest you do the next steps from a Live ISO because you never know 😉

So, let’s boot a Live ISO (e.g., Ubuntu live installation medium).

Switch to superuser (no password required in the live media):

Mount the BTRFS filesystem into /mnt, which should already exist in the live media. We have to mount the partition hosting our existing installation. In my example, it is /dev/sdb10:

You should see the existing subvolumes in the mount directory:

Create the new subvolumes

Here are the new subvolumes:

Let’s move the contents of /mnt/@/var/log to /mnt/@log and the same for /mnt/@/var/cache into /mnt/@cache

The directory /mnt/@/var/tmp/ should be empty, so there’s nothing to move (you may want to check that).

Adjust the fstab of the installed system to point to the renamed subvolumes and mount the new subvolumes as /var/log, /var/cache, and /var/tmp

You will have to use the correct UUID, which is the same as the one for the already mounted volumes (when you copy and paste, make sure you update both the mount point and the subvolume name consistently):

Remember to double-check everything!

Let’s unmount the partition

and we should be able to reboot the system, hopefully without problems

Install Timeshift

We can install it from the Ubuntu repositories with

NOTE: since this is not the latest version, Timeshift will always generate snapshots in the same directory, so there’s no need for the additional fixed mount point as in Arch (see my previous post).

Let’s create a new Timeshift snapshot and browse it with the Timeshift toolbar button “Browse”. We can verify that the directories @/var/log, @/var/cache, and @/var/tmp are empty in the snapshot. The same holds for home (but that was already true thanks to the initial subvolume for home).

Configure grub-btrfs

We have to install grub-btrfs from sources because there’s no official package for Ubuntu. However, after installing the dependencies,

the installation procedure is straightforward:

We now need to configure that to monitor the Timeshift snapshot directory instead of the default one (/.snapshots).

Updated version (20 November 2022)

UPDATE 20/Nov/2022: A new version of grub-btrfsd is available only by installing from sources. What follows is based on this new version. At the bottom of the post, there are still the old instructions, but they’re not valid anymore.

If you see an ASCII splash screen installing grub-btrfs from sources, you’re using the latest version.

Now, let’s make sure grub-btrfs can find Timeshift’s snapshots (remember, we’ve just created one). So let’s run update-grub, and we should see in the end something like the following output:

The last lines prove that grub-btrfs can detect snapshots.

Automatically update the grub menu upon snapshot creation or deletion (20 November 2022)

UPDATE 20/Nov/2022: A new version of grub-btrfsd is available only by installing from sources. What follows is based on this new version. At the bottom of the post, there are still the old instructions, but they’re not valid anymore.

Grub-btrfs provides a daemon watching the snapshot directory and updates the grub menu automatically every time a snapshot is created or deleted.

Important: This daemon requires an additional package:

By default, this daemon watches the directory “/.snapshots” (the default directory for Snapper). Since Timeshift uses a different directory, we have to tweak the configuration for the daemon.

Let’s run:

We must change the line

into

This is required for Timeshift version 22.06 and later because Timeshift creates a new directory named after their process ID in /run/timeshift every time they are started. Since the PID will be different every time, also the directory will be different. Grub-btrfs provides the command line argument –timeshift-auto to correctly detect the current snapshot directory (In previous versions of grub-btrfs, we had to tweak /etc/fstab to deal with that).

Let’s start the daemon:

In the journalctl log, we should see something like (where the date and time have been stripped off):

Let’s start Timeshift. In the journalctl log, we should see something like this:

Let’s verify that if we create a new snapshot, grub-btrfs automatically updates the GRUB menu: in a terminal window, run “journalctl -f” to look at the log, then create a new snapshot in Timeshift. In the log, you should see something like the following lines:

Similarly, if we delete an existing snapshot, we should see something similar in the log.

Remember that it takes a few seconds for grub-btrfs to recreate the grub menu.

Once we’re sure everything works, we can enable the daemon to always start at boot:

The next time we boot, our grub menu will also show a submenu to boot snapshots.

Concerning doing some experiments booting a snapshot and restoring it, please look at my other post.

Old version (with old release 4.11)

UPDATE 20/Nov/2022: These are the older instructions for the previous version of grub-btrfs, where there was no “grub-btrfsd.service” and there was another systemd program (“grub-btrfs.path”).

I leave these instructions here just for “historical reasons”.

The file contents

should be replaced with

Let’s reload and re-enable the monitoring service:

If we have already created a few snapshots, we can run update-grub and verify that new grub entries are created for the found snapshots:

Let’s verify that if we create (or delete) a snapshot, grub-btrfs automatically updates the GRUB menu: in a terminal window, run “journalctl -f” to look at the log, then create a new snapshot in Timeshift. In the log, you should see something like the following lines (where the date and time have been stripped off):

This output tells you that the grub-btrfs monitoring service works!

The next time we boot, our grub menu will also show a submenu to boot snapshots.

Concerning doing some experiments booting a snapshot and restoring it, please look at my other post.

27 thoughts on “Timeshift and grub-btrfs in Ubuntu

  1. Didier Spaier

    Hello Lorenzo,

    thanks for this well written tutorial. I maintain the Slint distribution https://slint.fr and intend to provide a simple way for our users, including the newbies, to handle issues like “system not booting after an upgrade”, so this post is valuable food for thinking about that.

    Cheers,
    Didier

    Reply
    1. Lorenzo Bettini Post author

      Hello Didier!
      Thank you for the kind words!
      I wish you all the best with your distribution.
      Slackware was my first Linux distro back to… to many years ago O:)

      Reply
  2. Didier Spaier

    Hello Lorenzo,

    instead of relying grub-btrfs I ended up writing my own script (with limited features) : https://slint.fr/testing/snapshots.sh

    However I won’t publish it as is as it uses timeshift commands, which I have found to be less reliable for restoring snapshots that the GUI. Instead, I will rewrite it using btrfs commands directly, removing the dependency on timeshift.

    Reply
    1. Lorenzo Bettini Post author

      Hi Didier,
      Have you considered using snapper? I haven’t used it yet, but if one wants to do without timeshift it might be a good alternative.

      Reply
      1. Didier Spaier

        Hi Lorenzo,
        Yes I have considered snapper. It can write boot entries for the snapshots in the GRUB menu so no need for grub-btrfs if you use it.
        To investigate further I have installed OpenSUSE Leap 15.4 yesterday. Snapper has indeed more features than timeshift and is not as limited wrt the layout of the subvolumes. However I think that my limited use case do not need it, further the way it writes boot entries would not help blind users of Slint. This notwithstanding I will take inspiration on the way they do that.

        Reply
        1. Lorenzo Bettini Post author

          Hi Didier,
          I agree that Snapper is much more powerful but even more complicated.
          For my use case, Timeshift is enough 🙂

          Reply
  3. Jim Abernathy

    Thanks for the post. Very helpful. I installed using this guide on Linux Mint 21.1 and for the defrag and compress I had to use:
    sudo btrfs fi defragment / -r -c
    Otherwise it didn’t compress.

    Reply
  4. diego

    I’m stuck at this stage. Cannot go further, sorry novice here, can you clearly brief here with example mounting command lines. When I try it says there is no …. on etc/fstab. I already try to add subvolumes into fstab so cannot mount the subvolumes at the beginning. Like a dilemma.

    “Adjust the fstab of the installed system to point to the renamed subvolumes and mount the new subvolumes as /var/log, /var/cache, and /var/tmp
    nano /mnt/@/etc/fstab

    You will have to use the correct UUID, which is the same as the one for the already mounted volumes”

    this part a bit for experienced users, please simple it for us too.
    Thanks

    Reply
    1. Lorenzo Bettini Post author

      I’m afraid I can’t think of anything more detailed than this. That’s an advanced topic that assumes you’re familiar with the basics of Linux filesystems and with btrfs in general.

      Reply
  5. federico

    I followed for Tuxedo OS but could not mount as a novice user. Would be nice if you look for it

    Reply
    1. Lorenzo Bettini Post author

      As I said in the other answer, that’s an advanced topic that assumes you’re familiar with the basics of Linux filesystems and with btrfs in general. Moreover, if you use another distribution, you’ll have to adapt things yourself.

      Reply
  6. brian mulllan

    Lorenzo

    One question came to mind regarding your Guide on BTRFS and grub-btrfs installation & configuration.

    I’m currently running Ubuntu 22.04.2 LTS on this particular machine.

    When the next LTS (24.04 LTS) is released, would you expect any problem during the automated Ubuntu Upgrade process. For example, the new 24.04LTS would not know anything about grub-btrfs.

    Thanks for any thoughts regarding future OS upgrades.

    Brian

    Reply
    1. Lorenzo Bettini Post author

      Hi Brian

      since you installed grub-btrfs from the git repository, the Ubuntu update process will not touch it. These programs are shell script so I would expect them to keep on working. Of course, doing an update of the git repository of grub-btrfs or a fresh installation from git will give you the latest version of grub-btrfs, which might or might not work with the new version of Ubuntu. It would be best to check grub-btrfs GitHub repository for possible bug fixes or updates.

      Reply
    1. Lorenzo Bettini Post author

      Not the whole /var, only some of its subdirs that are not meant to be part of a snapshot. That’s the same scheme adopted by endeavouros.

      Reply
  7. brian mullan

    Lorenzo.. I am still running Ubuntu 22.04.3 LTS using grub-btrfs and the system installed/configured to use BTRFS as per your article here.

    But with April 2024 approaching faster than I realize, I thought I’d ask if you might see any btrfs-grub or btrfs related issues in an Ubuntu LTS upgrade from 22.04.3 LTS to 24.04 LTS. Such as, would the upgrade overwrite configs for anything already working.

    Reply
    1. Lorenzo Bettini Post author

      Brian, I’m not using timeshift anymore: I switched to snapper, which, at least in Arch, is automatically configured with grub-btrfs and works like a charm (see https://www.lorenzobettini.it/2023/03/snapper-and-grub-btrfs-in-arch-linux/). Snapper is much more powerful and easier to deal with especially with the program btrfs-assistant. I haven’t been using Ubuntu for a while as well.

      Technically, there should be no problem. Of course, the best thing to do is to follow the GitHub repository of the related projects, and, for sure, to do some experiments (e.g., in a VM) with the current development release of Ubuntu to catch possible problems before it’s officially released.

      Reply
      1. brian mullan

        I guess I should look at snapper since I’ve seen quite a few people comment on it in the past 6 months.

        Thanks

        Reply
        1. Lorenzo Bettini Post author

          Yes, I suggest you take a look at snapper, which is much more flexible and easier (especially with btrfs-assistant)

          Reply
  8. Shawn

    This works for OpenMandriva too, with slight modifications to the instructions.

    1) You need to “sudo dnf install make git” instead of “sudo apt install build-essentials git”, because OpenMandriva doesn’t have make installed by default.

    2) You need to “sudo systemctl enable crond.service” because OpenMandriva doesn’t have cron enabled by default.

    3) You need to “sudo ln -s /boot/grub2 /boot/grub” and “sudo ln -s /usr/bin/grub2-script-check /usr/bin/grub-script-check” because OpenMandriva uses the “grub2” set of filenames for GRUB-related files, and Timeshift won’t find them without symbolic links to the filenames it’s expecting.

    Hopefully someone with OpenMandriva will benefit from this info.

    Reply
    1. Lorenzo Bettini Post author

      Thanks for the updates.

      Please note that the 3rd point is not strictly necessary: you can tweak the grub-btrfs configuration file with the paths for grub2 (they are documented in the configuration file for fedora based distros by the way).

      Reply
      1. Shawn

        I prefer to use symlinks instead of editing the configuration file, because symlinks can’t possibly be overwritten by possible future changes to grub-btrfs’ source code, and simultaneously solve similar problems for any other utilities looking for system files with specific names.

        Reply
  9. Pingback: A look at Ubuntu 23.10 Mantic Minotaur | Lorenzo Bettini

  10. till

    Thank You very much for this! I made my own Xubuntu Clone. Now i have my encrypted de-snapted adblocking and Auto-Snapshot Grub2 System. Everything is very fine!

    Reply

Leave a Reply to Didier SpaierCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.