grub-btrfs icon indicating copy to clipboard operation
grub-btrfs copied to clipboard

(Guide for) Configuring systemd to update grub after modifying timeshift snapshots

Open danboid opened this issue 3 years ago • 4 comments

I am running grub-btrfs and timeshift under endeavouros. I want to configure grub-btrfs so that it will update the grub menu after creating a new BTRFS snapshot with timeshift but I was unable to successfully use the example grub-btrfs.path given in the current grub-btrfs README.

grub-btrfs.path depends upon run-timeshift-backup.mount but I have no such file or service installed or configured. How do I create or configure run-timeshift-backup.mount?

I also presume that the user has to configure the PathModified= statement in grub-btrfs.path by checking the path used by timeshift (timeshift --list) so this should be covered in the README too, if required.

danboid avatar Aug 07 '22 20:08 danboid

I have got it working now - systemd now auto updates GRUB when I create or delete timeshift snapshots so I have created a guide to getting this configured correctly.

Configuring systemd to update grub after modifying timeshift snapshots

Neither timeshift nor grub-btrfs will update your grub menu after you create or delete any snapshots so we have to manually configure systemd to do this for us, if we want the ability to boot directly into snapshots from GRUB.

First, we must check the base path used to store your timeshift snapshots. Create at least one snapshot with timeshift and then run timeshift --list

# timeshift --list
Mounted '/dev/sda5' at '/run/timeshift/6724/backup'
Device : /dev/sda5
UUID   : fac5f415-21f5-44e9-93a8-c4b6b5687c72
Path   : /run/timeshift/6724/backup
Mode   : BTRFS
Status : OK
1 snapshots, 387.5 GB free

Num     Name                 Tags  Description  
------------------------------------------------------------------------------
0    >  2022-08-30_21-56-43  O

We can see here that the path of the snapshot is /run/timeshift/6724/backup. If you create more snapshots you will notice a similar path used but with a different 4 digit number so from this we can see it is actually /run/timeshift that we want to monitor for new directories / snapshots.

systemd has a function called PathModified that we'll use to monitor /run/timeshift for changes to update grub but we can only use it with dynamically created dirs such as this by creating a new mount point for it. Edit /etc/fstab and add a line like:

UUID=<UUID> /run/timeshift/       btrfs   defaults,noatime  0 0

You must replace <UUID> with the UUID of your btrfs disk that you are snapshotting. After adding the new fstab entry, reboot and run:

# systemctl list-units -t mount
  UNIT                                              LOAD   ACTIVE SUB     DESCRIPTION                              
  ...
  run-timeshift.mount                               loaded active mounted /run/timeshift

To check systemd has registered the new mount point.

Create /lib/systemd/system/update-grub.service with the following contents, after checking grub-mkconfig -o /boot/grub/grub.cfg is the correct command to update GRUB on your OS. If not, adjust it as required:

[Unit]
Description=Regenerate grub menu

[Service]
Type=oneshot
# Regenerate the grub menu
ExecStart=bash -c 'grub-mkconfig -o /boot/grub/grub.cfg' 

Edit (or create) /lib/systemd/system/grub-btrfs.path with the following content. Replace every instance of run-timeshift.mount with the name of the systemd mount unit name used for the snapshots if the path / unit name differs on your system:

[Unit]
Description="Monitor /run/timeshift for new snapshots"
DefaultDependencies=no
Requires=run-timeshift.mount
After=run-timeshift.mount
BindsTo=run-timeshift.mount

[Path]
PathModified=/run/timeshift
Unit=update-grub.service

[Install]
WantedBy=run-timeshift.mount

Now by enabling / starting grub-btrfs.path, systemd will update GRUB when snapshots are created or deleted.

systemctl enable grub-btrfs.path
systemctl start grub-btrfs.path

danboid avatar Aug 30 '22 22:08 danboid

I'll create a PR to add these instructions to the README if @Antynea proves he's still with us and he deals with the existing PRs.

danboid avatar Aug 30 '22 22:08 danboid

I am working on a daemon right now to watch for snapshots and triggering grub-btrsd instead of relying on systemd mounts and its almost finished. Maybe you want to give it a try. I put instructions into the PR, #218, for more instructions visit https://github.com/Schievel1/grub-btrfs/tree/update-daemon.

Schievel1 avatar Sep 02 '22 21:09 Schievel1

Yes I will. I'm not happy with my current systemd solution because you have to add an extra mount point which seems messy to me. Its one of the reasons I don't like snap packages - extra mounts.

danboid avatar Sep 02 '22 21:09 danboid